Register on the forum now to remove ALL ads + popups + get access to tons of hidden content for members only!
Site Owners Forums - Webmaster ForumsCalendarContact Us

Go Back   Site Owners Forums - Webmaster Forums > Web Programming > Programming General > PHP / mySQL


Reply
 
Thread Tools Rate Thread Display Modes
Old 12-01-2008, 03:42 AM   #1
alex.brown
Registered User
 
Join Date: Dec 2008
Posts: 1
PHP Cookies Tutorial

A cookie is a flat file that stores tiny bits of information. It is stored on the client’s machine and is passed to the client when they visit your site. Each cookie can store anything from usernames to number of visits to a site. This tutorial will teach you how to create, retrieve, display, and delete cookies using php
1. Create a Cookie using PHP

Cookies are created using the setcookie() function. Below is an example of the setcookie() function
setcookie($name, $value, $expired, $path, $domain, $secure)
There are values that you can pass into the setcookie() function. The values are: name of the cookie, value of the cookie, the date the cookie expires, path where the cookie is available on the server, domain where the cookie is available, and where it is coming from a secure page.
The three values that we are worried about are $name, $value, and $expired.
Below is the php code to create a php cookie named "username". We set the value of the cookie to "Tom", and set the expiration date at one hour from now.
<?php
setcookie("username", "Tom", time()+3600);
//http://www.infysolutions.com
?>
The cookie is set through the browser once a client visits your site or page that you set the cookie on.

2. Retrieve & Display a Cookie using PHP

Now let’s retrieve & display the cookie we just set. We retrieve the cookie using the predefined $_COOKIE array. Then we display the cookie using the echo function. Here is the code that will get you the cookie.
<?php
echo $_COOKIE["username"];
?>
3. Delete a cookie using PHP
We can delete the cookie when we are finished with it. Cookies are deleted by simply setting the expiration date to a past date. The example below will delete the cookie named username.
<?php
setcookie("username", "Tom", time()-(60*60*24*365));
?>
That’s it! You have just created a cookie on the client’s machine, retrieved & displayed the cookie and deleted the cookie once you were done with it.
alex.brown is offline   Reply With Quote

Old 09-11-2009, 06:30 AM   #2
nazar2k2
Registered User
 
Join Date: Jul 2009
Location: Bangalore
Posts: 11
go thru this tutorial

Hi,

To know more about the cookies and its related functions please refer the w3school sites and php.net site.
nazar2k2 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP MySQL Web Development Security Tips Kristin123 PHP / mySQL 0 10-16-2008 04:33 AM
PHP 4 or PHP 5? shaun PHP / mySQL 1 12-21-2007 08:39 PM
PHP Tutorial - Ad Rotation Script axer PHP / mySQL 0 12-13-2007 01:58 AM
PHP Tutorial DerkThunder PHP / mySQL 2 04-01-2007 08:15 AM
Misconceptions about PHP joeboe General Discussion 4 08-08-2006 06:02 PM


All times are GMT -7. The time now is 09:02 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Kindly hosted by OH Telecom servers (get one!)
Site Delivered By Liberty Of Mind - #1 Adult & Mainstream Solutions Provider