Session and Cookie in PHP Reviewed by Momizat on . Session and cookie creates an environment.Both are used to value of variable from one page to another in a project. Session :- Session creates an environment an Session and cookie creates an environment.Both are used to value of variable from one page to another in a project. Session :- Session creates an environment an Rating: 0

Session and Cookie in PHP

Session and cookie creates an environment.Both are used to value of variable from one page to another in a project.

Session :- Session creates an environment and data stored on the server side (in browsers).

Cookie :- Cookie creates an environment and data stored on the client side (in user computer).

Example of Session in PHP

<?php
// Always user top of page session_start();
session_start();

// assign value of name variable of session
 
$_SESSION['name']='manish';
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php

// Get value of session variable
echo "Name :- ".$_SESSION['name'];
//output Name :-manish

?>
</body>
</html>


<?php
// how to destroy session value
// unset() function is used to remove value of particular variable

unset($_SESSION['name']);

// And session_destroy() is used to completely destroy all session data

session_destroy();

?>

Example of Cookie in PHP

<?php

// Five parameter in setcookie(name, value, expire, path, domain);
// expire parameter is used to define expiring time of cookie value of variable
 
setcookie('name', 'manish',time()+3600);

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
 <?php

// Get value of cookie variable
echo "Name :- ".$_COOKIE["name"];
//output Name :-manish

?>
 </body>
</html>


<?php
// how to destroy cookie value 
//here empty value of cookie variable 
setcookie("name", "", time()-3600);

?>

Leave a Comment

© 2014 Powered By