Summary ?? Cookie is a method in which a server or script can protect information on a customer's workstation over HTTP. A Cookie is a small file that is retained by the Web server in the user's browser. it can contain information about users (such as identity authentication codes,
Summary
?? Cookie is a method in which a server or script can protect information on a customer's workstation over HTTP. Cookie is a small file retained by the Web server in the user's browser, it can contain information about the user (such as the ID code, password, the user's method of shopping on the Web site or the number of times the user visits the site ). The Web site can visit Cookie information whenever a user links to the server.
How to set cookies?
?? You can use the setcookie function in PHP to set a cookie. Cookie is part of the HTTP header. Therefore, you must set the cookie effect before any content is sent to the browser. This restriction is the same as that of the header () function. Any cookie sent from the client will be automatically converted into a PHP variable. PHP retrieves and analyzes the information header, extracts the cookie name, and changes it to a variable. Therefore, if you set a cookie such as setcookie ('mycookie ', 'Cookies'), php will generate a variable named $ mycookie with the value of 'cookies.
?? Let's take a look at the setcookie function syntax:
?? Init setcookie (string CookieName, string CookieValue, int CookieExpireTime, path, domain, int secure );
?? Parameter clarification:
?? PATH: displays the directory on the web server. the default PATH is the Directory of the called page.
?? DOMAIN: The DOMAIN name that the cookie can apply. by default, the DOMAIN name of the called page is used. This domain name must contain two '.', so if you specify your top-level domain name, you must use '.mydomain.com'
?? SECURE: if it is set to '1', the cookie can only be remembered by the user's browser as a SECURE server.
Cookie application example
?? Suppose we have a website to be registered, which actively identifies the user's identity and carries out relevant control: if it is a registered user, it sends it to him; if you are not a registered user, a link to the registration page is displayed.
?? According to the above request, we first create a database to retain the information of the registered user: first name, last name, and Email address ), counter (visit counter ).
?? Follow these steps to create a table:
Mysql> create database users;
???? Query OK, 1 row affected (0.06 sec)
???? Mysql> use users;
???? Database changed
???? Mysql> create table info (FirstName varchar (20), LastName varchar (40), email varchar (40), count varchar (3 ));
???? Query OK, 0 rows affected (0.05 sec)
?? Create a php page to check the cookies against the database.
?? Since php can convert identifiable cookies into corresponding variables, we can check a variable named 'mycookies:
<? If (isset ($ myCookies) {// if the Cookie already exists
????......
????} Else {// if the Cookie does not exist
????......
????}
???>
?? When a cookie exists, perform the following steps:
?? First, obtain the cookie value, use the explode function to analyze different variables, increase the counter, and set a new cookie:
$ Info = explode ('&', $ myCookies );
????......
???? $ Count;