5

How to set a PHP session variable through linux command prompt?

Clarification

So, as you know we can set session variables in PHP using $_SESSION global variable when coding. I would like to know if there is a way to set this variable through php command prompt?

For example, in the code, if I can set $_SESSION['temp'] = "whatever"

Is there a way to set the same variable through command prompt PHP?

5
  • Could you elaborate a bit more on what you are trying to accomplish? Commented Jan 2, 2011 at 0:29
  • 2
    I think you will get a more complete answer on Stack Overflow. Commented Jan 2, 2011 at 1:15
  • Agreed, I don't think this is going to be Linux-specific Commented Jan 2, 2011 at 1:24
  • Who's session are you setting ? Commented Jan 2, 2011 at 1:44
  • I'm writing a very specific command line application (or trying to) that automates a few tasks, that requires this operation. It is my own session for now. Commented Jan 2, 2011 at 11:01

4 Answers 4

6

PHP's default session handler stores the session data in serialize() format in a file, which means it's basically plain-text. You could certainly manipulate that file from the command line, using any of the standard unix text manipulation tools (perl, sed, awk, even echo/cat in a shell script, etc...), as long as you don't introduce a syntax error into the serialized data.

But at that point, unless you find a function/library/module that does unserialize() and most likely serialize() as well, you might as well just PHP itself to do the manipulation. It'd be a pretty rare system that doesn't have the CLI version of PHP installed alongside the webserver version.

$dat = file_get_contents('/path/to/session/file');
$session = unserialize($dat);
$session['temp'] = 'whatever';
$dat = serialize($session);
file_put_contents('/path/to/session/file', $dat);
Sign up to request clarification or add additional context in comments.

Comments

2

I've handled debugging code which uses $_SESSION variables by setting the relevant variables in a separate file (i.e. session_vars.php), then doing the following:

cat session_vars.php main.php | php

This will prepend the PHP from session_vars.php to main.php without making any changes to main.php, allowing you to keep your code clean.

Comments

1

"Session" here refers to the concept used to work around the fact that HTTP is stateless between requests. PHP sessions operate by stuffing all the data from $_SESSION into some store on the server (defaults to files, but frequently changed around to be databases, memcache, etc.) and then issuing a "session cookie", which contains a magic unique value that the browser can re-present, prompting PHP to read all that data back.

The key point here is that it's typically operated by means of that cookie, and at the very least, by a session identifier. When executing a PHP script from the command line, you don't really have a session, per se. So the question becomes, whose session are you trying to manipulate?

Comments

1

Actually there is a way of doing this under CLI.

For the first request, you need to call session_id() to get the first id and save it somewhere like /tmp folder with a unique name(or save it in database or memcache etc.), for the rest of requests, before you call session_start() in the script, read session id from file and pass it to php.

Check out this sample code below :

if($session_id = @file_get_contents('/tmp/SESSION_ID_xxx.txt')) {
  session_id($session_id);
}
else {
  file_put_contents('/tmp/SESSION_ID_xxx.txt', session_id());
}

session_start();

$_SESSION['var1'] = 'foo'; // this variable will retain the value for all following calls of this script

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.