3

I want to restore all PHP session variables from an earlier session using a value hashed and salted and then stored in a cookie. I use this value to retrieve the old session id and then try to load that session.

if(isset($_COOKIE['user']))
    {
        $user = sanitizeString($_COOKIE['user']);   
        $result = queryMySQL("SELECT sessionID FROM cookie WHERE user='$user'");

             if ($result->num_rows == 1)
                {     
                    $row = $result->fetch_array(MYSQLI_ASSOC);              
                    //echo $row['sessionID'];
                     $storeResult=$row['sessionID'];
                     session_id($storeResult);
                }     
    }

session_start();

Of existing posts on stackoverflow, most on point is How to restore a PHP session? but it doesn't actually address retrieving sessions that have not been deleted by the server. So long as the sessions are still on the server, shouldn't I be able to reload them with these two lines of code?

session_id($storeResult);
session_Start();

Right now I see that the cookie is set but the session variables (e.g. $_SESSION['votes']) never are set. All I am doing is closing the browser and reopening, so the time after the initial session is minimal.

From prior postings, there doesn't seem to be a direct way to check whether a session with a particular id exists. Any other suggestions for troubleshooting this? Thank you.

Community
  • 1
  • 1
  • Have you checked whether your browser deletes cookies on close? this is often a configuration – NiMeDia Nov 13 '14 at 06:36
  • Thank you. Yes I checked, the cookie persists. For example I can echo or print a javascript alert with the value of the cookie but then when I check whether a session variable is set or try to print the session variable, it comes up empty. – Sunnyside Productions Nov 13 '14 at 14:14
  • If you’re using the `sanitizeString` function from the book *Learning PHP, MySQL, JavaScript, CSS & HTML5*, then [you shouldn’t be using it](http://stackoverflow.com/a/2940101/53114). – Gumbo Nov 14 '14 at 19:28
  • @Gumbo I am indeed using this. What's wrong with it? – Sunnyside Productions Nov 14 '14 at 19:30
  • @SunnysideProductions Read my linked answer. – Gumbo Nov 14 '14 at 19:31

0 Answers0