0

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.

WilliamAlexander
  • 374
  • 2
  • 4
  • 20

2 Answers2

0

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

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Thank you for the Update Pedro - I should have noted in my initial post that I did try changing the session path as one of my earlier troubleshooting tests. – WilliamAlexander Apr 14 '17 at 12:28
  • Have you tried outputting `$_POST` content without redirection? Is there any content ? does it show any errors ? – Pedro Lobito Apr 14 '17 at 12:31
  • You may want to take a look at the last comment on this question: http://stackoverflow.com/questions/14631033/sessions-not-working-on-my-blue-host-server – Pedro Lobito Apr 14 '17 at 12:33
  • Thank you - I had seen that but was certain the files were without BOM - nevertheless I resaved the files to be sure, still the problem persists. – WilliamAlexander Apr 14 '17 at 12:44
  • I'm sorry but I really don't know how to help you further! – Pedro Lobito Apr 14 '17 at 13:08
0

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.

WilliamAlexander
  • 374
  • 2
  • 4
  • 20