[英]Accessing PHP cookie from another php script
I have a problem accessing a cookie variable from another php script. 我从另一个PHP脚本访问Cookie变量时遇到问题。 Here's the code snippet
这是代码片段
if (isset($remember) && $remember == 'on') {
setcookie("username", $user, time() + (60 * 60 * 24 * 30));
setcookie("password", $pass, time() + (60 * 60 * 24 * 30));
}
How can I access the cookie content from an external script? 如何从外部脚本访问cookie内容? Thanks
谢谢
When you send HTTP request from external script, The setcookie()
method will append a new header to the HTTP response header called Set-Cookie
. 当您从外部脚本发送HTTP请求时,
setcookie()
方法会将新的标头附加到名为Set-Cookie
的HTTP响应标头中。
Set-Cookie: username=myUserName
Set-Cookie: password=myUserPass
To read those cookies (actually all we need is to parse HTTP header from the response) use something like this: 要读取这些cookie(实际上,我们所需要做的只是从响应中解析HTTP标头),使用类似以下内容的方法:
file_get_contents("http://localhost:8080/test.php");
$receivedCookies = array();
$headerCount = count($http_response_header);
for($i = 0; $i < $headerCount; $i++){
if(strpos($http_response_header[$i], "Set-Cookie") !== false){
$receivedCookies[] = $http_response_header[$i];
}
}
var_dump($receivedCookies);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.