top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How cookies can be set and destroyed in PHP?

0 votes
89 views
How cookies can be set and destroyed in PHP?
posted Aug 5, 2014 by Amritpal Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Setting a cookie

We will use the setcookie() function to do this. The parameters used in this example are:

"name " This will be the name of the cookie. "value" The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename'] expire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

setcookie.php
code:

<?php
setcookie('username', 'maveric', time()+10);
?>

viewcookie.php
To view anytime the cookie status we need this piece of code:
code:

<?php
echo $_COOKIE['username'];

Removing a cookie

In this part we can use the previous files, with a small editing.
To remove a cookie happens simply by resetting the cookie. Instead of setting the cookie the value for time will be put to minus value for the previous plus value.
code:
<?php

setcookie('username', 'maveric', time()-10);

?>
answer Aug 6, 2014 by Devyani
0 votes

A cookie is often used to identify a user.
The function setcookie() is used to enable a cookie.
Syntax:
setcookie(name, value, expire, path, domain);

where name is cookie name,value is value of cookie ,expire is time after which cookie will get expired. The PHP $_COOKIE variable is used to retrieve a cookie value.

While deleting a cookie ,one should assure that the expiration date is in the past.
Syntax: setcookie(name,,time()-3600);

answer Aug 7, 2014 by Karamjeet Singh
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...