PHP Sessions Not working
Solution 1
Make sure cookies are enabled so the second page actually re-creates the session of the first page instead of creating a new, empty one. Check out http://www.php.net/manual/en/session.idpassing.php for details.
EDIT
Another possible cause for sessions not working as expected, especially if it only affects certain pages, is that you have accidental whitespace or other output before your first 'session_start() error
Solution 2
It looks like you are just redirecting the user to the second page without posting anything. You would want to set hidden variables and actually submit your form to the second page. Re-directing will lose all the post variables.
If you want to send variables to the second page with a redirect, you can use session variables or a GET variable passed in with the URL to the second page.
Solution 3
Try using
$_SESSION['myvariable'] = 'Hello World';
in upper case
Josh
Updated on September 24, 2020Comments
-
Josh over 3 years
I just can't get my variables to carry over to the next page. The variable assignment works fine on the initial page (tested), but the value goes null when going to the next. Plz Help!
Page 1:
<?PHP session_start(); ?> <HTML> <HEAD> <META NAME='robots' CONTENT='noindex,nofollow'> <TITLE>Master Chief's Gamestore</TITLE> <STYLE type="text/css"> BODY { color: #ffffff; background: url('~/halo-reach.jpg'); background-repeat: no-repeat; background-position: top; background-color: black; } </STYLE> </HEAD> <body vLink='#3366CC' link='#3366CC' alink='#3366CC'> <br/><br/><br/><br/><br/> <table width='100%' height='80%'> <tr> <td align='center' valign='middle'> <table width="50%" cellspacing="0" cellpadding="25" border="0"> <form method="post"> <tr bgcolor="#666633"> <th id="header" align="left" colspan="3"> <DIV align="center"> <font face="Verdana, Arial, Helvetica" color="white" size="3"> Master Chief's Gamestore </font> </DIV> </th> </tr> <tr bgcolor="#A3A385"> <td width="100%" valign="middle" colspan="2"> <font face="Verdana, Arial, Helvetica" size="2"> <div align="center"><br/><br/> Username:  <input type="text" name="username">  Password:  <input type="password" name="password"><br/> <br/><input type="submit" name="act" value="Login"><br/> <input type="submit" name="act" value="Register"><br/> <input type="submit" name="act" value="Main"><br/> <br/><br/> </div> </font> </td> </tr> <?PHP if ($_POST['act'] == "Login") { mysql_connect("~", "~", "~") or die("unable to connect to server"); mysql_select_db("~") or die("unable to select database"); $user = $_POST['username']; $pass = $_POST['password']; $query = "select * from users where user like '%" . $user . "%' and pass like '%" . $pass . "%';"; $result = mysql_query($query); $rows = mysql_numrows($result); if ($rows == 0 || strlen($user) == 0) { echo "<br />Login Failure"; } else { $_SESSION['user'] = mysql_result($result, 0, "user"); $_SESSION['id'] = mysql_result($result, 0, "P_Id"); header('Location: ~/success.php'); } } if ($_POST['act'] == "Register") { header('Location: ~/register.php'); } if ($_POST['act'] == "Main") { header('Location: ~/index.php'); } ?> <div align="center"> <tr bgcolor="#666633"> <td align="left"> Logged in as: <?PHP $user = $_SESSION['user']; if (!$user) { $user = 'Guest'; } echo $user; ?> </td> <td align="right"> Password:  <input type="password" name="srcpw"> <input type="submit" name="dspphp" value="Display PHP"> </td> </tr> </div> </form> </table> </td> </tr> </table> </body> </html>
Page 2:
<?PHP session_start(); ?> <HTML> <HEAD> <META NAME='robots' CONTENT='noindex,nofollow'> <TITLE>Master Chief's Gamestore</TITLE> <STYLE type="text/css"> BODY { color: #ffffff; background: url('~/halo-reach.jpg'); background-repeat: no-repeat; background-position: top; background-color: black; } </STYLE> </HEAD> <body vLink='#3366CC' link='#3366CC' alink='#3366CC'> <br/><br/><br/><br/><br/> <table width='100%' height='80%'> <tr> <td align='center' valign='middle'> <table width="30%" cellspacing="0" cellpadding="25" border="0"> <form method="post"> <tr bgcolor="#666633"> <th id="header" align="left" colspan="3"> <DIV align="center"> <font face="Verdana, Arial, Helvetica" color="white" size="3"> Master Chief's Gamestore </font> </DIV> </th> </tr> <tr bgcolor="#A3A385"> <td width="100%" valign="middle" colspan="2"> <font face="Verdana, Arial, Helvetica" size="2"> <div align="center"> <br/><br/>Success<br/><br/> <input type='submit' name='main' value='Main'> <?PHP if ($_POST['main'] == "Main") { header('Location: http://~/index.php'); } if ($_POST['act'] == "Display Code") { if ($_POST['pw'] == "pass") { highlight_file("success.php"); } } ?> </div> </font> </td> </tr> <div align="center"> <tr bgcolor="#666633"> <td align="left"> Logged in as: <?PHP $user = $_SESSION['user']; if (!$user) $user = 'Guest'; echo $user; ?> </td> <td align="right"> Password:  <input type="password" name="pw"> <input type="submit" name="dspphp" value="Display PHP"> </td> </tr> </div> </form> </table> </td> </tr> </table> </body> </html>
EDIT: I've tried this sample code and it does NOT work.
Page 1:
<?php session_start(); if (isset($_GET['link'])) { $_session['myvariable'] = 'Hello World'; header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php'); exit; } ?> <a href="<?php print $_SERVER['REQUEST_URI'] . '?link=yes'; ?>">Click Here</a>
Page 2:
<?php session_start(); print 'Here is page two, and my session variable: '; print $_session['myvariable']; exit; ?>
But as I posted below, I have used sessions in other pages successfully on the same server. Soo frustrating....
Thanks for all the posts btw!
-
Marc B about 13 years
http://~/index.php
That's not a valid URL, unless you've got a site on your local network named~
. -
Josh about 13 yearsI've changed the code to not include private information.
-
Thilo about 13 yearsCheck my edited answer regarding headers sent if you haven't yet.
-
Josh about 13 yearsYou fixed it, thanks Thilo :))
-
-
Josh about 13 yearsMaybe my formatting is confusing. My assignment is enclosed within if($_POST['act'] == "Login"). Is this what you mean?
-
Mikecito about 13 yearsI see you check for the variable, but the variable never makes it to the second page because of your redirect (Header/Location line) on the first page. When you redirect like that it does not pass any POST variables to the next page. So you would either have to pass it with a GET or with a SESSION variable to the next page. Or you could use Javascript to submit the form from the first page to the second page, but I suggest a session variable in case people do not have Javascript enabled.
-
Thilo about 13 yearsThat's what he's doing - just before the redirect, he assigns values to $_SESSION. Problem is, $_SESSION is empty on the subsequent page.
-
Mikecito about 13 yearsYeah, the assignments are there, but he is still checking for POST variables on the second page. Those will always be null unless he posts something to it.
-
Thilo about 13 yearsThat's true, but I'm assuming he's looking for the $_SESSION['user']. Josh, can you clarify in your question which line of your page 2 is not working as expected?
-
Josh about 13 yearsAt the bottom of the page where is says "Logged in as:"
-
Josh about 13 yearsI've run sample code and it does not work, but I've used sessions before on this same server, pages that are still hosted and working properly.
-
Josh about 13 yearsI mean the code following that line, so yes you are correct the $_SESSION['user'] variable is the one that is not carrying over.
-
Josh about 13 yearsI had to clear the page cookie to get it to work. Thank you SO much for pointing me down the cookie path!
-
Josh about 13 yearsYeah trusting someone elses test code was probably a mistake. I sent that file to the recycle bin! Thanks anyway though :)