How to set PHP session variable via linux command line?

How to set PHP session variable via linux command line?

Explanation

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

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

Is there a way to set the same variable via the PHP command line?

+4
source share
4 answers

The PHP session handler by default stores session data in serialize() format in a file, which means that it is mostly textual. You can, of course, manipulate the file from the command line using any of the standard UNIX text tools (Perl, Sed, AWK, even an echo / cat in a shell script, etc ...), as long as you don’t "t insert syntax error into serialized data.

But at that moment, if you do not find a function / library / module that does unserialize() and most likely serialize() , as well, you can also just do the PHP manipulations yourself. This would be a fairly rare system that does not have a CLI version for PHP installed next to the web server 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); 
+6
source

I processed the debugging code that uses the $ _SESSION variables, setting the corresponding variables to a separate file (i.e. session_vars.php ), and then doing the following:

 cat session_vars.php main.php | php 

This will add PHP from session_vars.php to main.php without making any changes to main.php , which allows you to keep your code clean.

+2
source

The β€œsession” here refers to the concept used to circumvent the fact that HTTP does not have statelessness between requests. PHP sessions work by gathering all the data from $_SESSION to some repositories on the server (by default files, but often change around like databases, memcache, etc.), and then give out a "session cookie" that contains a magical unique value. which the browser can reimagine, suggesting PHP to read all this data.

The key point here is that it is usually managed with this cookie and at least with a session id. When you run the PHP script from the command line, you really don't have a session as such. So the question is, whose session are you trying to manipulate?

+1
source

There is actually a way to do this in the CLI.

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

Check out this code example 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 
+1
source

Source: https://habr.com/ru/post/1333962/


All Articles