SESSION-
A session contains user information on server side. When a user opens a page, then the information is first transferred to the server, then php will match that information to the server-side cookie.
If the browser is closed once, all the data in the session will be lost. On re-opening the browser a new session will start. To maintain the data $_SESSION super global variable is used.
Advantages-
- Ability to store large amount of data.
- Secure because data is stored on the server side.
- Can store objects and string both.
Disadvantages-
- All data is lost when browser is closed.
- Depend on cookie.
Creating a session:
<?php session_start(); ?>
Setting value:
<?php $_SESSION[first_name] = Neha; ?>
Reading value:
<?php echo $_SESSION[first_name]; ?>
Removing data:
<?php unset($_SESSION[first_name]); ?>
Ending session:
<?php session_destroy(); ?>
COOKIE-
A cookie contains user information in the client side. $_COOKIE superglobal stores the value, which is read by the php.
There is no need to store cookie, it remains stored in our system.
Advantages-
- Cookie remains stored if the browser is closed.
Disadvantages-
- Limited amount of data can be stored.
- Stored in client side, so user can view it.
Creating a cookie:
<?php
if (!isset($_COOKIE['Ordering'])) {
setcookie("Ordering", $_POST['ChangeOrdering'], time() + 31536000);
}
?>
Using a cookie:
<?php
echo (isset($_COOKIE[ordering])) ? $_COOKIE[ordering] : cookie value not set;
?>
Deleting a cookie:
<?php setcookie(favorite_color); ?>
0 Comment(s)