Cookie in PHP
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:
- Server-side script sends a set of cookies to the browser. For example: name, age, .
- The browser stores this information on the local machine for future use.
- 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
You should read it
- What is a cookie? How does cookie work?
- What is a cookie? What do cookies do and how do they work in the browser?
- How to use Cookie Cutter by Neeva to block cookies when opening the web
- How to enable Coalition cookies, how to disable/enable cookies on iPhone
- How to Delete Cookies on a Mac
- What are Supercookies, Zombie Cookies and Evercookies and are they harmful?
- 'Off the top in' cookie
- How to delete a site's cookies on Firefox
May be interested
- Session in PHPanother way to make data accessible on different pages of an entire web site is to use a php session.
- Send Email using PHPphp must be configured correctly in the php.ini file with details about how your system sends email. open php.ini available in the / etc / directory and find the section that starts with [mail function].
- Upload File in PHPa php script can be used with an html form that allows users to upload files to the server. first these files are uploaded to a temporary directory, then moved to a destination by a php script.
- Standard writing code in PHPeach company has different encryption standards based on their practical standards. encryption standards are necessary because there may be many developers working on different modules, so if they start inventing their own standards, then the source code will become very unmanageable. and it will become difficult to maintain source code in the future.
- Variables are predefined in PHPphp provides a large number of predefined variables for any script that runs. php provides an additional set of arrays containing variables from web server, environment variables and user input. these new arrays are called superglobal.
- Exception handling and error (Error & Exception Handling)error handling is the process of catching errors created by your program and then taking the appropriate actions. if you handle errors incorrectly, it can lead to unexpected results.