thinkphp cookies and Session conflicts create a workaround for cookies _php instances

Source: Internet
Author: User
Tags setcookie

In a recent poll of a website, a cookie in thinkphp and a session conflict caused the cookie to be out of use.

The site is made from the thinkphp framework, there is a page above show a lot of pictures, require each picture below have a corresponding vote, by restricting the IP address to restrict visitors to a certain time range, can vote on multiple pictures, but each picture can only vote once, start using IP storage in the database solution, Later improved to use session storage, IP in the service file, but to store the generated SessionID in a local cookie, follow the instructions in the thinkphp manual all goes well, but after the cookie is generated, But the local computer can not find the cookie text, originally thought to be the local computer to limit the generation of cookies, but the results of the view and test is unlimited, so they set up a separate PHP file, test generation cookies, no problem, found in the local cookie text, So on the Internet to find the following content. The order solves the problem by using the Setcookie () function to set the cookie and get the desired result.

The solution is as follows:

Locate php.ini this profile, and then look for one:output_buffering change its value from the original off to on, restart ApacheIt's OK.

There are also a number of similar problems:

Sometimes you will find that the original local run no problem files, testing on the server, unexpectedly prompts local error: Warning:cannot Modify header information-headers already sent by ...
Such a statement, it is obvious that this reason is caused by the Setcookie, looked up the internet, like the explanation: the cookie itself in the use of some restrictions, such as:

1. The description of the call Setcookie must be placed before the label
2. Do not use echo before calling Setcookie
3. Cookies will not appear in the program until the Web page is loaded again
4.setcookie functions must be sent out before any data is exported to the browser

Based on these limitations, the execution of the Setcookie () function often encounters "Undefined index", "Cannot modify header information-headers already by" ... And so on, the solution to the "cannot modify header Information-headers already sent by" method is to delay the data output to the browser before the cookie is generated, so you can add ob_ to the front of the program The start () function. So the problem can be solved. But if you want to add Ob_start (), there is not feasible, the procedures are written, just to change this seems a bit depressing! Found that prompted this error, I was thinking about how the local did not prompt this question, but also thought that the php.ini configuration is not the same, think again is not the same, are similar. Then look at the following sentence "output started at ..." means that before the Setcookie already has output in another place, then found the output started at the back of the file, and finally found that the first line is blank.

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.