SESSIONS IN PHP

  • A cookie recides on the users web Browser and may be suited for storing small amount of information and for limited concern on security.
  • When we need to store a considerable amount of information we use session and we have a Security advantage of session being stored on the web server itself which is more secure than a Cookie on the User’s Browser.
  • PHP allows us to use Sessions by a Session Variable which is a ‘Super Global’ , It holds information about a Single User.
  • Sessions create a Unique Id for each Visitor and stores variables based on this UID

SESSION START

  • First we must start up a Session before using it to store User’s Information
Session Start Snippet
<?php session_start(); ?>
<html>
<body>
</body>
</html>

SESSION VARIABLES

  • A Session variable is created by the variable $_SESSION
  • We can assign any name to a Session variable
       Session Variable Snippet
<?php
session_start();
// store session data
$_SESSION['name’]=”Sathesh”;
?>
<html>
<body>
<?php
//retrieve session data
echo "The Author of ScriptSnippets is=". $_SESSION['name'];
?>
</body>
</html>
Output:
The Author of ScriptSnippets is Sathesh

SESSION VARIABLES

  • A Session variable is created by the variable $_SESSION
  • We can assign any name to a Session variable
       Session Variable Snippet
<?php
session_start();
// store session data
$_SESSION['name’]=”Sathesh”;
?>
<html>
<body>
<?php
//retrieve session data
echo "The Author of ScriptSnippets is=". $_SESSION['name'];
?>
</body>
</html>

Output:

The Author of ScriptSnippets is Sathesh

SESSION DESTROY

  • If you want to unset a particular Session variable we can use the unset() function to remove the value from the session variable
Unsetting a variable Snippet
<?php
unset($_SESSION['name']);
?>
    • When you do not need any of your Session variable and our aim is to completely destroy the session then we use session_destroy() function.

Session Destroy Snippet

<?php
session_destroy();
?>
Thank u for reading this  Article hope it was useful !
-Regards
ScriptSnippets

About Sathesh Bm

Sathesh Bm has written 26 post in this website.

Engineer, Web Lover, Thinker, Blogger

Related Posts Plugin for WordPress, Blogger...