Common solutions for invalid cookies in php development
Source: Internet
Author: User
Common solutions for invalid cookies in php Development common solutions for invalid cookies in php development the cookie has a client and has nothing to do with the server. pay attention to the case sensitivity issue in your program, linux is case-sensitive, and php Tutorial configuration, for example, whether or not it is a common solution for invalid cookies in php development
Common solutions for invalid cookies in php development
The cookie exists on the client and has nothing to do with the server. pay attention to the case sensitivity issue in your program. linux is case sensitive and the php Tutorial configuration is also available, for example, whether the configuration of register_globals = Off is the same on both sides.
The meaning of the @ and # symbol in PHP
Function foo ($ n)
{
$ Result = 1/$ n;
Return $ result;
}
Echo @ foo (0); // The function generates an error except 0, but this error is not displayed after @ is added.
Echo "end"; // output end
# Annotator
Same as //, # is a single line annotator (multiline Annotator is /**/).
Because @ setcookie is used, even if an error occurs when the cookie is written, no output is generated, leading to the failure to detect the problem. Finally, Change @ setcookie to setcookie, and the program outputs the following error message:
Warning: Cannot modify header information-headers already sent by (output started
After checking the internet, I found that there was no output content before setting the setcookie. then I checked the code and did not find anything output before the setcookie. I searched for it, the problem is found. The details are as follows:
Today, I visited the WordPress Chinese forum. The popularity in the jar is not high, but there are still many masters. The experts who will write plug-ins and templates and beginners who cannot even edit files will not be mixed together. this is the case in the forum, haha.
Several posts mentioned the same error, as mentioned in this post: "Warning: Cannot modify header information-headers already sent by (output started at c: program fileseasyphp1-8wwwwp-config.php: 1) in c: program fileseasyphp1-8wwwwp-login.php on line 9"
This is a typical problem. When a WordPress program is executed, it first calls a configuration file such as the wp-config.php, and also calls the wp-db.php to establish a database tutorial connection for later use. These files only make some settings and do not output html code. After the settings are complete, the program itself starts to execute. some programs will use the header command to set an HTTP header. Because the HTTP header must be set before html code output, otherwise the html code has already been sent to the client, and HTTP has already been sent and cannot be recovered. WordPress CodeX explained this problem: How do I solve the Headers already sent warning problem? The article states that it is necessary to ensure that each file, especially the frequently edited wp-config.php file It cannot end with any other character. Specifically to the above example, it is obvious that the prompt information says the first line of the wp-config.php begins the html output, which may be the first line
Solution
The WordPress Chinese forum does not provide the full-text search function, so I can only search for titles. so I used Google to search for Cannot modify header information site: wordpress.org.cn. it seems that many people have encountered this problem. At present, WordPress is mainly used in the original WordPress English version and several WordPress Chinese versions. My Chinese package does not contain wp-config-sample.php files, naturally none of my business; the original WordPress ASCII code, naturally does not contain BOM, will not make such an error; the Chinese version of WordPress made by xigang is available in the WordPress Chinese forum. I downloaded WordPress 2.0.4 and 2.0.3 and checked them. there is no problem. in the Chinese version of WordPress 2.0.4, the wp-config-sample.php file uses GB2312 encoding and DOS line tail, GOD! However, if someone uses notepad to modify this file, the DOS line tail will not cause editing problems, and GB2312 encoding will not cause BOM problems.
Cookie usage
Instructions for deleting cookies begin -----
Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])
To delete a cookie, you must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser.
The following example shows how to delete the cookie you just set:
// Set the Expiration Time to www.3ppt.com one hour ago
Setcookie ("TestCookie", "", time ()-3600 );
Setcookie ("TestCookie", "", time ()-3600 ,"/~ Rasmus/",". utoronto. ca ", 1 );
?>
----- Description about cookie deletion ends -----
The method to delete a cookie is to set the cookie's validity period to before the current time, which is also done by almost all php programmers.
Later, a friend who first came into contact with php told me that he wanted to set the value of a cookie to null in the program. as a result, the cookie was directly deleted. I did not believe it at the time, so I tested it.
Below:
Setcookie ("testcookie ",'');
Print_r ($ _ COOKIE );
The result is that the entire $ _ COOKIE array is empty, rather than the $ _ COOKIE ['testcookie '] is empty. So we captured the packet with winsock, observed the returned http header, and found that the http header was "Set-Cookie: testcookie = deleted; expires = Mon, 18-Jun-2007 02:42:33 GMT ", this indicates that "setcookie (" testcookie ",''); "does delete the cookie directly, which is not described in the php Manual.
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.