Starting a Session in PHP

Starting a Session in PHP

Starting a Session in PHP.  When you are opening , using and closing an application or a website, that counts as a session. But the system does not have any Idea , who is opening and closing an application or  a website .As the Web Browser or the internet does not store the User State Information.

So What is a Session ?

A session is a variable which store the data of the User, when  the user is opening a page or multiple page of a website.  The Duration of the Session time length can be set by the Webmaster. Each User will be given a Session name, and all activity by the User will be stored under this name.

Difference Between Session and Cookies

Sessions are stored in the User Browsers Devices, while sessions  are stored in the server. Thus the User will not be able to turn Off Sessions as it is being controlled on the server side.

Function involved to set a Session

Personally honestly speaking i do not remember all these function, I always refer back if it comes off my mind.

  1. $_SESSION[“”] – This is use to store or set the User Information. Check here
  2.  session_start(); –  This is use to Start a session. Check here
  3.  session_destroy(); – This is use to Destroy a session. Check here

Refer the Sample Code Below

Example 1 

 


<?php

session_start();

echo 'Welcome to my page';

$_SESSION['User'] = 'User 1';
$_SESSION['Surfing']   = 'PHP';
$_SESSION['time']     = time();


echo '<br /><a href="print20.php">page 2</a>';


echo '<br /><a href="print20.php?' . SID . '">page 2</a>';


?>



 

Example 2

 

<?php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['User']; 
echo $_SESSION['Surfing'];  
echo date('Y m d H:i:s', $_SESSION['time']);


echo '<br /><a href="print19.php">page 1</a>';

?>


 
Well that warp up on how to Start a Sessions. Learning how to program a session is an asset to PHP programmer. Through Session Webmaster can gather useful analytics from the User,and use it for marketing and generating more leads to the Company.

Check out how to Set Cookies in PHP Here

Leave a Comment

sixteen + fifteen =