SESSION IN PHP
SESSIONS IN PHP (PART-1)
SESSIONS IN PHP (PART-2)

PHP Session

  • In PHP, a session is a way to store information (in variables) to be used across multiple pages. ,
  • Web browsers and Web servers have a stateless interaction and do not maintain track of user sessions
  • HTTP protocol
    • Enables Web browsers to communicate with Web servers
    • Has no methods or functions to maintain the status of a particular user

WHAT IS A PHP SESSION?

  • In PHP, When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the Internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn’t maintain state.
  • In PHP, Session variables solve this problem by storing user information to be used across multiple pages (e.g username, favorite color, etc). By default, session variables last until the user closes the browser.
  • So; Session variables hold information about one single user, and are available to all pages in one application.

START A PHP SESSION

  • In PHP, A session is started with the session_start() function.
  • In PHP, Session variables are set with the PHP global variable:
  • $_SESSION.
  • Note: The session_start() function must be the very first thing in your document. Before any HTML tags.

DESTROY A PHP SESSION

  • To remove all global session variables and destroy the session,
  • use
    • session_unset() — remove all session variables
    • session_destroy() — destroy the session

Session In PHP

  • In PHP, Web sites that cannot depend on HTTP or Web servers for complex user interaction need session tracking
  • Refers to the total time the user accesses information on a particular Web site before exiting the Web site
  • Manages data for a particular user in a specific session
  • Enable distinguishing user specific information for the entire duration of the session

IMPORTANCE OF A SESSION

  • Consider an example, in a particular Web site, the user has to first register and then log on to access any information lit
  • For such authentication procedures, the state of the user has to be maintained across the Web site
  • Web sites traditionally use GET and POST methods to pass user information from one script to another
  • When these methods are used, PHP assigns user information variables in the following format:
  • $name = $ GET [ ‘ name 1;
  • In the code, the variable $name stores the value that the script retrieves from the HTML form
  • This process of transferring user information is time consuming and not essential for large Web sites

Source Code

page1.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="">
        <label for="">Username</label>
        <input type="text" name="username">
        <br>
        <label for="">Password</label>
        <input type="text" name="password">
        <br>
        <input type="submit" value="Submit" name="SubmitBtn">
    </form>
    
 <?php 
    session_start();
    if(isset($_REQUEST['SubmitBtn']))
    {
        if($_REQUEST['username'] == "Admin" && $_REQUEST['password'] == "Admin123")
        {
            $_SESSION['username'] = $_REQUEST['username'];
            header('location:page2.php');
        }
        else
        {
            echo "login failed";
        }
    }
    ?>
    
</body>
</html>

page2.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <?php
    session_start();
    
    if(isset($_SESSION['username']))
    {
        echo "Welcome " . $_SESSION['username'];
    }
    else
    {
        header('location:page1.php');
    }
    ?>
    
    <a href="page3.php">Go to page 3</a>
</body>
</html>

page3.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <?php
   
    session_start();
    
    if(isset($_SESSION['username']))
    {
        echo "Welcome " . $_SESSION['username'];
    }
    else
    {
        header('location:page1.php');
    }
  
    ?>
    <a href="logout.php">Logout</a>
</body>
</html>

logout.php

<?php

session_start();
session_destroy();
header('location:page1.php');

?>

Download Notes Of This Blog From the link given below

https://www.mediafire.com/file/l2o98jzwzem9yzh/SESSIONS+IN+PHP.pptx/file

Download Source Code Of This Blog From the link given below

https://www.mediafire.com/file/199ho3c6ad6j7n7/web.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *