80 likes | 101 Views
Implementing Cookies in PHP. various aspects of cookies in PHP Creating a cookie Accessing a set cookie Deleting a cookie. Creating a cookie. You need three basic functions to create a cookie. setcookie() time() mktime() The setcookie() Function
E N D
Implementing Cookies in PHP various aspects of cookies in PHP Creating a cookie Accessing a set cookie Deleting a cookie
Creating a cookie You need three basic functions to create a cookie. setcookie() time() mktime() The setcookie() Function You can create a cookie in PHP by using the setcookie() function. The Web server uses this function to set a cookie and send it to the client when the client requests a connection. setcookie (string name, string value, int expiry, string path, string domain, int secure);
Syntax of setcookie() As the above syntax specifies, the setcookie() function takes six arguments. These include: name: This argument specifies the name of the variable that will hold the corresponding value in the name-value pair. value: This argument holds the value of the variable specified by the name argument. expiry: This argument specifies the time after which the cookie will be considered invalid and thus rendered unusable. Note The value of the expiry argument is specified by two functions, time() and mktime(), about which you’ll learn a bit later in this chapter. path: This argument specifies the hierarchy of files and directories on the Web server for which the cookie is considered to be valid. Secure: This argument determines whether the cookie must be transferred through a secure channel. If no value is specified for the argument, an insecure channel is used to transfer the cookie. However, if the value of this argument is 1, the cookie is transmitted using the HTTPS protocol that ensures secure transactions over the Internet.
The time() and mktime() Functions You can use the time() and mktime() functions to set the life span of a cookie to avoid the expiry of the cookie when the browser window closes. The time() function is used to determine the current time. It returns time in seconds. The syntax of the time() function is specified below. int time(); The mktime() function, on the other hand, accepts units of time, such as hours, minutes, seconds, months, and years, as parameters and is used to convert this information into a time stamp. Just like the time() function, the time stamp returned by the mktime() function is in seconds. The syntax of the mktime() function is specified below. int mktime(int hour, int minute, int second, int month, int year);
<?php $name = "MyCookie"; $value = "This is my first Cookie!"; $expiry = mktime(0, 0, 0, 7, 1, 2022); $domain = "localhost.localdomain"; setcookie($name, $value, $expiry, /, $domain, 0); ?> <html> <head> <title> Testing the Cookie </title> </head> <body> <br> <br> <br> <h1 align = center> This appears when the cookie is created successfully! </h1> </body> </html>
Accessing a cookie Using the name of the cookie as a variable. When you try to access a cookie in PHP, a variable with the same name as the cookie is created. This variable name, then, can be used by your scripts. As a result, you can either reload the page that was used to create the cookie or load another page that uses the cookie. Suppose you created a cookie called My_Cookie. You can use the name of the cookie as a variable in your script as specified below: echo "My_Cookie"; Using $HTTP_COOKIE_VARS [“cookie_name”]. Again, the variable that is created by default in PHP by using the cookie name is also stored in the global array as $HTTP_COOKIE_VARS ["cookie_name"]. An additional advantage of using this global array is that it helps in distinguishing between different cookie names on the basis of their data sources, even if they happen to have the same name. You can use this global array in your PHP scripts as given below: echo "$HTTP_COOKIE_VARS ["My_Cookie"]" $_COOKIE [ “cookie_name”]
Deleting Cookies After a cookie has served its purpose and you don’t need it any longer, you might want to delete it. No use unnecessarily storing unwanted cookies! PHP does not boast of an exclusive function for this purpose. In fact, the good old setcookie() function once again comes to the rescue! Call the setcookie() function with only the name parameter when you want to delete a cookie. The syntax to delete a cookie is: setcookie("cookie_name");