written 6.2 years ago by
binitamayekar
★ 6.6k
|
•
modified 6.2 years ago
|
Session
- A session begins when a user logs in to or accesses a particular computer, program or web page and ends when the user logs out or shuts down the computer, closes the program or web page.
- A session can temporarily store information related to the activities of the user while connected.
- For example, this is one way a website can remember what is in your shopping cart if you leave and come back.
- A session is used to store information that is used across multiple web pages. It stores different information for each user accessing a website.
- A session includes server-side and client-side cookies, where the client-side cookie contains a reference to the requested data on the server. Therefore, when a user visits a website, the Web browser sends the reference code to the server, which loads the requested data.
- A file is created by the session within a temporary directory on the server. This file stores the registered session variables and their values. This data is available to all the web pages of the website when the user is accessing the specified website.
- Session data can be made persistent by storing it to a persistent cookie or in a MySQL database, if needed.
- Sessions are safer to use than cookie because a user can block the cookies from being written, but cannot block the sessions. Moreover, the session information is lost when the user exits the website.
Session Tracking & Ways to do Session Tracking
- When user visits a website, their activities have to be tracked to analyze their behavior. Tracking is done to know the interest of user such as shopping baskets or the part of a site they visits the most. It also helps in maintaining security.
- Some commercial sites track users by the page they visit the most. It helps them to advertise their products.
- The easiest way to track a user is through cookie or else tracking also done with the help of session. Session is more reliable as compared with the cookie.
- The most significant difference between the two is that cookies are stored on the client, while the session data is stored on the server. As a result, sessions are more secure than cookies and sessions work even when the user has disabled cookies in their browser.
Session Tracking in PHP by using Session
- In PHP, Sessions are called using the session_start() function. Before creating any session, it first checks whether the session is already created or not. If no such session is created, then it creates a new session with the specified name.
- At the time of web page creation and before HTML tag starts, it is recommended to make a call to session_start() function.
- The isset() function checks whether or not the session variable is already set or not.
- Example: suppose we want to know the number of times that a page has been loaded, we can use a session to do that. The code below shows how to create and retrieve values from sessions.
<?php
session_start(); //start the PHP_session function
if(isset($_SESSION['page_count']))
{
$_SESSION['page_count'] += 1;
}
else
{
$_SESSION['page_count'] = 1;
}
echo 'Your visitor number ' . $_SESSION['page_count'];
?>
//Display Output: Your visitor number 1
- The session_destroy() function is used to destroy the whole PHP session variables. If you want to destroy only a session single item, you use the unset() function.
- Session_destroy() removes all the session data including cookies associated with the session. Unset() only frees the individual session variables. Other data remains intact.
Session Tracking in PHP using Cookie
- In PHP, the setcookie() function defines a cookie. It's sent along with the other HTTP headers.
- Cookies must be sent before any HTML is sent to the page or they do not work, so the setcookie() function must appear before the
<html>
tag.
- A cookie follows the syntax
setcookie(name,value,expire,path,domain,secure,httponly);
- Where name denotes the name of the cookie and value describes the cookie's contents. For the setcookie() function, only the name parameter is required. All other parameters are optional.
- Example: To set a cookie named "UserVisit" in the visitor's browser that sets the value to the current date, and further sets the expiration to be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days)
<?php
$Month = 2592000 + time(); //this adds 30 days to the current time
setcookie(UserVisit, date("F jS - g:i a"), $Month);
?>
- To retrieve a cookie from the user's computer upon the next visit, call it with the following code:
<?php
if(isset($_COOKIE['UserVisit']))
{
$last = $_COOKIE['UserVisit'];
echo "Welcome back! You last visited on ". $last;
}
else
{
echo "Welcome to our site first time!";
}
?>
- This code first checks if the cookie exists. If it does, it welcomes the user back and announces when the user last visited. If the user is new, it prints a generic welcome message.
- To destroy a cookie, use setcookie() again but set the expiration date to be in the past:
<?php
$past = time() - 10; //this makes the time 10 seconds ago
setcookie(UserVisit, date("F jS - g:i a"), $past);
?>