Saturday, 24 February 2018

Difference Between Session and Cookie in PHP

Cookie
 Cookies are stored in browser 
 It is stored limited amount of data.
 It is less secure because it is stored in browser. 
 Setting  the  cookie time to expire the cookie.
 The default expiration period for a Cookie is 30 minutes.
  Example:
<!DOCTYPE html>
<?php
$cookie_name = "admin";
$cookie_value = "dipak";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
     echo "Cookie named '" . $cookie_name . "' is not set";
}
else {
     echo "Cookie '" . $cookie_name . "' is set<br>";
     echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
<p >Note: Please reload the page to see the new value of the cookie. </p>
</body>
</html>

Session
Sessions are stored in server.
It is stored unlimited amount of data.
It is more secure because it is stored in server.
Using  session_destory(), We will destroy the sessions.
The default expiration period for a Session is 20 minutes.
Example:
     demo.php
<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
$_SESSION["admin"] = "dipak";  
echo "Session information is set<br/>";  
?>  
<a href="result.php">Click to get session value</a>  
</body>  
</html>  
result.php
<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
echo "Admin is: ".$_SESSION["admin"];  
?>  
</body>  
</html>  

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...