通过命令提示符执行 PHP5 脚本时是否可以读取 cookie/会话值?

2021-12-27 00:00:00 command command-line php nant

当我使用命令提示符执行我的 php 脚本时,我需要从 cookie 或会话中读取一些值.我怎样才能做到这一点?

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?

如何从 Windows 命令提示符访问 cookie 或会话值?

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

推荐答案

Cookie 是从用户的 Web 浏览器发送的.当您从命令行执行 php 脚本时,没有浏览器可以发送或接收 cookie.无法访问或保存 cookie,除了您在命令行中传递的参数外,不会向脚本发送任何内容.

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.

话虽如此,如果您知道他们的 PHPSESSID cookie,则有一种方法可以读取使用浏览器的人已经访问过的会话.

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.

假设有人使用 Web 浏览器访问了您的脚本,并且他们的 PHPSESSID 是 a1b2c3d4,而您想通过他们的会话执行脚本.在命令行执行以下操作.

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");'

其中 path_to_php_script.php 是您要执行的 php 脚本的路径.实际上,如果要执行的 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");'

好的,现在假设您不想访问某人的会话,但您只想执行脚本,就像您已经有一个会话一样.只需执行上一个命令,但输入您想要的任何 sessionid.只要您每次调用脚本时使用相同的 PHPSESSID,您的会话就会在调用脚本之间保持不变.

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.

相关文章