PHP Cookie tutorial details

  • 2020-06-07 04:03:27
  • OfStack

1. Set Cookie
PHP USES the SetCookie function to set Cookie. It is important to note that Cookie is part 1 of the HTTP protocol header for passing information between the browser and the server, so the Cookie function must be called before any content output belonging to the HTML file itself.
The SetCookie function defines an Cookie and appends it to the HTTP header. The PROTOTYPE for the SetCookie function is as follows:

int SetCookie(string name, string value, int expire, string path, string domain, int secure);
All parameters except name are optional. value,path,domain3 parameters can be replaced with an empty string to indicate that they are not set; The expire and secure parameters are numeric and can be represented by 0. The expire parameter is a standard Unix time marker that can be obtained in seconds using the time() or mktime() functions.
The secure parameter indicates whether this Cookie is transmitted over the network via the encrypted HTTPS protocol.

The current setting of Cookie does not take effect immediately and will not be seen until the next page, because Cookie is passed from the server to the client browser on this set page, and the browser on the next page will not be able to fetch Cookie from the client machine back to the server.
Setting Cookie on the same page actually works backwards, so to delete a new Cookie before inserting one, you must first write the inserted statement and then the deleted one, otherwise you may get unwanted results.
Here are a few examples:
SetCookie("MyCookie", "Value of MyCookie");
With expiry time:
SetCookie("WithExpire", "Expire in 1 hour", time()+3600); //3600 seconds =1 hour
SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", "", 1);

There is one more point to make. For example, if you have several different directories on your site, if you use Cookie without a path, Cookie in one directory will not be visible in another directory, that is, Cookie is path-oriented. In fact, even if no path is specified, the WEB server automatically passes the current path to the browser. The specified path forces the server to use the set path. The solution to this problem is to add the path and domain name to the call to SetCookie, which can be "" or " ".
The part of the SetCookie function that represents value is automatically passed by encode, that is, if the value of value is "test value" it becomes "test%20value" when passed, just like method 1 of URL. Of course, this is transparent to the program, because PHP automatically puts decode when it receives the value of Cookie.

To set multiple Cookie with the same name, use an array by:
SetCookie("CookieArray[]", "Value 1");
SetCookie("CookieArray[]", "Value 2");
SetCookie("CookieArray[0]", "Value 1");
SetCookie("CookieArray[1]", "Value 2");

2. Receive and process Cookie
PHP's support for Cookie's reception and handling is very good, is completely automatic, like the principle of FORM variables, and is extremely simple.
For example, if you set an Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $myCookie, which is the value of Cookie. Arrays also work. Another option is to refer to the PHP global variable $HTTP_COOKIE_VARS array.
Here are some examples :(suppose these were set in the previous page and still work)
echo $MyCookie;
echo $CookieArray[0];
echo count($CookieArray);
echo $HTTP_COOKIE_VARS["MyCookie"];
It's that simple.

3. Delete Cookie
There are two ways to delete an existing Cookie:

1 is to call SetCookie with the name argument only, then the Cookie named this name will be deleted from the relationship machine;
Another option is to set the expiration time of Cookie to time() or time()-1, so that the Cookie is deleted after the page is viewed.
Note that when an Cookie is deleted, its value is still valid on the current page.

4. Restrictions on Cookie
First, it must be set before the contents of the HTML file are output;
Second, different browsers do not handle Cookie well, and sometimes the results are wrong. For example: MS IE+SERVICE PACK 1 cannot correctly handle Cookie with domain name and path, Netscape Communicator 4.05 and MS IE 3.0 cannot correctly handle Cookie without path and time. As for MS IE 5, it does not seem to work with Cookie with domain name, path, and time. I found this when I was designing the pages of this site.
The third limitation is on the client side.

A maximum of 30 Cookie can be created per browser and no more than 4KB can be created per browser, and no more than 20 Cookie can be set per WEB site.

Related articles: