PHP is used in Cookie

What is a Cookie?

Cookie stored in the client browser, cookie is a part of Http head, when a browser requests a page, it will be sent in the past in the form of Http head. Requested page, you can get the value of the cookie by PHP.
Cookie and browser and domain name related to different browsers each store, cookie will only be sent in the current domain name, the domain name will not bring the other cookie to the request.

Set Cookie:

  • 语法:
    bool setcookie( string name,[string value],[int expire],[string path],[string domain]);

* Parameters:
name: the cookie name (required)
value: the value of the cookie (Optional)
The expire: the expiration time, timestamp format (optional). The default is not set session-level cookie, cookie when the browser is closed failure.
path: the path active server (optional). The default is the path of the current page settings cookie, '/' represents the entire domain effectively, '/ A' represents a page under Categories A valid. Only pages containing the path set before they can get the value of the cookie
domain: The cookie valid domain name (optional), only the specified domain name before they can get the cookie, the default for all domain names can get. Such as, "www.php.com", but also ".php.com".

  • 实例:
    setcookie('validCode','value',time()+3600*12,'/','www.test.com');

cookie name: validCode

cookie值:value

Expiration time: 12 hours expired

You can get the cookie page: all pages

You can get the cookie domain: www.test.com

Photo resolution:

$cookieValue = 'value';
$flag = setcookie('validCode', $cookieValue,time()+60,'/');

First set, Set-Cookie response header has been named validCode of the information, indicating that the cookie has been stored in the cookie directory of the hard drive inside the browser

At this time, the request header which carries not change Cookie, the next request will be put on.

Set after the completion of the requested page will carry the cookie :( If you limit the path or domain name, the cookie will only carry the specified page to request)

Get Cookie:

$cookieValue ='';

if (isset ($ _ COOKIE [ 'validCode'])) // set first determines whether the Cookie
{
$ cookieValue _COOKIE = $ [ 'validCode'];
}

Destruction Cookie:

To a previous point in time can be destroyed by setting the cookie expiration time:
setcookie ( "the User", "", Time () - 3600);

Cookie Note:

1, setcookie) before (can not have any html output is blank, a blank row will not do.

2, setcookie (), you call echo $ _COOKIE [ "name"] there will be no output in the current page. You must be refreshed or to the next page before they can see the value before the Cookie expires.

3, because the cookie information stored on the user's computer, it would be possible to forge or modify the resulting Cookie Cookie deception, generally can be encrypted cookie value to prevent cheating. When read Cookie, Cookie for decryption can be.

4、Cookie是保存在客户端的,用户禁用了Cookie,你的Cookie自然也就没作用啦!

Guess you like

Origin www.cnblogs.com/caominjie/p/10932731.html