About PHP in Cookie

1.1 Principle:

      Cookie is stored in the client information packet (a file)

      1. The client sends a request to the server

        2. The value of the server into the response header sent to the client

        3. The browser automatically put information request to the server investment

1.2 Set Cookie

<PHP?
the setcookie ( 'name', 'Tom'); // name = tom into the response header

        After the client has cookei information, each request server, the information will be automatically Cookie request header to the server is placed.

1.3 Gets the value of the Cookie

? <PHP
echo $ _COOKIE [ 'name']; // get the name from the request header is the name of the cookie

          note:     

          After 1, close the browser, cookie disappeared. This cookie is called temporary cookie

          2, cookie information is not shared among different browsers, not cross-browser.

1.4 Permanent Cookie

        Note: After you close the browser cookie value does not disappear

        Syntax: add to the cookie expiration time in a permanent cookie, the expiration time is the time to type timestamp

 

Time = Time $ () + 3600;
the setcookie ( 'name', 'Tom', $ Time); // cookie effective time is 3600 seconds

 

1.5Cookie active directory       

            The default cookie effective in the current directory and subdirectories

          cookie disposed generally valid in the entire station

setcookie ( 'name', 'tom', 0, '/'); // / indicates the root directory

1.6 support subdomains

<PHP?
the setcookie ( 'name', 'Tom', 0, '/', 'baidu.com'); // baidu.com valid in the domain
?>
<A the href = "http://www.bb .baidu.com / bb.php "> Jump </a>

1.7 Is it safe transport           

                Https secure transmission is transmitted.

              http and https can be transferred cookie by default

setcookie ( 'name', 'tom', 0, '/', '', true); // true means that only transmission is https

1.8 Is it safe to visit

                By default, PHP and JS can access cookie

              Secure access: PHP can access, JS is not the default false

<?php
setcookie('name','tom',0,'/','',false,true);   
?>
<a href="/5-demo2.php">跳转</a>

1.9 Delete Cookie

  ? <PHP
// setcookie ( 'name', false); // delete the cookie method
// setcookie ( 'name'); // delete the cookie method two
setcookie ( 'name', 'tom ', time () - 1 ); // delete the cookie method three

                Note: cookie can only save the numbers and strings.

The disadvantage of 1.10cookie         

                  1, because you can see the value of the cookie in the browser, so security is low

              2, since the numbers and only store a string, the poor controllability

              3, because the data transmitted in the request header, the request for increased data load time.

              4, because the data is stored in the browser, the browser stores a suction space is limited, typically 4K.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160062.htm