KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

How to set cookie in PHP

Cookie is a piece of information stored in client machine. PHP use the function setcookie() to set or delete cookie in the client machine.

Syntax:

bool setcookie ( string cookiename [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )

setcookie() parameters

Parameter Description Examples
cookiename The name of the cookie. 'cookiename' is called as $_COOKIE['cookiename']
value The value of the cookie. This value is stored on the clients computer this value is retrieved through $_COOKIE['cookiename']
expire The time the cookie expires. time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes).
path The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.
domain The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain. Refer to tail matching in the spec for details.
secure Indicates that the cookie should only be transmitted over a secure HTTPS connection. When set to TRUE, the cookie will only be set if a secure connection exists. The default is FALSE. 0 or 1

In all the parameters showing above, except cookiename, could be omitted.

PHP set cookie

The most common used setcookie function is following:

setcookie("kidslovepc","1",time()+7*24*60*60); //expire after 7 days.

The other options are shown in following examples:

setcookie("kidslovepc");//cookie expire once after client closes all browsers

//cookie will expire after one hour
//cookie only accessible to directory php-tutorial and all its subdirectory

setcookie("kidslovepc","1",time()+60*60,"/php-tutorial");


//cookie accessible to all directory,"/"
//cookie is session cookie, time is 0
//cookie accessible to all subdomains of kidslovepc.com

setcookie("kidslovepc","1",0,"/","kidslovepc.com");

Retrieve Cookie

Once the cookies have been set, they can be accessed with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

<?php
// Print an individual cookie
echo $_COOKIE["kidslovepc"];
echo $HTTP_COOKIE_VARS["kidslovepc"];

// view all cookies
print_r($_COOKIE);
?>

Delete cookie

We also your setcookie() function to delete cookie. When deleting a cookie, the expiration date is set in the past, to trigger the removal mechanism in browser.

Examples follow how to delete cookies:


<?php
// set the expiration date to one hour ago
setcookie ("kidslovepc", "", time() - 60);
setcookie ("kidslovepc", "", 0, "/", ".kidslovepc.com");
?>

Cookie Array

Array is a good way to set multiple cookie in client machine.

<?php
// set the cookies
setcookie("kidslovepc[three]", "cookiethree");
setcookie("kidslovepc[two]", "cookietwo");
setcookie("kidslovepc[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['kidslovepc'])) {
foreach ($_COOKIE['kidslovepc'] as $name => $value) {
echo "$name : $value <br />\n";
}
}
?>

Delete cookie Array

<?php
foreach ($_COOKIE as $key=>$value) {
setcookie ($key, $value, time() - 1);
}

?>

 

 
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer
?? ? ? setcookie ($key, $value, time() - 1);
?? }

?>

 

 
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com
Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer