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
- Instructions on how to use Topping effectively in Cookie Run: Kingdomtopping is an extremely important part of cookie run: kingdom because it helps to increase the power of cookies, increasing the team's stats in combat.
- The list of ranks and functions of the treasures in Cookie Run: Kingdomtreasures in cookie run: kingdom are items for battle, which can help in a variety of ways. they can be active (must be touched by the player to make them active) or passive (continuously active or automatically activated).
- Cookie Run: Kingdom – Tips to overcome the Tower of Sweet Chaostower of sweet chaos is unlocked after going 4-12 in cookie run: kingdom adventure mode. this is one of the best sources of jelly exp and contains treasure chests that provide many useful rewards
- Cookie Run: Kingdom - A Guide to Collecting Soulstones in Dark Modein cookie run: kingdom, the dark mode is similar to the regular story mode, except that the levels are more difficult to overcome. a person can only play each stage 3 times, meaning that losing a stage will not be counted.
- How to collect coins and Frost Crystals fast in Cookie Run: Kingdomhow to collect coins and frost crystals fast in cookie run: kingdom, coins are important currency in cookie run: kingdom. this article will guide
- Cookie Run: Kingdom – Instructions to unlock Rainbow Shell Galleryrainbow shell gallery is an additional shop in cookie run: kingdom where players can purchase items, epic level soulstones with rainbow pearls.
- Is it really necessary to accept all cookies when browsing the web?cookie consent pop-ups are so common that you can simply click accept all without reading the message. but do you really need to accept all cookies?
- What is a cookie? How does cookie work?a cookie is a piece of text that a web server can store on a user's hard drive. cookies allow a website to store information on a user's computer and then retrieve it. information pieces will be saved as name-value pairs (name-value).
- List and strengths of servers in Cookie Run: Kingdomlist and strengths of servers in cookie run: kingdom, list of servers available in cookie run: kingdom, strengths of each server and how to choose
- Malware WSL appeared with the ability to steal browser authentication cookieswindows subsystem for linux (wsl) has not been released for a long time, but there is already malicious code targeting this system.