Summary of example of setting cookie path in php

Source: Internet
Author: User
Tags set cookie setcookie

First look at a function in phpcms

The code is as follows: Copy code

Phpcms/libs/classes/param. class. php
/**
* Set Cookie
* @ Param string $ var
* @ Param string $ value
* @ Param int $ time
* @ Param string $ path
*/

Public static function set_cookie ($ var, $ value = '', $ time = 0, $ path = ''){
$ Time = $ time> 0? $ Time: ($ value = ''? SYS_TIME-3600: 0 );
$ S = $ _ SERVER ['server _ port'] = '000000 '? 1: 0;
$ Var = pc_base: load_config ('system', 'Cookie _ pre'). $ var;
$ Path = ''? Pc_base: load_config ('system', 'Cookie _ path'): $ path;
$ _ COOKIE [$ var] = $ value;
If (is_array ($ value )){
Foreach ($ value as $ k => $ v ){
Setcookie ($ var. '['. $ k. ']', sys_auth ($ v, 'encoding'), $ time, $ path, pc_base: load_config ('system', 'Cookie _ domain '), $ s );
            }
} Else {
Setcookie ($ var, sys_auth ($ value, 'enabled'), $ time, $ path, pc_base: load_config ('system', 'Cookie _ domain '), $ s );
        }
    }
Param: set_cookie ('auth', '', time ()-1 ,'/');

This function is no different from cookie. We will not introduce it first. Many of you may understand it. Let's look at it later.

The fourth parameter of the setcookie function is the cookie path. This article attempts to set the path. The following are two test pages (the path and file directory are directly indicated by url ):

Http: // localhost/test/cookie/father. php

Http: // localhost/test/cookie/child. php

 

The code is as follows: Copy code

<? Php
// Location: locahost/test/cookie/father. php

// Setcookie ("TestCookie", $ value, time () + 3600 ,"/~ Rasmus/"," .example.com ", 1 );
Setcookie ("father_set_1 _/", "@", time () + 24*3600 ,"/");
Setcookie ("father_set_2 _/child", "@", time () + 24*3600, "/child /");
Setcookie ("father_set_3_test/cookie/child", "@", time () + 24*3600, "/test/cookie/child /");

Print_r ($ _ COOKIE );
?>

 

The code is as follows: Copy code

<? Php
// Location: localhost/test/cookie/child. php

// Setcookie ("TestCookie", $ value, time () + 3600 ,"/~ Rasmus/"," .example.com ", 1 );
Setcookie ("child_set_0", "@", time () + 24*3600 );
Setcookie ("maid _/", "@", time () + 24*3600 ,"/");
Setcookie ("child_set_2 _/child", "@", time () + 24*3600, "/child ");
Setcookie ("child_set_3 _/test/cookie/child/", "@", time () + 24*3600, "/test/cookie/child /");

Print_r ($ _ COOKIE );
?>

Access the page father. php-> child. php-> father. php-> child. php in the following order and view the results using firebug:


First, access father. php

As shown in the figure, this request is sent to the server and the $ _ COOKIE array is printed, but the array is empty. Three cookie values are set: "father_set_1 _/", "father_set_2 _/child", and "father_set_3_test/cookie/child ", the corresponding paths are "/", "/child", and "/test/cookie/child". "/" indicates the site root directory.
First request father. in php, the client does not have related cookie records in the initial state. In the preceding figure, only the received cookie is displayed. If no sent cookie exists, no COOKIE is output when the $ _ cookie array is printed.
From the above, we can see that the cookie of each request on the page changes: when the page request is sent, the browser sends the client's existing cookie, the server receives the cookie and saves it to the array $ _ COOKIE array for use by the program; when the cookie set by the server to respond to this request has not been sent to the client, the $ _ COOKIE array is not recorded.
 
The first access to child. php

Access child. php can see that two cookies are sent in the request: "father_set_1 _/" and "father_set_3_test/cookie/child ", four cookies are received: "child_set_0", "child_set_1 _/", "child_set_2 _/child", and "child_set_3 _/test/cookie/child /".
The $ _ COOKIE array printed on the page contains two values: the data sent during the request.
Here is a strange thing. There are only two printed $ _ COOKIE arrays. The first one is father. didn't the php page set three cookie values? Why can't I access "father_set_2 _/child?
It is no surprise to analyze it carefully here. Each time the browser sends a request, it only sends the cookie in the parent path and current path to the server, the $ _ COOKIE array of the server is the cookie sent from each request. This means that a page can only be set as the cookie of the current page path and parent path, the cookie in the sibling path cannot be accessed.
 
Second visit to father. php

The second access to father. php prints two values, that is, the request sends two values, that is, father. php can access two values: "father_set_1 _/" and "child_set_1 _/". The corresponding paths are "/".
Here we can see that although father. php has set three cookies, the browser does not send any cookie from other sub-paths during the request due to the path, so it cannot be read. "Child_set_1 _/" is the cookie set for the page in the sub-path. Because the path is the path of the page, it can be read.
It is worth noting that the value of "child_set_0" cannot be printed during this access. in php, the path "child_set_0" is empty. This is the current path by default, not the site root directory, to set cookies for all sub-paths to allow page access from the parent path, you must set the path.
 
Second access to child. php
When you access child. php again, a total of five values can be printed, but the cookie with the path "/child" cannot be printed. This indicates that the browser only sends the cookie value of the "direct" path relationship.

Well, now you don't know how to set the Coke path. If there is nothing else, we need to set the path with that. If it is a multi-domain name, we can directly set the primary domain name, you can specify a directory in the directory. You can specify a single file path in a single file.

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.