Is it possible to read cookie/session value while executing PHP5 script through command prompt?
Solution 1:
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.
Solution 2:
There are no cookies in the CLI, so... yeah.
You can pass the desired session name as an argument or environment variable and then use session_name()
to set it in your script.