How To Get Cookie'S Name In Php?

The Anatomy of Cookies in PHP

In most cases, cookies are set in the HTTP header (although JavaScript can also phối a cookie directly on a browser). It generally shows the location path of the cookies in PHP. If a PHP script sets a cookie, it can submit headers that look lượt thích this:

HTTP/1.1 200 OK

Date: sat, 24 April 2021 21:03:38 GMT

Server: Apache/1.3.9 (UNIX) PHP/4.0b3

Set-Cookie: name=xyz; expires= sat, 24 April 2021 21:03:38 GMT;

Path=/; domain=localhost

Connection: close

Content-Type: text/html

The Set-Cookie header, as you can see, contains a name value pair, a GMT date, a path, và a domain. URL encoding will be used for the name & value. The expires field tells the browser that the cookie should be "forgotten" after the specified time and date. If the browser is set lớn save cookies, the details will be saved until the expiration date.

Bạn đang xem: How to get cookie's name in php?

If the user navigates lớn some page on the internet.

*

How lớn Create Cookies in PHP

The setcookie() function is used to generate a cookie.

Syntax:

setcookie(cookie_name, cookie_value,date_of_ expire,cookie_ path,Web_ domain, protocol_security);

Cookie_name:

This sets the cookie's name, which is saved in the HTTP COOKIE VARS environment variable. When accessing cookies, this variable is used.

Cookie_value:

This is the nội dung that you really choose to store & sets the value of the named variable.

Date_of_expire:

This specifies a future time in seconds.After this time, the cookie will no longer be available.

If this parameter is not set, cookies will expire when the website Browser is closed.

cookie_path:

This indicates which folders the cookie is valid for. The cookie is true for all directories thanks to lớn a single forward slash character.

web_domain:

This can be used to specify a domain name in very broad domains, and it must include at least two cycles in order khổng lồ be legitimate. Cookies are only valid for the host & domain from which they were generated.

protocol_security:

This can be set lớn 1 khổng lồ indicate that the cookie can only be transmitted via secure HTTPS transmission, or 0 to lớn indicate that the cookie should be sent via standard HTTP.

Xem thêm:

Sample Code

setcookie("first_cookie", "username_of_the tài khoản is anon", time()+3600, "d:/testcoookie/","", 0);

First cookie located here, is the name of the cookie that stores the meaning as the username of the trương mục is anon và the time between logged in and 3600 milliseconds. Following that, it will be deleted from the screen.

D:/testcookie/- location of cookies which store the cookie value in a specified amount of time.

 Code

*

ExplanationIn the above example, two cookies have been created. One is the first_cookie used to lớn hold the name of the user .Second is the second_cookie used lớn store the age of the user.Both the cookies will be saved into the location d:/testcookie/ on the local machine.The cookie will expire after 3600 milliseconds when it starts to lớn be kept on the local machine. That zero represents the HTTP.

How lớn Access the Cookies in PHP

Cookies can be accessed in a variety of ways in PHP. The most straightforward method is to use the $_COOKIE or $HTTP COOKIE VARS variables. It's usually an associative array that's keyed by cookie name and includes a danh mục of all the cookie values sent by the browser in the current request. The example below will access all of the cookies mix in the previous example.

Sample Code

echo $_COOKIE<"first_cookie">. "";

echo $HTTP_COOKIE_VARS<"second_cookie">;

Code

*

Check Cookies

The isset() function can be used lớn determine whether or not a cookie has been set.

Code:

*

Output:

Set Cookies Welcome anon


*

Deleting the Cookies in PHP

Officially, you can điện thoại tư vấn setcookie() with only the name argument khổng lồ delete a cookie, however, this does not always function well & should not be depended upon.

Code:

*
 

To reset the time as subtraction timing in the example above. It deletes cookies from the machine automatically.

Cookie 'first_cookie' is deleted.

Modifying the Cookies

To change the value of a cookie, simply use the setcookie() function to vị so:

*

Output

*

Advance your career as a MEAN stack developer with the Full Stack web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

To recognize a user, a cookie is frequently used. A cookie is a tiny tệp tin placed on the user's machine by the server. The cookie will be sent each time the same machine requests a page via a browser. Cookie values can be created and retrieved using PHP.

Hope you found the information in this article useful. Become a master of modern coding techniques with obatambeienwasirherbal.com’s Postgraduate Program in Full Stack web Development that is conducted in partnership with Caltech CTME. Joining this course will help you advance your career as a software developer, và you'll have everything lớn become a full-stack technologist.

As a support during the difficult times, we have also put our courses together for free! You can also gain access to our miễn phí courses & enhance your current skill set. There is no prerequisite for any course. Just your interest and passion is enough. 

Just browse through the courses và if you have any questions, leave them in the comments section of this article, and our experts will get back to lớn you on them, as soon as possible!