Home > Backend Development > PHP Tutorial > ThinkPHP's cookie and session conflict causes the cookie to be unusable - PHP tutorial

ThinkPHP's cookie and session conflict causes the cookie to be unusable - PHP tutorial

WBOY
Release: 2016-07-13 10:26:26
Original
954 people have browsed it

When I was voting on a website recently, 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 The picture can only be voted once. It was initially solved by using IP to store it in the database. 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 well. However, After the cookie was generated, 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 limit, so I created a separate php file to test the generated cookie. No problem, the cookie text was found 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 search for an item: output_buffering, change its value from off to on, and restart Apache and it will be ok.

There are many other similar questions:

Sometimes you will find that a file that originally ran without problems locally will prompt an error that does not appear locally when tested on the server: Warning: Cannot modify header information - headers already sent by…
For such a statement, it is obvious that the reason for this is caused by setcookie. After checking the Internet, there is the following explanation: The cookie itself has some restrictions on its use, such as:

1. The description of calling setcookie must be placed before the tag
2. Echo
cannot be used before calling setcookie. 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 data output 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824685.htmlTechArticleRecently 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. The website is made using the ThinkPHP framework, and there is a page showing...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template