Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Session

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 211
    Comment on it

    when we work on an application ,the fist thing we do is open it we do some updates on it and then close it, this is similar than an session. the system know who we are,it knows every thing i.e when we start working on application, when we closed it. But on internet, the server does not recognize us who we are and what we want to do.

    This problem can be solved in php by using SESSION variable, It saves information or contain information about user and can be available for many pages

    Start a PHP Session

    In php we can start a session by using session_start() function.

    eg:-

     <?php
        //starting a session
    session_start();
    ?>
    <html>
    <body>
    
    <?php
    // Seting session variables
    $_SESSION["color"] = "red";
    $_SESSION["name"] = "ram";
    echo "Session variables are set.";
    ?>
    
    </body>
    </html>
    

    Get PHP Session Variable Values

    <?php
    session_start();
    ?>
    
    <html>
    <body>
    
    <?php
    
    echo "Favorite color is " . $_SESSION["color"] . ".<br>";
    echo "Favorite animal is " . $_SESSION["name"] . ".";
    ?>
    
    </body>
    </html> 
    

    Modify a PHP Session Variable

    <?php
    session_start();
    ?>
    
    <html>
    <body>
    
    <?php
    // for changing a session variable, we have to overwrite it
    $_SESSION["color"] = "green";
    print_r($_SESSION);
    ?>
    
    </body>
    </html> 
    

    Destroy a PHP Session

    session_unset() and session_destroy() are used for deleting all the session variables

    <?php
    session_start();
    ?>
    
    <html>
    <body>
    
    <?php
    // removing all session variables
    session_unset();
    
    // destroy the session
    session_destroy();
    ?>
    
    </body>
    </html> 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: