Solution to the conflict between cookie and session of ThinkPHP causing Cookie to be unusable

  • 2021-07-06 10:21:56
  • OfStack

When voting for a website recently, I encountered the conflict between cookie and session in ThinkPHP, which led to the inability to use Cookie.

The website is produced by ThinkPHP framework, There is a page with many pictures on it. Ask for a corresponding vote under each picture. By limiting the ip address, Restrict tourists within a fixed time range, Can vote on multiple pictures, But each picture can only be voted once. Start using ip storage in the database to solve, It was later improved to use session storage, ip in the service file, However, store the generated sessionid in the local cookie, According to the instructions of ThinkPHP manual, the operation of session1 is smooth, but after generating cookie, the text of cookie can't be found in the local computer. Originally, it was thought that the local computer restricted the generation of cookie, but the results of viewing and testing were no restrictions, so I set up an php file separately, and tested to generate cookie. No problem, I found the text of cookie locally, so I found the following contents on the Internet. Sequence solves the problem, using the setcookie () function to set cookie and get the desired result.

The solution is as follows:

Find the configuration file php. ini, and then look for one item: output_buffering change its value from off to on, and restart Apache to ok.

There are also many similar problems:

Sometimes you will find that there is no problem with the original file running locally, and when you test it on the server, you will be prompted with an error that does not appear locally: Warning: Cannot modify header information-headers already sent by …
Such a statement, obviously, causes this reason because of setcookie, checked 1 under the Internet, there are the following explanations: cookie itself has 1 restrictions in use, for example:

1. The description of calling setcookie must be placed before the label
2. You cannot use echo until you call setcookie
3. cookie does not appear in the program until the page is reloaded
4. The setcookie function must send any data before it is output to the browser

Because of the above limitations, when executing setcookie () function, we often encounter problems such as "Undefined index", "Cannot modify header information-headers already sent by", etc. The solution to the error of "Cannot modify header information headers already sent by" is to delay the data output to the browser before generating cookie, so you can add ob_start () at the front of the program. In this way, the problem can be solved. But if you want to add ob_start (), it is not feasible, and the program is finished, so it seems a bit depressing to change this! When I found this error, I was thinking about why I didn't prompt this problem locally. I thought it was PHP. ini with different configurations, but it was wrong to think about it. They were all the same. So look at the following sentence "output started at …", which means that there is output in another place before setcookie, so find the file followed by output started at, and finally find that the first line is blank.

Related articles: