Home  >  Article  >  Backend Development  >  How to set and obtain php cookies

How to set and obtain php cookies

(*-*)浩
(*-*)浩Original
2019-10-12 10:31:193701browse

Cookies are stored in the client browser. Cookies are part of the Http header. When requesting a page through the browser, it will be sent in the form of an Http header. The requested page can obtain the cookie value through PHP.

How to set and obtain php cookies

Cookies are related to browsers and domain names. Different browsers store them separately. Cookies will only be sent in the current domain name, and other domain names will not bring cookies to requests.

Cookie settings (Recommended learning: PHP video tutorial)

Grammar:

setcookie( string name,[string value],[int expire],[string path],[string domain]);

Parameter description:

name: cookie name

value: cookie value

expire: expiration time, timestamp format. If not set, the cookie will expire after the browser is closed.

path: Valid path on the server side. The default is the path of the page when the cookie is currently set. '/' means that the entire domain name is valid, and '/A' means that the page under the A category directory is valid. Only the pages included in the set path can get the cookie value

domain: The domain name for which the cookie is valid. Only the specified domain name can get the cookie. By default, all domain names can get it. For example, "www.baidu.com" can also be ".baidu.com".

Example:

setcookie("validCode","value",time()+3600*2,'/','www.baidu.com');
 //cookie名称:validCode
 //cookie值:value
 //过期时间:两个小时后过期
 //可以拿到cookie的页面:所有页面
 //可以拿到cookie的域名:www.baidu.com

Get cookie

$_COOKIE['validCode']

The above is the detailed content of How to set and obtain php cookies. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn