I have a problem accessing a cookie variable from another php script. 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? 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
.
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:
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);