How To Destroy Or Remove A Cookie In Php

An HTTP Cookie or Web Cookie is a small piece of data that is send from the web vps to the client browser that can be stored in the client machine for a future look ups.The cookies were primarily designed as a mechanism lớn maintain a session state of the user sessions without too much network and/or vps traffic.There are several good uses of cookies, although it has got a lot of bad press because of misuses by websites and concerns about user privacy. HTTP Cookies can be used to lớn maintain any information as long as it is text, such that the user shopping cart, authentication details etc.If you are developing a website, then it is quite possible that you will need khổng lồ set cookies on the user client. Of course, the exact requirements of the trang web will dictate whether you need a cookie at all, but let’s assume that you want to learn how to lớn set & unset cookies from webpages.No matter what framework you use, you can mix cookies using simple JavaScript on your page. This is independent of the JS library you might be using such as Dojo or JQuery. Having said that, there are several libraries available that are supposed to lớn ease the pain of managing the cookies.A cookie can contain associated meta information along with the value of the cookie. There are some required values, while many are not required và defaults lớn reasonable or appropriate value when setting the cookie. The general syntax (or spec) of an HTTP cookie is as shown below.A cookie is mix by using the HTTP request header Set-Cookie. The value of this header is the entire cookie that will be parsed và saved in the client browser.Set-Cookie: name=value;Comment=comment;Domain=domain;Max-Age:time-in-seconds;Expires=expiry-time;Path=path;Secure;Version=1Name: This is the name of the cookie. This can be any string that uniquely identifies the cookie. You will need this later if you want khổng lồ read the value.

Bạn đang xem: How to destroy or remove a cookie in php

Value: This is the value of the cookie. This can at-most be around 4kb. The 4093 byte limit is for the entire cookie, but assuming a reasonable kích cỡ for other fields, we can specify that the value can have at-most of about 4000 bytes.Comment: An optional field that is rarely used. You specify a string that explains why and what the cookie is used for.Expires: The expires field sets the exact time when the cookie is set to lớn expire or become invalid. The time is phối using the format Wdy, DD mon YYYY HH:MM:SS GMTMax-Age: Another way khổng lồ specify the expiration time. It specifies the time interval in seconds, which is calculated from the time the cookie is set. If both Max-Age and Expires is set, then Max-Age takes preference.

Xem thêm: Category: Code Viblo - Lập Trình Hướng Đối Tượng Và Kiến Trúc Mvc

Path: You can optionally phối the path of the webpage that the cookie is mix for.Domain: You can also explicitly set the domain name name for which this cookie is set for. If not specified, then the defaults to the domain name that requested the URL.Secure: No value need khổng lồ be specified here. Adding the keyword Secure to lớn the cookie will make sure that only secure and encrypted connections such as HTTPS is used to lớn set the cookie.HttpOnly: The cookie can set và accessed by using only http connections. Other methods such as Javascript calls will not be able to lớn access the cookie.A complete example of a cookie string will be something like:Set-Cookie:mycookiename="this cookie value";;Max-Age=3600;Path=/;Secure


You can set cookies using a JavaScript function. A very simple function to set cookie will look something like this:You can now call the function using the various arguments from anywhere in the website. A valid điện thoại tư vấn to mix a cookie that is expires in an hour will be likesetCookie("CookieName", "Cookie value is not important", 3600);On a side note, setting the max-age to a negative value will make it to lớn be somewhat of a session cookie, with the browser deleting the cookie when the browser session ends. In order to lớn delete or unset the cookie, gọi method with the key (or name) of the cookie and 0 (zero) as the age. Setting the max-age to zero instead of a negative value will remove the cookie.setCookie("CookieName", "", 0);


If you are using PHP as your server-side scripting language, then you can phối cookies using the setcookie(…) function. The function has the following syntax.bool setcookie(name, value, expires, path, domain, secure, httponly)A quick example issetcookie("CookieName", "this cookie value", time()+3600, "/", "");Note that the setcookie(…) method takes the expires as the argument, and not max-age. You will need to set the time at which you want the cookie to expire rather than the time interval as shown in the JavaScript example.As with the JavaScript, you can unset or delete the cookie by setting the expires field to lớn a time và date in the past. A value of 1 for the expires field will vì the trick.


You can use the standard JavaScript functions as shown above khổng lồ set cookies. There is also plugins available in JQuery that allow you to set & unset cookies using JQuery lượt thích syntax. One of the popular plugin is jquery-cookie.First include the plugin in your webpage…Ezoicreport this ad