Php cannot read cookies

Source: Internet
Author: User
Php cannot read cookies and makes a logon interface. it automatically remembers the username function if (empty ($ _ POST ['chkremember']) & nbsp ;{& nbsp; & nbsp; & nbsp; // The user does not select a ticket & nbsp; php cannot read cookies.
Create a login interface to automatically remember the user name

If (empty ($ _ POST ['chkremember']) {
// The user does not select a single region
If (! Empty ($ _ COOKIE ['username']) {
Setcookie ('username', '', time ()-100 );
}
} Else {
// Select a ticket
Setcookie ('username', $ username, time () + 7*24*3600 );
}

When a user selects a single token that remembers the user name, the COOKIE has been saved. a COOKIE file (administrator@localhost=12.16.txt) is added to the C: \ Users \ Administrator \ AppData \ Roaming \ Microsoft \ Windows \ cookiesmenu, and the content is correct.

However, it fails to read the COOKIE.
'/>

Function GetCookieVal ($ key ){
If (empty ($ _ COOKIE [$ key]) {
Return "none ";
} Else {
Return $ _ COOKIE [$ key];
}
}
?>

Print the COOKIE array print_r ($ _ COOKIE) directly );
The result is also an empty array.


Why is this?
Is the COOKIE storage path inconsistent with the read path?

Share:
------ Solution --------------------
1,
Setcookie ('username', $ username, time () + 7*24*3600 );
And
GetCookieVal ("username ")
It must be performed in different HTTP sessions

2. You did not specify the path when setting the cookie.
Therefore, the cookie is valid only in the path of the program that executes setcookie.
If the program executing GetCookieVal is not in the same path, the value is not obtained.

Setcookie ('username', $ username, time () + 7*24*3600 ,'/');
In this way, the cookie variable username can be valid throughout the website.

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.