Solution to cookie unavailability caused by Cookie conflicts with session in ThinkPHP _ PHP Tutorial

Source: Internet
Author: User
The solution to cookie unavailability caused by Cookie conflicts with session in ThinkPHP. When I recently voted for a website, I encountered a conflict between the cookie and session in ThinkPHP that caused the Cookie to be unavailable. The website uses the ThinkPHP framework. The page shows the failure of using cookies due to conflicts between cookies and sessions in ThinkPHP.

The website uses the ThinkPHP framework. many images are displayed on one page, requiring a vote under each image. by limiting IP addresses, visitors are restricted within a certain period of time, multiple images can be voted, but each image can only be voted once. the ip address is stored in the database. later, the session is used for storage, and the ip address is in the service file, however, you need to store the generated sessionid in the local cookie. according to the ThinkPHP manual, the session operation is successful. However, after the cookie is generated, the cookie text cannot be found on the local computer, I thought it was a local computer that restricted the generation of cookies, but there was no restriction on the results of viewing and testing. so I created a php file separately to test the generation of cookies. No problem, the cookie text is found locally, so the following content is found on the Internet. The sequence solves the problem. use the setcookie () function to set the cookie and get the desired result.

The solution is as follows:

Find the configuration file php. ini and find one: Output_buffering changes its value from the original off to on and restarts ApacheOK.

There are also many similar problems:

Sometimes you will find that there is no problem with the file running locally. when testing on the server, the system prompts that there is no local error: Warning: Cannot modify header information-headers already sent...
Obviously, this is because of setcookie. I checked the Internet and explained the following: the cookie itself has some restrictions, for example:

1. the description of the call setcookie must be placed before the tag.
2. you cannot use echo before calling setcookie.
3. the cookie will not appear in the program until the webpage 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, we often encounter "Undefined index", "Cannot modify header information-headers already sent "... To solve the "Cannot modify header information-headers already sent by" error, the method is to delay the data output to the browser before the cookie is generated. therefore, you can add the ob_start () function at the beginning of the program. In this way, the problem can be solved. However, if you want to add ob_start (), it is not feasible. it seems a little depressing that the program has finished writing it! When I found that this error was prompted, I was wondering why I didn't prompt this problem locally. I thought the PHP. ini configuration was different. if I thought it was wrong, it was almost the same. So let's look at the phrase "output started ..." It means that the output has been in another place before setcookie, so find the file followed by output started at, and finally find that the first line is blank.

Bytes. This website uses the ThinkPHP framework, which is displayed on the page...

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.