Cookie in PHP

Cookies are text files stored on the Client and they are kept for the purpose of tracking. PHP supports HTTP Cookies.

Cookies are text files stored on the Client and they are kept for the purpose of tracking. PHP supports HTTP Cookies.

There are 3 steps to identify user returns:

  1. Server-side script sends a set of cookies to the browser. For example: name, age, .
  2. The browser stores this information on the local machine for future use.
  3. The next time the browser sends any requests to the Web Server, it sends the cookie information to the Server and the Server will use that information to identify this user.

This article will show you how to set cookies, how to access them and how to delete them.

The structure of a cookie

Cookies are usually set in the HTTP header (although JavaScript can also set a cookie directly on a browser). A PHP script setting a cookie can send headers that look like this:

 HTTP / 1.1 200 OK 
Date: Fri, 04 Feb 2000 21:03:38 GMT
Server: Apache / 1.3.9 (UNIX) PHP / 4.0b3
Set-Cookie: name = xyz; expires = Friday, 04-Feb-07 22:03:38 GMT;
path = /; domain = quantrimang.com
Connection: close
Content-Type: text / html

As you can see, the Set-Cookie of the cookie contains a name / value pair, GMT date, path path, and a domain name. This name and value will be the encrypted URL. The expires field is an instruction for the browser to "forget" this cookie after the given time.

If the browser is configured to store cookies, it will keep this information until the expiry date. If the user points to any page that has the same path and domain as the cookie, it sends this cookie to the Server. The browser header can be seen as follows:

 GET / HTTP / 1.0 
Connection: Keep-Alive
User-Agent: Mozilla / 4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image / gif, * / *
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1, *, utf-8
Cookie: name = xyz

Then, a PHP script will have access to this cookie with the $ _COOKIE or $ HTTP_COOKIE_VARS [] environment variable, which keeps all the names and values ​​of the cookie. The above cookie can be accessed using $ HTTP_COOKIE_VARS ["name"].

Set cookies by PHP

PHP provides the setCookie () function to set a cookie. This function requires up to 6 parameters and it should be called before the tag. For each cookie, this function must be called separately.

 setcookie (name, value, expire, path, domain, security); 

Details of each parameter:

Name - Set the name of the cookie and it is stored in an environment variable, HTTP_COOKIE_VARS. This variable is used when accessing cookies.

Value - Set the value of variable name and it is the content you really want to store.

Expiry - Indicates the expiry date of the cookie. Time in seconds from 1/1/1970. After this time, the cookie will not be accessible. If this parameter is not set, the cookie will automatically expire when the browser is closed.

Path - Defines the directory where cookies are valid. A single slash character (/) allows cookies to be valid for all directories.

Domain - Identify the domain name. All valid cookies for the given domain name only.

Security - It can be set to 1 to indicate that this Cookie is only sent by secure transmission using HTTPS, whereas if set to 0, it means that the cookie can be sent using regular HTTP.

The following example will create 2 cookies, name and age , which will expire after 1 hour.

  php setcookie ( "name" , "QTM" , time ()+ 3600 , "/" , "" , 0 ); setcookie ( "age" , "18" , time ()+ 3600 , "/" , "" , 0 ); ?> Thiết lập cookie với PHP  php echo "Ví dụ thiết lập cookie" ?> 
 

Access cookies with PHP

PHP provides a lot of ways to access cookies. The easiest way is to use the $ _COOKIE or $ HTTP_COOKIE_VARS variable. The following example will access all cookies set in the above example.

 Truy cập cookie bằng PHP  php echo $_COOKIE [ "name" ]. "
" ; /* là tương đương với */ echo $HTTP_COOKIE_VARS [ "name" ]. "
" ; echo $_COOKIE [ "age" ] . "
" ; /* là tương đương với */ echo $HTTP_COOKIE_VARS [ "age" ] . "
" ; ?>

You can use isset () to check if cookies are set.

 Truy cập cookie bằng PHP  php if ( isset ( $_COOKIE [ "name" ])) echo "Xin chào! " . $_COOKIE [ "name" ] . "
" ; else echo "Xin lỗi. Mình không quen nhau!" . "
" ; ?>

Delete cookies with PHP

To delete a cookie you should call the setCookie () function only with the name parameter, but it doesn't always work well.

The safest way to set a cookie with a date indicates that the expiry date has expired.

  php setcookie ( "name" , "" , time ()- 60 , "/" , "" , 0 ); setcookie ( "age" , "" , time ()- 60 , "/" , "" , 0 ); ?> Xóa cookie bằng PHP  php echo "Cookie đã bị xóa!" ?> 

Follow tutorialspoint

Previous article: PHP function

Next article: Session in PHP

4 ★ | 2 Vote