通过命令提示符执行 PHP5 脚本时是否可以读取 cookie/session 值?
当我使用命令提示符执行 php 脚本时,我需要从 cookie 或会话中读取一些值。我怎样才能做到这一点?
如何从 Windows 命令提示符访问 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?
How to access cookie or session value from Windows command prompt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Cookie 是从用户的网络浏览器发送的。当您从命令行执行 php 脚本时,没有浏览器可以发送或接收 cookie。无法访问或保存 cookie,并且除了您在命令行上传递的参数之外,不会向脚本发送任何内容。
话虽如此,如果您知道某人的 PHPSESSID cookie,则有一种方法可以读取使用浏览器的人已经访问过的会话。
假设有人使用 Web 浏览器访问了您的脚本,并且他们的 PHPSESSID 是 a1b2c3d4,并且您希望使用他们的会话执行该脚本。在命令行执行以下命令。
其中path_to_php_script.php是您要执行的php脚本的路径。实际上,如果您要执行的 php 文件本身启动会话,则不必启动会话。因此,您可能想实际尝试此命令:
好的,现在假设您不想访问某人的会话,但您只想执行脚本,就像您已经有一个会话一样。只需执行前面的命令,但输入您想要的任何 sessionid。只要您每次调用脚本时都使用相同的 PHPSESSID,您的会话将在脚本调用之间保持完整。
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.
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:
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.
CLI 中没有 cookie,所以...是的。
您可以将所需的会话名称作为参数或环境变量传递,然后使用
session_name()
在脚本中设置它。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.您应该尝试以下操作:
然后在您的脚本上执行以下操作:
现在您的会话已准备好使用!
请记住,函数
session_save_path()
将获取在 .ini 文件中设置的“默认”路径。您始终可以使用自定义路径来加载会话。
You should try this:
Then on your script, do this:
And now you have your session ready to be used!
Remember that the function
session_save_path()
will get the "default" path, set in .ini files.You can always use a custom path to load the session.