Google News
logo
PHP Sessions

What is a PHP Session?

PHP session supports in consists of a way to preserve certain data across subsequent accesses.

A visitor accessing your web site or web application is assigned a unique id,  session id. This is either stored in a cookie on the user side or is propagated in the URL.

PHP session is a way of storing information in session variables, which could be used across multiple web pages for authentication. Unlike a cookie, the information is not stored on the user’s computer instead a session creates a file on the server, in a temporary directory, where it stores information in session variables. This stored information for a session will be available to all the web pages on the site during navigation. On the server, the location of a temporary file is determined by a setting in the php.ini file called session.save_path.

  • Sessions have the capacity to store relatively large data compared to cookies.
  • Server sends a cookie known as PHPSESSID to the user’s machine to store unique session identification string.
  • Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, color, etc). By default, session variables last until the user closes the browser.

Start a PHP Session

A PHP session is easily started by making a call to the session_start() function. This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.

Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.

The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.
Example :
<?php  
	session_start();  
?>  
<!DOCTYPE html>
<html>
<head>
    <title>PHP Sessions</title>
</head>
<body>

<?php  
	$_SESSION["user"] = "FTL";  
	echo "Session are set successfully..!<br/>";  
?>  
<h2><a href="http://www.freetimelearning.com/php/php-sessions.php" target="_blank">PHP Sessions</a> </h2>

</body>
</html>
Output :

PHP Session Counter

The PHP Session is mainly use of isset() function to check if session variable is already set or not set.

<?php
   isset($_SESSION['counter']);
?>

The following example are php session counter :

Example :
<?php
   session_start();
   if( isset( $_SESSION['count'] ) ) {
      $_SESSION['count'] += 1;
   }else {
      $_SESSION['count'] = 1;
   }
   $message = "This page total visitors are : ".  $_SESSION['count'];
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Session Counter</title>
</head>
   
<body>

      <?php  echo ( $message ); ?>

</body>
</html>
Output :

PHP Session Destroy

PHP session_destroy() function is used to destroy all session variables completely. This function is mainly used logout (or) signout pages.

Example :
<?php
   session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Session Destroy</title>
</head>
   
<body>

 <?php
	session_destroy(); 
 ?>

</body>
</html>