Sunday, October 20, 2019
How to Set and Use Cookies in PHP
How to Set and Use Cookies in PHP As a website developer, you can use PHP to set cookiesà that contain information about the visitors to your website. Cookies store information about a site visitor on the visitors computer that can be accessed upon a return visit. One common use of cookies is to store an access token so the user doesnt need to log in each time he visits your website. Cookies can also store other information such as the users name, the date of the last visit and shopping-cart contents. Although cookies have been around for years and most people have them enabled, some users either do not accept them because of privacy concerns or automatically delete them when their browsing session closes. Because cookies can be removed by a user at any time and are stored in a plain-text format, dont use them to store anything sensitive. How to Set a Cookie Using PHP In PHP, the setcookie() function defines a cookie. Its sent along with the other HTTP headers and transmits before the body of the HTML is parsed. A cookie follows the syntax: setcookie(name,value,expire,path,domain,secure,httponly); where nameââ¬â¹ denotes the name of the cookie and ââ¬â¹valueââ¬â¹ describes the cookies contents. ââ¬â¹For the setcookie() function, only theà nameââ¬â¹ parameter is required. All other parameters are optional.à Example Cookie ââ¬â¹To set aà cookie named UserVisit in the visitors browser that sets the value to the current date, and further sets the expiration to beà in 30 days (2592000 60 seconds * 60 mins * 24 hours * 30 days), use the following PHP code: ?php $Month 2592000 time();//this adds 30 days to the current timesetcookie(UserVisit, date(F jS - g:i a), $Month);? Cookies must be sent before any HTML is sent to the page or they do not work, so the setcookie() function must appear before the html tag. How to Retrieve a Cookie using PHP To retrieve a cookie from the users computer upon the next visit, call it with the following code: ?phpif(isset($_COOKIE[UserVisit])){$last $_COOKIE[UserVisit];echo Welcome back! br You last visited on . $last;}else{echo Welcome to our site!;}? This code first checks if the cookie exists. If it does, it welcomes the user back and announces when the user last visited. If the user is new, it prints a generic welcome message. TIP: If you are calling a cookie on the same page you plan to set one, retrieve it before you overwrite it. How to Destroy a Cookie To destroy a cookie, use setcookie() again but set the expiration date to be in the past: ?php $past time() - 10; //this makes the time 10 seconds ago setcookie(UserVisit, date(F jS - g:i a), $past);? ââ¬â¹Optional Parameters In addition to valueà andà expire, the setcookie() function supports several other optional parameters: Pathââ¬â¹ identifies the server path of the cookie. If you set it to / then the cookie will be available to the entire domain. By default, the cookie works in the directory its set in, but you can force it to work in other directories by specifying them with this parameter. This function cascades, so all subdirectories within a specified directory will also have access to the cookie.Domainââ¬â¹Ã ââ¬â¹identifies the specific domain that the cookie works in. To make the cookie work on all subdomains, specify the top-level domain explicitly (e.g., sample.com). If you set the domain to www.sample.com then the cookie is only available in the www subdomain.Secureââ¬â¹ specifies whether the cookie should transmit over a secure connection. If this value is set to TRUE then the cookie will set only for HTTPS connections. The default value is FALSE.Httponlyââ¬â¹, when set to TRUE, will only allow the cookie to be accessed by the HTTP protocol. By default, the value is FALSE. T he benefit of setting the cookie to TRUE is that scripting languages cannot access the cookie.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.