- When a user logs in to a application, performs some activity and close the application, this entire scenario is called as session.
- A web server needs to track all the activities of users. It needs to store required information about the user. This whole process is called as session tracking.
- There exists a session array which often stores the unique session ID for the session.
- PHP keep tracks of session by using a function called session_start().
- When session_start() function is invoked , session ID is created and recorded.
Following is a PHP code:
<?php
session_start();
if(isset($_SESSION[‘PGVISIT’]))
{
$_SESSION[‘PGVISIT’])=$_SESSION[‘PGVISIT’])+1;
echo “You are visiting this page for:”$_SESSION [‘PGVISIT’])” times”;
}
else
{
echo “You are visiting this page for the first time”;
}
?>
- Another way to store information is using Cookies. Cookies is a small file that server embeds in a user’s machine.
- Cookies are used to identify users. It consists of a name and textual value. It can be created by some software system on the server.
- In every HTTP communication between browser and server, a header is included.
The header part contains the information stored in cookies, about the message.
-- In PHP, a function setcookie() can be used to create cookie.
<?php
$Cookie_period=time()+60*60*24*30;
setcookie(“Myname”,”Alia”,$Cookie_period);
?>
An information from the cookie can be retrieved in a following way:
<?php
if(isset($_COOKIE[“Myname”])
echo “Welcome” .$_COOKIE[“Myname”].”….!!”;?>