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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smithygreg
Forum Newbie
Posts: 5
Joined: Tue Oct 16, 2007 11:24 pm

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

Post 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]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
smithygreg
Forum Newbie
Posts: 5
Joined: Tue Oct 16, 2007 11:24 pm

Session is started...

Post 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]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
smithygreg
Forum Newbie
Posts: 5
Joined: Tue Oct 16, 2007 11:24 pm

Still not working...

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

whoa - did NOT know that!

That's why I love DevNet 8)
smithygreg
Forum Newbie
Posts: 5
Joined: Tue Oct 16, 2007 11:24 pm

Update!

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Still not working...

Post 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' ) ;
(#10850)
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post 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.
smithygreg
Forum Newbie
Posts: 5
Joined: Tue Oct 16, 2007 11:24 pm

Argh!

Post 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
Post Reply