我从另一个php脚本访问cookie变量时遇到问题。这是的代码片段
if (isset($remember) && $remember == 'on') {
setcookie("username", $user, time() + (60 * 60 * 24 * 30));
setcookie("password", $pass, time() + (60 * 60 * 24 * 30));
}
如何从外部脚本访问cookie内容?感谢
当您从外部脚本发送HTTP请求时,setcookie()
方法将在名为Set-Cookie
的HTTP响应标头后附加一个新标头。
Set-Cookie: username=myUserName
Set-Cookie: password=myUserPass
要读取这些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);