0

My session variables are resetting every time I load a new page.

I have included a header.php file to each script with the session_start() function at the very top like so:

session_start();
error_reporting(E_ERROR);

I have also checked to make sure that the session_id is the same across all pages and it is.

I checked my web host's php_ini config file and I saw that the session.save_path was set to /tmp. I changed it to /var/lib/session/ like someone had suggested on this site and I began to get odd warnings in my code.

Warning: session_start() [function.session-start]: open(/var/lib/php/session//sess_97fca6d21c7ffa8333cd42eaa87f2eac, O_RDWR) failed: Permission denied (13) in /home/mforsyth/public_html/Beesting/header.php on line 2

I do not know what to do to fix this problem. Any help would be useful. If more details are needed please let me know.

EDIT: I have changed the folder back to /tmp and have made sure i can read/write into it and I can. I have also echoed the session id on every page and it all comes out the same. Also it seems that the session only lasts for one page

4 Answers4

2

What happens is that php tries to track your sessions with some information it writes to the directory that ThinkingMonkey mentioned.

As the directory is not writable by the php/webserver process' user, this fails. Thus you don't get a session.

Find out which user the process is running under and grant him the read/write right for that directory.

Kai Mattern
  • 3,090
  • 2
  • 34
  • 37
1

Thanks for the help. After further investigation and talking to my host about the matter, I was able to find conclude that the problem was NOT the capability of writing to the /tmp folder. In fact what the problem really was, was a javascript function in my header.php include file.

function logout()
{
    <?
session_destroy();
?>
alert("you have been logged out");
}

It was avoiding the fact that it was in a function, probably my fault seeing how the two languages are compiled differently. I did a simple ajax call to take care of the session destroy and all is well now. I wonder if anyone else out there has similar problem and if this helps them.

  • Javsscript and PHP are mutually exclusive languages, a JS function will be ignored in PHP except for the part within the PHP tags, hence that part will always be true whenever php leads that script. And PHP will always load and run before JS gets a look in. – Martin Jan 11 '17 at 01:42
0

Try restarting the webserver and php service, in case of nginx/php-fpm, try

root@server > service nginx restart
root@server > service php-fpm restart

that should do the trick!

UserBSS1
  • 2,091
  • 1
  • 28
  • 31
0

Had the same general problem and it turns out for some reason I was using JavaScript to delete the session cookie.

document.cookie = '[session_name]' +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
John
  • 1
  • 13
  • 98
  • 177