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 (instead of the Publishing Server, we recommend that you do not directly modify the content on the Publish Server) and perform a breakpoint test, finally, it is found that a complicated logic contains a function that calls login verification in the constructor. The verification fails.
Next we will look at the use of php cookies through the program. Maybe you are still wondering after reading the above section. What is the connection between this and cookie?
In general, the login verification in our MVC structure is as follows:
The constructor In the controller uses a dedicated login verification function to add such a function based on whether the module needs login verification.
Bug program: After setcookie, the current page has been logged on for verification.
Cause of bug: the current page cannot obtain the cookie set on this page.
Let's test it with a program:
Here is a cookie setting function. We are using it now and we are lazy.
Copy codeThe Code is 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 );
}

Create test1.php with the following content:
Copy codeThe Code is as follows:
<? Php
$ Td = date ('D ');
Dsetcookie ("testvar", $ td, 30*24*60*60 );
Print_r ($ _ COOKIE );
?>

An empty array () is obtained ();
When can we use this $ _ COOKIE variable for the first time after setting?
Now let's make a slight change to the test1.php program:
Copy codeThe Code is as follows:
<? Php
$ Td = date ('D ');
Dsetcookie ("testvar", $ td, 30*24*60*60 );
Print_r ($ _ COOKIE); // You Need To comment this out. Otherwise, a header warning will be reported.
$ Location = "test2.php ";
Header ("Location:". $ location );
?>

Then let's show $ _ COOKIE in test2.php.
Copy codeThe Code is as follows:
<? Php
Print_r ($ _ COOKIE );
?>

Here we will get:
Array
(
[Testvar] = 10
)
The cookie value can be obtained and used here.
Why?
As you can understand, the current Cookie does not take effect immediately, but will not be visible until the next page.
This is because the Cookie is transmitted by the server to the client browser on the configured page.
The reason why the customer's machine is taken back to the server.

Write it here:

You can go to the following links to learn more about cookies.

Php cookie and Its Usage

Netscape official original Cookie definition Website:Http://www.netscape.com/newsref/std/cookie_spec.html

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.