session_start() creates new session every refresh

2019-08-10 21:59发布

问题:

This question already has an answer here:

  • How to fix “Headers already sent” error in PHP 11 answers

I am having an issue with session_start(). It is creating a new session every refresh/load of the page.

here is the code:

<?php
    $bob = session_id();
    echo "Session ID on load is ".$bob;
    echo "<br>";
    if($bob==""){
        session_start();
        $bob = session_id();
        echo ' session ID currently is '.$bob;
    }
// a bunch more stuff

when i load the page, I get the following:

Session ID on load is session ID is currently ed320bc5e24c871c9db8ea30e6796c14 (or a variant)

if I refresh the page i get:

Session ID on load is session ID is currently fbd69d01d511a7be382799dca7279a86 (or a variant)

the session Id is always blank before session_start() is called and it is always a new session_id()

It does this in all browsers and I have checked to make sure cookies are turned on.

the session save path is given as /tmp. I am not sure exactly where that is, but looking through my root and all other directories, I can't find a session file (assuming it would look something like sess_fbd69d01d511a7be382799dca7279a86).

So I am thinking there is something going on with the save path, but am too new at this to know for sure, and the server admins are being fairly unhelpful. What should my next steps at diagnosing the issue be? The server is running 5.3.22.

phpinfo is here

Thanks for any help.

ps you can visit pcm.pcmxa.com to see the issue for yourself if you wish.

回答1:

If your session directory (most like /tmp as you said) is not writable, then it won't be able to save and will have to regenerate a new one each time. Here is how you can verify it:

if ( !is_writable(session_save_path()) ) {
   echo 'Session save path "'.session_save_path().'" is not writable!'; 
}

If that is the case, you will need to have the server admins give what ever user your webserver runs as permission to write to the /tmp directory.