Facebook Twitter Reset

This Tutorial Has Been Viewed 694 Times.

How To Set And Get Cookies Using PHP

Here is a simple code that explains how a cookie is set and retrieved using php.

A cookie is set with the following code: setcookie(name, value, expiration)

$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(MyVisit, date(“F jS – g:i a”), $Month);
?>
The above code sets a cookie named “MyVisit” in the visitors browser. The cookie sets the value to the current date, and set’s the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)

Now as the cookie is set, we have to retrieve this cookie in another page so for that copy this code

Now let’s retrieve the cookie.

 

<?php 
if(isset($_COOKIE['MyVisit']))

$last = $_COOKIE['MyVisit']; 
echo “Welcome back! <br> You last visited on “. $last; 

else 

echo “Welcome to our site!”; 

?>

Simple try out this code and enjoy!!!!!!!!!!!

VN:F [1.9.13_1145]
Rating: 7.0/10 (3 votes cast)
VN:F [1.9.13_1145]
Rating: +1 (from 1 vote)
How To Set And Get Cookies Using PHP, 7.0 out of 10 based on 3 ratings

Incoming search terms:




You will also be interested in ,

No comments yet.

Leave a Comment

Spam protection by WP Captcha-Free

Affordable Seo PackagesSeo BlogEdu Backlinks
More in PHP (119 of 122 articles)