Page 1 of 1

Why aren't my session variables available on other pages?

Posted: Wed Oct 17, 2007 12:12 am
by smithygreg
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey There..I am totally new to php programming so please excuse me if this is a stupid question...
I want to store whether the user is logged in in a session variable...So, In my login page, I check their password against the databse, then I call this code...The session IS started on this page...

Code: Select all

$_SESSION['IsAdmin']=1;
header( 'Location: AdminIndex.php' ) ;
In the AdminIndex Page, I am running this code...

Code: Select all

<?php

session_start();

if(isset($_SESSION['IsAdmin']))
{
echo "Set";
}
else
{
echo "Not Set";

}
$_SESSION['Test']="TEST";
echo $_SESSION['Test'];
?>
This code echoes.."Not SetTEST"...So, The variables are OK when they are on the same page, but not cross pages.
I have echoed session_id() on both the login page and the result page and the id is the same...

Is there some config variable that isn't set? I am using WAMP and I don't believe I have changed any of the default settings.

Thanks in advance!

Greg


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Oct 17, 2007 12:38 am
by Christopher
session_start(); loads the data into the $_SESSION variable. You need to call it on every page that uses $_SESSION before $_SESSION is used.

Session is started...

Posted: Wed Oct 17, 2007 1:13 am
by smithygreg
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


The session is started on the page where I am setting the variable.
This is the code from that entire page...

Code: Select all

<?php session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validate Admin</title>
</head>

<body>

<?php 
include("misc.inc");

$connection = mysql_connect($host,$user,$password) or die("Couldnt connect..what a damn surprise");
$db = mysql_select_db($database, $connection) or die("Couldn't connect to database..another big surprise");

$pwd = $_POST["pwd"];
$query = "SELECT * From admininfo WHERE ID='blahblah';";
	$result = mysql_query($query) or die("Couldn't execute query2");
	while ($row = mysql_fetch_array($result))
		{
			if ($pwd==$row['pass'])
			{
				$_SESSION['IsAdmin']=1;
				header( 'Location: AdminIndex.php' ) ;
			}
			else
			{
				header( 'Location: AdminLogin.php?Result=Failed' ) ;
			}
		}


?>
		
</body>
</html>
I know that the databse stuff is working..I am getting redirected to the correct page, but $_SESSION['IsAdmin'] is not set on the next page...

Thanks!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Oct 17, 2007 1:18 am
by Christopher
If you are redirecting then you need to do a session_write_close() before the redirect to write the data. Otherwise the next script will read the old session data because it is all happening in the same request. Normally the session data is written at the end of a request.

Still not working...

Posted: Wed Oct 17, 2007 1:27 am
by smithygreg
Hmm..Still not working...
This is my code for the redirect now...

Code: Select all

$_SESSION['IsAdmin']=1;
session_write_close();
header( 'Location: AdminIndex.php' ) ;
Could it be the redirect that is killing it? What would be another way of going about this?

Posted: Wed Oct 17, 2007 1:29 am
by Kieran Huggins
whoa - did NOT know that!

That's why I love DevNet 8)

Update!

Posted: Wed Oct 17, 2007 1:45 am
by smithygreg
Update!
Even though everywhere I have looked, they say not to use this, I tried this using session_register() and I still have the same problem...
HELP!
THANKS!

Re: Still not working...

Posted: Wed Oct 17, 2007 2:31 am
by Christopher
Simplify and see if your sessions are working at all, or something simple like:

Code: Select all

session_start();
$_SESSION['IsAdmin']=1;
session_write_close();
header( 'Location: AdminIndex.php' ) ;

Posted: Wed Oct 17, 2007 4:57 am
by crystal ship
Try to get rid of the <html> tags, allow not even a space or empty line before the header() function. Then it will work.

Argh!

Posted: Wed Oct 17, 2007 9:27 am
by smithygreg
Still no luck...
I took out all HTML..I even removed all php from the page and now it's just a page with 3 lines that sets the session variable then forwards to the next page...
Could this have something to do with the fact that I am running this on WAMP and debugging it from Dreamweaver?
I can't believe it could be this hard...Whats wrong with me???
Thanks for all the help so far..
-Greg