GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Dec-2008, 05:52
jerry123 jerry123 is offline
New Member
 
Join Date: Dec 2008
Posts: 1
jerry123 is an unknown quantity at this point

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);
?>
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
//www.*.com
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.
-----------------------------------------------------------------------------------
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB 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
Looking for a book or tutorial on OpenSSL with php Blake MySQL / PHP Forum 2 13-Feb-2008 10:19
Help ! I need help starting up on Php ! onauc MySQL / PHP Forum 11 05-Jan-2005 00:57
PHP in HTML jrobbio Web Design Forum 4 24-Jul-2003 07:05
All the big PHP script collections that matter jrobbio MySQL / PHP Forum 5 06-Jun-2003 17:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:33.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.