Contract Killer PHP Cookie Scope – Do not use your cookie on the current page

Source: Internet
Author: User
Because there is no problem in the local test, SSH to the remote server (not the publisher, it is recommended not directly on the publish server directly to change things), a breakpoint test, and finally found that there is a more complex logic in a function called in the constructor of the login verification. No verification passed so it jumped away.
Let's look at the use of PHP cookies through the program, perhaps you read the above paragraph still wonder, this and cookie what is the connection?
Roughly speaking, the login verification used in our MVC structure:
The constructor in the controller uses a function that specifically validates the login, and adds such a function depending on whether the module requires login validation.
Bug Program: After Setcookie, the current page is verified by login.
Bug Reason: The current page is not able to get the cookie just set on this page
Let's use the program to test the following:
Here is a set of cookies function, we now use, lazy oh

Copy the Code code as follows:


function Dsetcookie ($var, $value, $life = 0) {
Global $_cookie_domain, $_cookie_path, $_timestamp, $_server;
Setcookie ($var, Escape ($value), $life? $_timestamp + $life: 0,
$_cookie_path, $_cookie_domain, $_server[' server_port '] = = 443? 1:0);
}


Then set up test1.php, which reads as follows:

Copy the Code code as follows:


$TD = Date (' d ');
Dsetcookie ("TestVar", $TD, 30*24*60*60);
Print_r ($_cookie);
?>


Will get an empty array of arrays ();
So when will we be able to use this $_cookie variable for the first time after setup?
Now let's change the test1.php program a little bit:

Copy the Code code as follows:


$TD = Date (' d ');
Dsetcookie ("TestVar", $TD, 30*24*60*60);
Print_r ($_cookie);//This should be commented out or the header warning will be reported
$location = "test2.php";
Header ("Location:". $location);
?>


And then we'll show $_cookie in the test2.php, look.

Copy the Code code as follows:


Print_r ($_cookie);
?>


Here we are going to get:
Array
(
[TestVar] =10
)
This cookie value can be obtained and used here.
Why is it?
You can understand that the cookie that is currently set does not take effect immediately, but will not be visible until the next page.
This is due to the fact that the cookie is passed to the client's browser on this page of the setting and the next page browser can
The reason that the client's machine was taken back to the server.

Write here first:

You can get a deeper look at the cookie by following the links below

PHP cookies and their use

Netscape Company's official original definition of cookie URL:http://www.netscape.com/newsref/std/cookie_spec.html

The above describes the scope of contract killer PHP cookies – Do not use your cookies on the current page, including contract killer content, and want to be helpful to friends who are interested in PHP tutorials.

  • Related Article

    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.