Just because HTTP is a stateless protocol it does not remember what user has done last time or a last minute we have to manually manage our state while communicating with the server, state management is a good topic and it includes some core PHP basics such as Session state management , Query string, cookies and Databases.
Managing states in PHP :
.
PHP Cookies :-
Cookies are small piece of information that stored at the client side for later reference .
Some basic function of cookies are storing user name and a encrypted password at the client side when a user
click on the remember me check box, or when a website wants to track their user,.
PHP syntax for cookies are :
Tips :-
1. A cookie must be set before ant output is sent to the browser, this is the same condition as for header including <html><head> tag and ant whitespace.
An example of using cookies :
<?php
//this file is included
if(isset($_POST['set']))
{
if ( setcookie("name","developer",time()+30) ) //thirty seconds expiry time
echo "Cookie has been set successfully!";
else
echo "FAIL";
}
?>
<html>
<head><title>PHP cookies.</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> method="post" >
<table>
<tr><td>Name</td><input type="text" name="name" /></tr>
<tr><td> </td><input type="submit" name="set" value="setcookie" /></tr>
</table>
</form>
</body>
</html>
save this as name anyname.php
Now retrieving value from a cookie : -
Once the cookie has been set it have to retrieved on the next page for that purpose we use $_COOKIE superglobal array it is An associative array of variables passed to the current script via HTTP Cookies.
An example for $_COOKIE :-
<?php
echo "Getting value the saved cookie";
$value = $_COOKIE['name'];
echo "<br/>";
echo "Cookie value is : " .$value;
?>
save this page to anyname.php
Managing states in PHP :
.
PHP Cookies :-
Cookies are small piece of information that stored at the client side for later reference .
Some basic function of cookies are storing user name and a encrypted password at the client side when a user
click on the remember me check box, or when a website wants to track their user,.
PHP syntax for cookies are :
(PHP 4, PHP 5)
setcookie — Send a cookie
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [,bool $secure = false [, bool $httponly = false ]]]]]] )Tips :-
1. A cookie must be set before ant output is sent to the browser, this is the same condition as for header including <html><head> tag and ant whitespace.
An example of using cookies :
<?php
//this file is included
if(isset($_POST['set']))
{
if ( setcookie("name","developer",time()+30) ) //thirty seconds expiry time
echo "Cookie has been set successfully!";
else
echo "FAIL";
}
?>
<html>
<head><title>PHP cookies.</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> method="post" >
<table>
<tr><td>Name</td><input type="text" name="name" /></tr>
<tr><td> </td><input type="submit" name="set" value="setcookie" /></tr>
</table>
</form>
</body>
</html>
save this as name anyname.php
Now retrieving value from a cookie : -
Once the cookie has been set it have to retrieved on the next page for that purpose we use $_COOKIE superglobal array it is An associative array of variables passed to the current script via HTTP Cookies.
An example for $_COOKIE :-
<?php
echo "Getting value the saved cookie";
$value = $_COOKIE['name'];
echo "<br/>";
echo "Cookie value is : " .$value;
?>
save this page to anyname.php