Home > Backend Development > PHP Tutorial > Basic use of cookies in php

Basic use of cookies in php

autoload
Release: 2023-04-09 20:06:01
Original
4095 people have browsed it

1. Cookie Introduction

Cookie is a mechanism that stores data on the remote browser side and uses it to track and identify users. Cookie is completely maintained on the client, such as: IE firefox When the client disables cookie, it will no longer be used.

2.Cookie configuration and application

Setcookie(string name, string value, int expire,string path, string domain, int secure);
Copy after login
  • name is the cookie variable name identifier. You can use it to reference cookie variables in PHP just like using ordinary variable names; (required)

  • value is cookie The initial value of the variable; (required)

  • expire Indicates the validity time of the cookie variable; (optional.)

  • path The relative path of the cookie variable; (Optional.)

  • domain Represents the website of the cookie variable; (Optional .)

  • secure is valid only when https is securely transmitted. (Optional.)

3. Receive and process cookies

<?php
$value = "my cookie value";// 发送一个简单的 cookie
setcookie("TestCookie",$value);
?>
...
<html>
<body>
Copy after login
echo $TestCookie;
echo $CookieArray[0];
echo $_COOKIE["TestCookie"]; 
echo $HTTP_COOKIE_VARS["TestCookie"];
Copy after login

4. Delete cookies

To delete an existing Cookie, there are two ways:

  • SetCookie("TestCookie", "");

  • SetCookie("TestCookie", "value" , ​​time()-1 / time() );

##5. Restrictions on using cookies

  • Must be set before the content of the

    HTML file is output;

  • Different browsers treat

    Cookie's handling is inconsistent and sometimes produces erroneous results. The limit is on the client side.

  • The maximum number of

    Cookie that can be created by a browser is 30, and each cannot exceed 4KB. Each WEB site can set The total number of cookies cannot exceed 20.

Recommended:

php video tutorial

The above is the detailed content of Basic use of cookies in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template