Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to read some values from cookie or session when I am executing my php script using command prompt. How can I do that?

How to access cookie or session value from Windows command prompt?

like image 689
learner Avatar asked Sep 28 '11 05:09

learner


People also ask

How to retrieve cookies value in PHP?

Accessing Cookies with PHP Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will access all the cookies set in above example. You can use isset() function to check if a cookie is set or not.

What are cookies explain the cookies handling in PHP with proper example?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.


2 Answers

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.

like image 111
Ray Perea Avatar answered Oct 13 '22 13:10

Ray Perea


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.

like image 36
Ignacio Vazquez-Abrams Avatar answered Oct 13 '22 13:10

Ignacio Vazquez-Abrams