問題描述
當我使用命令提示符執行我的 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.
這篇關于通過命令提示符執行 PHP5 腳本時是否可以讀取 cookie/會話值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!