Session Management in PHP

 Managing Sessions and Using Session Variables

 

coding-zon-php-sessions-demo

 

 

What is  session?

  • Session is the time spent by a single user on a website.
  •  Session can't be shared by any other users. Ex: Login to website.
  •  When a session is created, One unique-id is provided for each user at the time of session
    start. Ex: sess_07ne2iojd9983gl3ic2a2e06q0
  •  Session can create user specific variables.
  •  These variables are stored at server side. Whereas cookies are stored at client side.
  •  Session-ids available in WAMP/XMPP’s tmp directory.
  • Data available in session accessed by all the pages in a web application.

 

Creating a Session and Retrieving Session ID

session_start() :  Creates a new session and generates a session ID.
session_id()     :  Displays session ID.



session_create.php

 
<?php
 
session_start();
echo session_id();
 
?>
 
output:
sess_07ne2iojd9983gl3ic2a2e06q0 

Creating Session Variables and Reading Session Variables:

 

$_SESSION['user']="John";
echo "<br>Welcome..Mr ". $_SESSION['user'];
 
add above 2 lines in session_create.php and run it. 
 
Example
 
<?php
 
session_start();
echo session_id();

$_SESSION['user']="John";
echo "<br>Welcome..Mr ". $_SESSION['user'];

?>
 
 
** session variable, once it is created, can be accessed from any page in 
the web application.
 
** In every page, first line should be session_start() so that, session is 
continued to the next page. And session variables can be accessed by that page.
 
** Session Variables available within session. Once the Session destroyed 
all the variables get cleared from the session.

Clearing session variables and destroying a Session:

session_unset() : clears all session variables

session_variable_unset.php

<?php
 
session_start();
 
unset($_SESSION['user']);
 
echo "<br>Hello ". $_SESSION['user']; 
 
?>

Output

Gives Unified variable error
 
Counting page views using session variable
 
  
Example:
 

session_views.php

 
<?php
 
session_start();
 
if(isset($_SESSION['viewscount']))
 
$_SESSION['viewscount']=$_SESSION['viewscount']+1;
 
else
 
$_SESSION['viewscount']=1;
 
echo "<br><h2>This website Viewed=". $_SESSION['viewscount']."times";
 
?> 
 
 Retrieving session data from another page in a website.
 

session_page1.php  

 
<?php
 
session_start();
 
$user['username'] = "John Doe";
$user['mobile'] = "4334523455";
$user['gender'] = "Male";
$user['email'] = "john123@example.com";
 
$_SESSION['user'] = $user;
 
print "<a href=\"session_page2.php\" > Move to Page2 </a>";
 
?> 
 

session_page2.php

 
<?php
 
session_start();
 
echo $_SESSION['user']['username']."<br>";
echo $_SESSION['user']['mobile']."<br>";
echo $_SESSION['user']['gender']."<br>";
echo $_SESSION['user']['email']."<br>";
 
?> 
 
Ref: http://php.net/manual/en/function.session-start.php
To find more on  sessions.
 

session_info.php

 
<?php
 
session_start();
 
echo "<b>Few Important Session functions are as follows:</b><br/>";
 
echo "<b>Session ID is:</b>".session_id()."<br/>";
 
echo "<b>Session module name is:</b>".session_module_name()."<br/>";
 
echo "<b>Session path is:</b>".session_save_path()."<br/>";
 
echo "<b>Session name is:</b>".session_name()."<br/>";
 
?>


What you learned in this tutorial blog.


How to pass form variables from one page to other page in PHP ?

How to Send Submissions to Your MySQL Database Using PHP
How to pass form variables from one page to other page in PHP ?
How to Create, Access and Destroy Sessions in PHP
how can you share data between PHP pages?



Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP