Recently, when I was doing a poll on a website, I encountered a conflict between cookies and sessions in ThinkPHP, which resulted in cookies not being able to be used.
This website is made using the ThinkPHP framework. There is a page that displays many pictures, and each picture is required to have a corresponding vote. By restricting the IP address, visitors are restricted from voting on multiple pictures within a certain time range, but Each picture can only be voted once. At first, the IP was stored in the database to solve the problem. Later, it was improved to use session storage. The IP is in the service file. However, the generated sessionid must be stored in a local cookie. Follow the instructions in the ThinkPHP manual to operate the session and everything goes smoothly. , but after generating the cookie, the cookie text could not be found in the local computer. I originally thought that the local computer restricted the generation of cookies, but the results of viewing and testing showed that there was no restriction, so I created a separate php file to test the generation. Cookie, no problem, I found the cookie text locally, so I found the following content online. The problem was solved in sequence, using the setcookie() function to set the cookie, and got the desired result.
The solution is as follows:
Find the php.ini configuration file, and then look for an item: output_buffering, change its value from off to on, and restart Apache and it will be ok.
There are many similar problems:
Sometimes you will find that files that originally ran without problems locally, when tested on the server, actually prompt an error that does not appear locally: Warning: Cannot modify header information - headers already sent by ...
Such a statement, obviously, the reason for this is caused by setcookie. After checking the Internet, there is the following explanation: The cookie itself has some restrictions on the use, such as:
1. The description of calling setcookie must be placed Before the tag
2. Before calling setcookie, echo cannot be used
3. The cookie will not appear in the program until the web page is reloaded
4. The setcookie function must be sent before any data is output to the browser
Based on the above restrictions, when executing the setcookie() function, you often encounter problems such as "Undefined index", "Cannot modify header information - headers already sent by"... etc. Solve the error "Cannot modify header information - headers already sent by" The method is to delay the output of data to the browser before generating the cookie. Therefore, you can add the ob_start() function at the front of the program. This way the problem can be solved. But if you want to add ob_start(), it is not very feasible. The program has been written, so it seems a bit depressing to change this! When I found out that this error was prompted, I was wondering why my local computer didn't prompt this problem. I thought it was because the PHP.ini configuration was different, but when I thought about it, it was wrong. They were all the same. So I looked at the sentence "output started at..." that followed, which meant that there was output in another place before setcookie, so I found the file that followed output started at, and finally found that the first line was blank.
For more related articles on solutions to the problem of cookie and session conflicts in ThinkPHP causing cookies to be unusable, please pay attention to the PHP Chinese website!