Guide to Cookies in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 16:58:25
Original
799 people have browsed it

Overview

Cookies are a way for a server or script to maintain information on a client's workstation under the HTTP protocol. Cookies are small files saved on the user's browser by the Web server, which can contain information about the user (such as identification number, password, how the user purchases on the Web site or the number of times the user visits the site). Whenever a user connects to the server, the Web site can access the cookie information.

How to set cookies?

In PHP, you can use the setcookie function to set a cookie. Cookies are part of the HTTP headers, so the cookie function must be set before any content is sent to the browser. This restriction is the same as the header() function. Any cookie passed from the client will automatically be converted into a PHP variable. PHP obtains the information header and analyzes it, extracts the cookie name and turns it into a variable. Therefore, if you set a cookie such as setcookie("mycookie", "Cookies") php will automatically generate a variable named $mycookie with the value "Cookies".

Let’s take a look at the setcookie function syntax:


init setcookie(string CookieName,string CookieValue,int CookieExpireTime,path,domain,int secure);

Parameter description:

PATH: Represents the directory on the web server. The default is the directory where the called page is located

DOMAIN: The domain name that the cookie can use, the default is the domain name of the called page. This domain name must contain two ".", so if you specify your top-level domain name, you must use ".mydomain.com"

SECURE: If set to "1", it means that cookies can only be remembered by servers that the user's browser considers to be safe.

Examples of cookie use

Suppose we have such a site that requires registration. It automatically identifies the user's identity and performs related operations: if it is a registered user, send him information; if it is not a registered user, it displays a link to the registration page.

According to the above requirements, we first create a database to save registered user information: first name, last name, email address, visit counter.

First, follow the steps below 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)
Then build 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":


  ……
  } else { //If Cookie does not exist
  ……
  }
  ?>

When the cookie exists, we perform the following steps:

First get the cookie value, use the explode function to analyze it into different variables, increase the counter, and set a new cookie:


$info = explode("&", $myCookies);
  ……
  $count ;
​​ $CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;
SetCookie ("myCookies",$CookieString, time() 3600); //Set cookie

Then use html statements to output user information.

Finally, the database is updated with the new counter value.

If this cookie does not exist, we display a link to the registration page (register.php).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631401.htmlTechArticleOverview Cookie is a way for the server or script to maintain information on the client's workstation under the HTTP protocol. Cookies are small files saved on the user's browser by the web server, which can...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!