Home >Backend Development >PHP Problem >How to use php cookies

How to use php cookies

王林
王林Original
2019-10-15 17:54:322507browse

How to use php cookies

Use of Cookies in PHP---Add/Update/Delete/Get Cookies and automatically fill in the user's username and password and determine whether it is the first time to log in

What is a cookie?

The server saves user information on the client, such as login name, password, etc.

These data are like cookies. The amount of data is not large. The server can read it from the client when needed and save it in the client's browser cache directory.

1. When the browser accesses cookie.php, the server will send an http response with Set-Cookie:name=zxf;expire=Wed, 21-Sep-2017 20:14 GMT. When the browser After getting the message, the cookie information will be saved to the local disk.

2. If we don’t have time (the third parameter)

The cookie will not be saved to the client. When the browser session ends, the cookie will expire

3. Cookie What is saved is string information

4. The client can save multiple keys=>val

5. During the cookie saving process, Chinese will be urlencoded, and the cookie can have multiple keys. Each key=>val can specify different validity times for different key values.

The code is as follows:

<?php
//添加cookie
setcookie("name","zxf",time()+3600);
//数组
 
/$arr = array(1,2,3); 
 $arr_str = serialize($arr); 
 setcookie("a",$arr_str,time()+3600); 
 
//获取cookie
  
var_dump($_COOKIE);
 
//更新cookie
 
setcookie("name","aaa",time()+3600);
 
//删除cookie
 
setcookie("name","",time()-20);
 
//删除所有
 
foreach ($_COOKIE as $key => $value) {
 setcookie($key,"",time()-1);
 }
echo "成功";
 ?>

If the key=>val of the cookie you delete has not been deleted, the cookie will be retained on the client. If you delete the cookie of this website, If all cookies are deleted, the browser will delete the cookie file.

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to use 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