Home > PHP Scripts Examples and Tutorials > PHP and Cookies
Today's News
India still ‘world class’ insists Mathews
SL to market Ayurveda drugs
Nine hand grenades found near Aluth Kade Court
Hirunika to join active politics
IPL teams splash the cash for Mahela and Murali
Anoma to lead protest to free SF
US diplomats heading to Sri Lanka
CID probing double murder in Kahawatta
Dr. R.H.S. Samaratunga appointed Sec to Petroleum Ministry
A/L fiasco - Probe team’s mandate limited
Three including police officer injured in Maligawatta shootout
External degrees are worthless says Minister S.B. Dissanayke
SL rules out currency devaluation in 2012
මාළිගාවත්තේ වෙඩි තැබීමක් -තිදෙනෙකුට තුවාලයි
ක්රිකට් මුල් පුටුව උපාලි ධර්මදාසට
Accidents on the rise as New Year dawns
CAA yet to act on gas price revision
38 students yet to receive A/L results - CTU
நாடாளுமன்ற தெரிவுக்குழுவை நாம் முற்றாக நிராகரிக்கவில்லை - சம்பந்தன்
மட்டு புதுவருட கொண்டாட்டங்கள்
Love & Relationship
Are you in Love?
10 Top Sex Secrets Men won't Tell
Top 10 — Female Turn Ons
10 Commandments for good wives
How to live a happy and satisfied Life
Did I marry the right person?
Are You Happy
Alcohol makes men BETTER in the bedroom, scientists claim
Too busy for Love Making?
Have you gone off sex?
Love Isn't Blind
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Are You Getting Attracted To Another Person?
Do you feel loved?
PHP and Cookies
In this article we will discuss about cookies and how to make cookies work with PHP.
Thursday, Nov 15, 2007 | 2308 Views | Comments [View/Post]
What the heck are cookies?
A cookie can hold small collection of information/data, that is stored on the users local computer and is mostly used by websites to identify users who have previously registered or visited the site.
We will be using the setcookie() function provided in PHP to set cookies.
The syntax for setcookie() function:
int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
Looks confusing let me give you an example to simplify things!
Setting a Cookie
<?php
$site_name = "PHPbuddy.com";
setcookie("first_cookie",$site_name,time()+604800);
?>
In the above example we have created a cookie with the name first_cookie which contains the value "PHPbuddy.com", the cookie has an expiry time of 1 weeks means that the cookie will be automatically deleted after one week. Okay that 604800 is 1 week in sec!
Reading data from Cookies: Now that we have made a cookie, I will show you how to read data stored in the cookie, there are three methods to reterive cookies.
$site = $first_cookie // Not recommended
$site = $HTTP_COOKIE_VARS["first_cookie"]; //Recommended
$site = $_COOKIE["first_cookie"]; // Recommended but requires PHP 4.1
I personally like the 2nd one. The first method relies on PHP to search through every possible variable and finally find the cookie and can be used by name. However with 'register_globals' off in the PHP configuration file would cause the cookie to fail. Instead using the second will always allow your scripts to run. This gets the cookies name out of the specified cookies variables which makes it a lot faster and reliable. The third method is the best, folks at PHP group tell you to use this method although it requires PHP 4.1 and above to work.
Deleting a cookie
It is good to delete a cookie manually from your site. All you do is set the same cookie but with no value and with an expiry date in the past. This forces the browser to delete the cookie from the users system. Below is how we'd delete our first_cookie cookie from the users system:
<?php
setcookie ("first_cookie", "", time()-60000);
?>
As shown, the value is empty and the expiry date is the current time() minus 60000 seconds, Any negative number will work but due to variations in computer times, it is not recommended to use -1 but instead something higher like a day or two.
int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
$site_name = "PHPbuddy.com";
setcookie("first_cookie",$site_name,time()+604800);
?>
$site = $first_cookie // Not recommended
$site = $HTTP_COOKIE_VARS["first_cookie"]; //Recommended
$site = $_COOKIE["first_cookie"]; // Recommended but requires PHP 4.1
setcookie ("first_cookie", "", time()-60000);
?>
Comment on this article
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
Follow @MulPituwa
News Categories
Accidents and Incidents
Art and Society
Business and Finance
Education and Employment
Environment and Development
Help and Aid
Science and Technology
Security and Politics
Share Market and Exchange Rates
Sports and Entertainment
Tourism and Foreign Affairs
Women and Health
News Other Language
World News
Movie News
Information
Information
Women and Health
Food Recipes
Love and Relationship
Web and IT
AJAX Examples and Tutorials
Java Scripts Examples and Tutorials
PHP Scripts Examples and Tutorials
Travel News
Most Populer
Men want good-looking women
Making sex exciting
Duminda unmasked by Anarkali
Too busy for Love Making?
Srikanth Vandana got a Baby Boy-Father and Son Born on Same Day!
Anjali’s days with Bharathiraja and K. Balachandar
Have you gone off sex?
Sex and Shoes
Dirty Talk!
Happy Birthday Surya
Alcohol makes men BETTER in the bedroom, scientists claim
Sexless Marriages
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Outsider Played Actress Sneha Hot Sexy Hip Cheap Photos?

































