简体   繁体   中英

PHP Session variables not saving between pages?

Login page:

<?php

session_start();

#echo session_id ();
$_SESSION["auth"]= "yes";

echo '<a href="Portaltest.php?t='.time().'">portal page link.  Auth set to: {$_SESSION["auth"]}</a>';

?>

Portal page:

<?php session_start();

echo "auth = {$_SESSION["auth"] } <BR />";
echo session_id ();


?>

The auth session is lost between the two pages somehow!

Edit

Here is a test url:

http://proserv01.services.lyris.com/NFPInsurance/UnsegmentedMemberReport/logintest.php

When trouble-shooting sessions, there are a few things I tend to do, but let's start with your code.

Here is an updated version of your page code so you actually see the value stored in $_SESSION['auth'] (your quotes were causing some trouble):

<?php

session_start();
$_SESSION["auth"] = "yes";
echo '<a href="Portaltest.php?t='.time().'">portal page link.  Auth set to: ' . $_SESSION["auth"] . '</a>';

?>

Here is the updated version of the portal page, which removes the additional space after the closing curly bracket:

<?php

session_start();
echo "auth = {$_SESSION["auth"]} <BR />";

?>

Now, if you don't see the auth with these revisions, you can try:

  1. Changing the code in portal so it just dumps out the session so you can see what you've got: session_start(); var_dump($_SESSION);
  2. Checking to make sure the error reporting is enabled, as PHP helps you identify many potential issues quite quickly (eg, index doesn't exist, the headers were already sent, etc.): ini_set('display_errors','1'); error_reporting(E_ALL);
  3. You can check your PHP config file (php.ini) to make sure that there are no settings causing session issues directly.

NOTE: For testing purposes only.

I am unsure as to what the expected results are, yet I will submit this as an answer with explanations set inside PHP comments.

Give this a try:

<?php

session_start();
$_SESSION["auth"]= "yes";

// will echo: portal page link. Auth set to: yes
echo '<a href="Portaltest.php?t='.time().'">portal page link.  Auth set to: ' . $_SESSION["auth"] . '</a>';
echo "<br>";

// will echo: auth = yes
echo "auth = {$_SESSION["auth"] } <BR />";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM