How to Read Cookie/Session Value While Executing PHP5 Script Through Command Prompt

Is it possible to read cookie/session value while executing PHP5 script through command prompt?

Cookies are sent from the user's web browser. When you execute a php script from the command line, there is no browser to send or receive cookies. There is no way to access or save cookies and nothing is sent to the script except the parameters you pass on the command line.

That being said, there is a way to read a session that someone with a browser has already accessed if you know their PHPSESSID cookie.

Let's say someone has accessed your script with a web browser and their PHPSESSID is a1b2c3d4, and you want to execute the script with their session. Execute the following at the command line.

php -r '$_COOKIE["PHPSESSID"] = "a1b2c3d4"; session_start(); require("path_to_php_script.php");'

Where path_to_php_script.php is the path to the php script you want to execute. And actually, you shouldn't have to start the session if the php file you want to execute starts the session itself. So, you may want to actually try this command:

php -r '$_COOKIE["PHPSESSID"] = "a1b2c3d4"; require("path_to_php_script.php");'

OK, now let's say you don't want to access someone's session, but you just want to execute the script as if you already had a session. Just execute the previous command, but put in any sessionid you want. And your session will remain intact between calls to the script as long as you use the same PHPSESSID every time you call the script.

Running script from console sotres cookies

Cookies is a browser feature. You cannot use it in console mode. (You can try to implement it yourself, please don't).

PHP script from command prompt - is it possible to set \ read cookies?

Cookies only exist in a browser. There's no browser involved when you run a script from the command line, so there's nowhere for those cookies to be stored.

If you need to store data across calls to a command-line script, you will need to read and write it to a file. One simple way of doing this might be:

// At the start of the script...
$PERSIST = unserialize(file_get_contents("myscript.cache")) ?: array();

// Read data from there...
print $PERSIST["blah"];
// Write some
$PERSIST["foo"] = "stuff";

// At the end of the script, save it back
file_put_contents("myscript.cache", serialize($PERSIST));

Can I continue existing session on script in background and modigy the values?

What you could try is to pretent a session cookie exists, like this:

WebInterface.php

$_SESSION["flag"] = false;
shell_exec("php BackgroundScript.php ".session_id()." &");

BackgroundScript.php

$id = $argv[1];
$_COOKIE["PHPSESSID"] = $id;
session_start();
//do things
$_SESSION["flag"] = true;

This can work under some circumstances, but also might not.

See: https://stackoverflow.com/a/7578766/3986005

I think a better solution would be to communicate through a database or file.

php session doesn't work in cli

You have to search and read online articles before asking question in stackoverflow.
there are even same questions on stack.

yes thats true , PHP sessions are mainly created to be used with cgi SAPI, and as I know there is no use for php sessions with CLI SAPI.
and the reason is simple ,
http is stateless and we use cookie and session to remember user on server and track his/her actions.
but in console , why you need session at all???

consider that when running php in console there is no browser thhere is no http request, so there is no http header, so there is no http COOKIE header .

if you need a storage to be available to different php processes which are (CLI SAPI)
you can use database for this purpose.
but still if you insist on using php session for any reason,
take a look at here :

Is it possible to read cookie/session value while executing PHP5 script through command prompt?

Passing variables to bash and php

You should escape all $ signs (except $1 and $2).

php -r "\$_COOKIE['PHPSESSID']='a095y187'; session_start(); \$id_client=$1; \$id_supplier=$2; \$_COOKIE['id_chosen_client_auto']=\$id_client; \$_COOKIE['id_chosen_client']=\$id_client; require 'manual_automap.php';"

PHP session_start function and CLI

Your problem is, apart from that it makes no sense to use sessions in CLI, that output has already started prior to session_start();.

As I see in your code, you code begins directly with session_start();, I believe you have some characters before <?php. Make sure <?php is on the very first line of your file (so also no empty lines above it), and that there is nothing (such as a white space) in front of it.

This should fix this problem you are having.



Related Topics



Leave a reply



Submit