PHP Setcookie Set cookie usage (and set invalid problem) _php tips

Source: Internet
Author: User
Tags set cookie setcookie
The result encountered a problem, Setcookie set the cookie did not take effect, in the browser side also did not see. Check it out, it turns out that Setcookie is done through the header of the HTTP request response, and needs to be executed before the response content output is requested (just like other header settings).

In the case of error_reporting = E_all in php.ini, the following prompts will pop up after the output Setcookie:
Copy Code code as follows:

Warning:cannot Modify header Information–headers already sent by (output started at c:\xampp\htdocs\b.php:2) in ... on Li Ne ...

But because at that time php.ini set to error_reporting = E_all & ~e_notice, so there is no hint, so the development of the proposal set to Error_reporting = E_all easy to observe some anomalies.

Attach an example of Setcookie usage
a.php
Copy Code code as follows:

<?php
Setcookie ("page", "a.php");
$page = $_cookie["page"]? $_cookie["page"]: "Unknown";
echo "from". $page. "<br/><br/>";
?>
This is a.php. Go to <a href= "b.php" >b.php</a>

b.php
Copy Code code as follows:

<?php
Setcookie ("page", "b.php");
$page = isset ($_cookie["page")? $_cookie["page"]: "Unknown";
echo "from". $page. "<br/><br/>";
?>
This is b.php. Go to <a href= "a.php" >a.php</a>

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.