Tutorial of Cookies in PHP
In this article we will discuss about cookies and how to make cookies work with PHP.
Software >> PHP Programming
Overview: In this article we will discuss about cookies and how to make cookies work with PHP.
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.
-
Lastest Articles
10 principles of search engine friendly web design
Search engine friendly web design is web design that is planned around known search engine optimization principles. If ranking well in the search engines is important to you then the first step towards6 tips to improve results from your website
Make sure you know why you want a website and what you want your website to do for you. Write down some broad goalsCross Browser Compatibility
there are really only two types of browsers you need to consider. If you run a fairly standard website, these will probably account for 99% of your audienceHow to get the Web 2.0 Look and Feel
refers to the new wave of community driven websites, it is also increasingly used to describe the fresh and clean design approach they useRevealing Facebook Application XSS Holes
Beginning tomorrow, September 1st, I will begin posting full technical details of cross-site scripting vulnerabilities that I have discovered in Facebook applicationsFacebook Applications are Now Even More Valuable Hacking Targets
which I called a FAXX hack, enables one to not only post links to Facebook for viral effects but also harvest a wealth of information on victimized users along the wayNew Trick to View Hidden Facebook Photos and Tabs
Last December, I posted a bit of JavaScript known as a bookmarklet that allowed you to see photo albums for any Facebook user if the album privacy settings allowed itUsing Google Buzz Can Expose Your Gmail Address
In short, having a public Google profile (which you might have created when checking out Google Buzz) can allow others to figure out your Gmail addressGoogle Takes Small Steps for Buzz, Points to Big Solutions for Social Networking
Buzz, Google's controversial attempt to unseat Facebook as the most mainstream of social activity stream readers, just made some much-needed changes that Facebook could learn from as wellGet Satisfaction Turns Facebook Fan Pages into Customer Support Hubs
Get Satisfaction, the popular online customer service company, just announced that it is bringing its service to Facebook fan pages


























