Loading...
 
Gmail / Facebook Style Chat Application


Posted: Monday 1st of October 2012

Creating Sessions with PHP



Sessions may refer to data transferred between two or more website pages. It could possibly refer to a variable that keeps its value even if you call it from a different file or page.
Lets say that we have a website and in the first page of the website which is normally the index.php file, we are asking for the username of the visitor who visited or logged into the system and we store the username of the logged in user into the variable $username.
e.g: $username = 'Emanuela'.

If the visitor navigates to another page, the value of the variable $username will be lost, so we would need to ask for the username on each page so in this case is way better to use sessions.

In order to use a session, the first thing to do is to start a session as shown below at the very top of your website page immediately after the openning of your php before any other variable.

Code:

<?php
session_start();
?>


AGAIN: The session_start(); function must be at the very beginning of your page, even before the <html> tag.
A session variable looks and works like an element of an associative array. For instance:
$_SESSION['username'] = 'Emanuela';
echo $_SESSION['username'];
And because sessions are keeping their value even if you go to another page. Example:

page_A.php
Code:

<?php
session_start(); //start a session
$_SESSION['username'] = 'Emanuela'; //Define session username as Emanuela
?>


page_B.php
Code:

<?php
session_start(); //start a session
if(isset($_SESSION['username'])) //Check if the session defined in page_A.php exist or not
{
      echo $_SESSION['username']; //Display the session username since its available
}
else
{
      echo 'No session found'; //Display error message if session username does not exist
}
?>


If the session username is found, it will output Emanuela

Lets say that we have a page that is displayed only to members. If somebody is logged in then, his or her username is stored in a session variable. In order to show the content of the page to the visitor, we need to check if the username session exists or not. This can be easily done using the below function:

Code:

<?php
isset($_SESSION['username']); //Returns True if session username exists and False if it does not
?>


So if we use the isset function our code will look like the one below:

Code:

<?php
session_start(); //start a session
if(isset($_SESSION['username'])) //Check if the session defined in page_A.php exist or not
{
      echo 'Welcome '.$_SESSION['username']; //Display the session username since its available
}
else
{
      echo 'No session is established yet, please login to proceed.'; //Display error message if session username does not exist
}
?>


When the user leaves your website or closes the browser, the sessions are automatically deleted, but if you want to delete a session your self then, use the following code:
unset($_SESSION['username']);
This is useful when you want to delete only a single session.
If you want to delete all the sessions in your broswer then, use the following code:
session_destroy();
NOTE: After this function you can not use more sessions on the page as all created sessions are destroyed.
To use any more sessions, you will need to create them again.
Example of using the session_destroy(); function:
Lets assume the below code is for your logout.php page

Code:

<?php
session_start(); //start a session before any other code
session_unset(); //unset the started session
session_destroy(); //Now destroy all created sessions
?>



By destroying the created sessions, the logged in user will not be logged out.


That's it guys...


Views Today: 0
Total Views: 804

Comments
0
OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Copyright © 2011 - 2024 | All Rights Reserved