PHP Cookies is used to sent the information by a web-server to a web-browser and whenever you accesses that server the browser will give you the same result without changing anything. Cookies in PHP is helpful to identify the user.
PHP Cookies is used to sent the information by a web-server to a web-browser and whenever you accesses that server the browser will give you the same result without changing anything. Cookies in PHP is helpful to identify the user.PHP Cookies
PHP Cookies is used to sent the information by a web-server to a web-browser and whenever you accesses that server the browser will give you the same result without changing anything. Cookies in PHP is helpful to identify the user.
If you are developing highly interactive website, it means you are going to deal with bunch of data flow between client browser and web server. It is very difficult to handle such large amount of data. This problem can be solved by using Cookies.
Cookies with PHP are helpful to create and retrieve cookies value.
setcookie() Function
This function is used to set a cookie and also send the information along with the HTTP Headers.
Syntax for PHP Cookies
setcookie($name,$value,$expire,$path,$domain,$secure)
Let's see the example below :
Example 1 :
<?php
SETCOOKIE("userName","Rose India");
SETCOOKIE("ADDRESS","NEW DELHI");
SETCOOKIE("TIME",date("M d y"));
if(isset($_COOKIE['userName']))
{
echo "Welcome to ".$_COOKIE['userName']."!!"."<br>";
echo "Your address is ".$_COOKIE['ADDRESS']."!!"."<br/>";
echo "You are last visited on ".$_COOKIE['TIME']."!!"."<br/>";
}
else
{
echo "Cookies set,please refresh the page!!";
}
?>
The output of the above example is : Welcome to Rose
India!!
Your address is NEW DELHI!!
You are last visited on Jan 18 10!!
Cookies Array
Example 1 :
<?php
SETCOOKIE("userName","Rose India");
0SETCOOKIE("ADDRESS","NEW DELHI");
SETCOOKIE("TIME",date("M/d/y"));
if(isset($_COOKIE['userName']))
1{
print_r($_COOKIE);
}
2else
{
echo "Cookies set,please refresh the page!!";
3}
?>
The output of the above example is : Array ( [userName] => Rose India [ADDRESS] => NEW DELHI [TIME] => Jan/18/10 [cookie] => Array ( [username] => ROSEINDIA [address] => NEWDELHI [time] => Jan/18/10 ) )
Example 1 :
<?php
setcookie("cookie[username]", "ROSEINDIA");
setcookie("cookie[address]", "NEWDELHI");
setcookie("cookie[time]", date("M/d/y"));
6if (isset($_COOKIE['cookie']))
{
foreach ($_COOKIE['cookie'] as $name => $value)
7{
echo "$name : $value <br />\n";
}
8}
?>
The output of the above example is : username =>
ROSEINDIA
address => NEWDELHI
time => Jan/18/10