PHP Cannot read cookies
Do a login interface, automatically remember the user name function
if (Empty ($_post[' Chkremember ')) {
The user does not select the Radio box
if (!empty ($_cookie[' username ')) {
Setcookie (' username ', ', Time ()-100);
}
}else{
User Select a Radio box
Setcookie (' username ', $username, Time () +7*24*3600);
}
When the user has selected a box that remembers the user's name, the cookie has been saved and a cookie file is C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies ([ Email protected][1].txt), the content is also correct
But reading the cookie is unsuccessful, and each time it returns control
function Getcookieval ($key) {
if (Empty ($_cookie[$key])) {
return "None";
}else{
return $_cookie[$key];
}
}
?>
Direct Print COOKIE array print_r ($_cookie);
The resulting empty array is also
What's going on here?
is the cookie save path inconsistent with the read path?
Share to:
------Solution--------------------
1.
Setcookie (' username ', $username, Time () +7*24*3600);
And
Getcookieval ("username")
Must be in a different HTTP session
2. You do not specify a path when setting a cookie
So the cookie is only valid on the path where the Setcookie program is executed.
If the program executing the getcookieval is not under the same path, the value will not be taken
Setcookie (' username ', $username, Time () +7*24*3600, '/');
This allows the cookie variable username to be valid throughout the site.