简体   繁体   中英

Session data not saving

I am debugging a php session that worked when I last tested it, a couple of months ago. The code has not changed in any way, but the session has now stopped working.

I have read multiple questions here as well as other articles but no suggestions have solved this problem. I believe my code is correct, but when I asked support at Bluehost, they say it must be a code problem:

I am starting a session and setting a few session variables:

<?php
    session_start();

$_SESSION["franchise_name"] = $_POST["name"];
$_SESSION["db_name"] = $_POST["name"];
$_SESSION["franchise_location"] = $_POST["franchise_location"];
$_SESSION["franchise_phone"] = $_POST["franchise_phone"];
$_SESSION["franchise_address"] = $_POST["franchise_address"];
$_SESSION["franchise_email"] = $_POST["franchise_email"];

header("Location: session.php"); /* Redirect browser */
exit;

?>

If I echo the session variables immediately after setting them, all is well. Good stuff. So I know everything works in this section.

session.php looks like this:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
    session_start();
echo 'Testing Output:';
echo session_status();
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
phpinfo();
?>

So when I test this, no data is being passed in the session. The output on session.php is:

Testing Output:2
array(0) {
}

I also set up a tester to see if session data is enabled:

<?php
// Start Session
session_start();
// Show banner
echo '<b>Session Support Checker</b><hr />';
if (!is_writable(session_save_path())) {
    echo 'Session path "'.session_save_path().'" is not writable for PHP!<br />'; 
} else {
    echo 'Session path "'.session_save_path().'" is writable for PHP!<br />'; 

}
// Check if the page has been reloaded
if(!isset($_GET['reload']) OR $_GET['reload'] != 'true') {
   // Set the message
   $_SESSION['MESSAGE'] = 'Session support enabled!<br />';
   // Give user link to check
   echo '<a href="?reload=true">Click HERE</a> to check for PHP Session Support.<br />';
} else {
   // Check if the message has been carried on in the reload
   if(isset($_SESSION['MESSAGE'])) {
      echo $_SESSION['MESSAGE'];
   } else {
      echo 'Sorry, it appears session support is not enabled, or you PHP version is to old. <a href="?reload=false">Click HERE</a> to go back.<br />';
   }
}
?>

The result of this test shows that the session data didn't work here either and that the session path is indeed writable. Phpinfo shows that sessions are enabled.

Is there anything else I can try to help me troubleshoot this issue? Thanks.

Update: I did try seeting the session path with ini_set(' session.save_path','SOME WRITABLE PATH'); but that did not solve the problem.

from php session_start() page:

Note :
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

remove the spaces before session_start() , they are considered output.


Update:

It can also be related to the location of the session files, you may try setting a new location:

ini_set('session.save_path','/some/safe/path/to/sessions');
session_start();

Make sure sessions has the appropriate permissions

I called Blue Host - this second support person was more alert than the first and recognized that Varnish Caching could cause the problem. We disabled it and voila! Fixed.

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