0

I am running a script whitch is ruuning another script in background

exec('php index.php test/test/'.Session::instance()->id().' > /dev/null &');

As you can see in the example above, I am passing the session ID to it simply because I need to have the same exact session it the script run in background.

Is there a way to recreate session with all its data by knowing the session ID?

2 Answers 2

3

Use session_id($theKnownID).

Sign up to request clarification or add additional context in comments.

Comments

1

There is an inbuilt shm handler in session_*

Or you could implement shared memory in assigned slots, however you would also have to garbage collect these yourself. see http://theserverpages.com/php/manual/en/ref.shmop.php

As for session, see following, you will either have to choose your own unique ID or let the script print it out first time and then re-use.

Keep in mind, the settings for session.*, in particular lifecycle span, i.e. session.gc_maxlifetime

<?php
$cmd=array_shift($argv); // skip 'php script.php' entry
while(count($argv) > 0) {
    $cmd = array_shift($argv);
    if($cmd == "--sid") {
        // SESSID can be anything if we set it manually,
        // default it looks like an md5 like so:
        // b589a6p6s7na1o2ojhff3m1pl7
        $sid = array_shift($argv);
    } else {
        $saveme = $cmd;
    }
}
// must call set session id before starting session
if(isset($sid)) session_id($sid);
session_start();

if(isset($saveme)) $_SESSION['store'] = "iamsaved:".$saveme;
if(isset($_SESSION['store']))
    echo "Session variable :" . $_SESSION['store']."\n";
echo "SID: ".session_id() ."\n";
?>

Try this, save as 'script.php', go terminal and enter:

php script.php SavedVariable 

Should output as

   ' Session variable :iamsaved:SavedVariable'
   ' SID: sf24r35d98e7smah9abq7ao1i5'

Then take the SID and use as parameter for next run:

php script.php --sid sf24r35d98e7smah9abq7ao1i5

Output will be identical, using the retreived $_SESSION, accessing 'store' value.

Following will also work and let you choose your own SID Note: PHP Warning: session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' :

php script.php --sid validassockey_as_my_sid AnotherSavedVariable

Outputs:

   ' Session variable :iamsaved:AnotherSavedVariable'
   ' SID: validsessionid_as_my_sid'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.