Re: Re: SESSION and include
| From: | Fredrik Tillman | Date: | Wed, 08 Feb 2006 16:11:13 +0000 |
| Subject: | Re: Re: SESSION and include | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-230104@lists.php.net to get a copy of this message | ||
ok.. I was a little too fast again when explaining my problem.. Gonna put some code up for you to see...
First of all I DO use quotes. I tried both single and double ones (',")
Ok here are some code:
IN LOGINSCRIPT:
<?
/* Check User Script */
session_start(); // Start Session
include 'db.php';
// Convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];
if((!$username) || (!$password)){
echo "Please enter ALL of the information! <br />";
include 'index.htm';
exit();
}
// Convert password to md5 hash
$password = md5($password);
// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);
if($login_check > 0){
while($row = mysql_fetch_array($sql)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );}
$_SESSION['first_name'] = $row["first_name"];
$_SESSION['last_name'] = $row["last_name"];
$_SESSION['email_address'] = $row["email_adress"];
$_SESSION['user_level'] = $row["user_level"]; header("Location: index.php");
}
} else {
echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />Please try again!<br />";
include 'index.htm';
}
?>
IN INDEX.PHP:
<?
session_start();
if ( empty( $_SESSION['first_name'] ) ) {
?> <center><font color=red><strong>You are not logged in!</strong></font></center><br />
<? include 'index.htm';
} else { include 'db.php';
include("http://test.bluenotevoices.se/data.php");
?>
[... Some code here...]
/* defining variables ... ($main is actually transered from last page the user was on, so in my script it is not defined but I put the code here to let you see it...) */
$main="page.php"
<?php if ("1"==$_SESSION['user_level']){
?> [ Some working code here ]
<? }
require ($main);
?>
}
IN PAGE.PHP
<?php
session_start();
include "data.php";
if (empty($_SESSION['user_level'])) { echo ("Session is empty");}
?>
[More code here]
Can you see anything I am doing wrong?
/F
Jochem Maas wrote:
Fredrik Tillman wrote:ok.. Let me explain the problem better. 'user_level' is set by a login script. It seems to be working fine since I can make things like: if ("1"==$_SESSION[user_level]) { let this stuff happen }$_SESSION[user_level] is wrong unless 'user_level' is a defined constant in your code (I bet it isn't). instead write: $_SESSION['user_level'] -- notice the quotes, they delimit the _string_ that is the key of the array. array keys are either strings or integers.on my mainpage. on that same mainpage I use include ("page.php"); (I also tried require...) If I access page.php directly (by writing the URL in my browser) things like if ("1"==$_SESSION[user_level]) { let this stuff happen } will work just fine, but when page.php is included in mainpage the $_SESSION[user_level] is empty for that included part...how is $_SESSION['user_level'] being set and/or updated? have you tried a var_dump($_SESSION) on the included page to see what _is_) in the session? are you per change closing the session prior to including the relevant file? I think you'll need to show some code - the way you describe it seems to me to be not possible that $_SESSION['user_level'] is set in the first script and nolonger available in the included script ($_SESSION is a super global that is available everywhere so long as there is an active/open session)