Does anyone know how to create some code that will destroy a session id and instantly generate a brand new one?

So far I have tried (amongst other things);

  • session_destroy() followed by session_start()
  • the unset function
  • hard coding a new session id myself (eg, $PHPSESSID="asdf")
  • the manual
  • this forum

...but so far I can't find anything that works for this seemingly straightforward task. Any help would be mega appreciated.

    This may work for you

    <?php
    //Re-assigns sessin id's on every page refresh
    session_start();	//start the session
    $old_sessid = session_id();	//assign the session id to a variable
    session_regenerate_id();	//regenerate the session
    $new_sessid = session_id(); //assign the new session id to a variable
    session_id($old_sessid);	//assign the old session id to session id in order to destroy it
    session_destroy();	//destroy the session
    $old_session = $_SESSION; // copy the $_SESSION array to be able to
    				//use the data associated with the old session id.
    session_id($new_sessid); //change the session id
    session_start();	//start the session
    $_SESSION = $old_session;	//copy the old SESSION array to the new SESSION
    ?>

      Thanks.

      The situation now is that the session_regenerate_id() function is not working on my computer at home when I'm testing the scripts, but it works when I upload it to my website.

      My computer at home uses PHP version 4.1.1 (which came as part of PHP triad) and I'm trying desperately to figure out a way of upgrading to a more recent version of PHP. Maybe that'll help.

        Write a Reply...