SoFunction
Updated on 2025-03-02

Detailed explanation of the working principle and examples of php cookies

Works with some instances of read and write operations for cookies.

Cookies and session status

These two concepts are essential when doing BS development. Let’s first understand them. It is difficult to go deeper without actual applications. Look at the reference address in depth!
What are cookies?

A cookie is a small piece of text information that is passed between the web server and the browser along with user requests and pages. Every time a user visits a site, the web application can read the information contained in the cookie. Basic How Cookies Work If a user visits a page on the site again, when the user enters the URL www.******.com, the browser looks for the cookies associated with the URL on the local hard drive. If the cookie exists, the browser sends it to your site along with the page request.

What are the uses of cookies?

The most fundamental purpose is: Cookies can help web sites save information about visitors. To put it more generally, cookies are a way to maintain continuity of web applications (i.e., perform "state management") and make the web site remember you.

•When a customer visits a website based on PHP technology, a cookie can be generated in PHP using the setcookie() function. The system will send the cookie to the client and save it in the C: Documents and Settings username Cookies directory.

• Cookies are part of the HTTP header, so the setcookie() function must be called before anything from the HTML itself is sent to the browser. This limitation is the same as the header() function (if you need to know the header() function, please check it yourself).

•When the customer visits the website again, the browser will automatically send the cookies corresponding to the site in the C: Documents and Settings username Cookies directory to the server, and the server will automatically convert the cookies sent from the client into a PHP variable. In PHP5, the cookies sent by the client will be converted into global variables. You can read it through $_COOKIE['xxx'].

Define a cookie

•Set cookies:

• Syntax: boolsetcookie(stringname,[stringvalue,[int expire,[stringpath,[stringdomain,[int secure]]]]]);

This cookie function can have 6 attributes, and commonly used ones have 3 parameters.

1. Example:

The code is as follows

$value="the best way is by yourself";
setcookie("cookiename",$value,time()+60*60*24*7);

1. Receive and process cookies

PHP has good support for cookies. Like form forms, PHP will automatically receive HTTP headers from the web server and it when receiving them. When receiving, use $_COOKIE["cookiename"] or $HTTP_COOKIE_VARS["cookiename"] (not recommended)

1. Note:

If the website has several different file directories and uses cookies without paths, then this cookie can only be accessed under the path where the file where the cookie is set. If a path is specified, the path at the time is set is used as the specified path to access the cookie.

Create a cookie array:

1. One:

The code is as follows

setcookie("CookieArray[0]","Value1");
setcookie("CookieArray[1]","Value2");

1. Second:

The code is as follows

setcookie("CookieArray['one']","Value1");
setcookie("CookieArray['two']","Value2");

Use arrays in ()

The code is as follows

<?php
setcookie("cookie[three]","cookiethree");
setcookie("cookie[two]","cookietwo");
setcookie("cookie[one]","cookieone");
//After refreshing the page, it will be displayedif(isset($_COOKIE['cookie'])){
foreach($_COOKIE['cookie']as$name=>$value){
echo"$name:$value<br/>n";
}
}
?>

Delete cookies

1. To delete an existing cookie, there are two ways:

1. Call a setcookie with only name parameter, then the cookie named this name will be deleted from the client;
setcookie("MyCookie");//Delete MyCookie

2. Just the time of the issue is enough, then this cookie will be deleted after browsing this page (actually invalid).

For example:

The code is as follows

setcookie(“MyCookie”,”Value”,time()-1);

//Delete MyCookie.

Note: When a cookie is deleted, its value is still valid on the current page. If you want to set the cookie to be invalid after the browser is closed. Then you can set expiretime to 0 directly, or do not set this value.

For example: setcookie("name","value",0).

Things to note about cookies

•1. There cannot be any html output before setcookie(), it is just spaces, and no blank lines can be used. It must be set before the content of the html file is output.

•2. After settingcookie(), you will not output when you call echo$_COOKIE["name"] on the current page. You must refresh or go to the next page to see the cookie value.

•3. The cookies are handled differently without the browser. Clients can disable cookies, and browsers will also limit the number of cookies. The maximum number of cookies a browser can create is 300, and each cannot exceed 4KB. The total number of cookies that can be set per WEB site cannot exceed 20.

•4. Cookies are saved on the client. If the user disables cookies, your cookies will naturally have no effect! Therefore, avoid over-reliance on cookies and think of solutions if cookies are disabled in case.

The above is a compilation of PHP cookie information. Friends in need can refer to it.