PHP Cannot read cookies

Source: Internet
Author: User
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 in C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\ There is a cookie file (Administrator@localhost[1].txt) Under the Cookies menu, and the content is 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?


Reply to discussion (solution)

Should be returned every time empty value, I typo.

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.

Thank you, Hero, saved!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.