Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using database session handling in Code PHP

    • 0
    • 2
    • 2
    • 2
    • 1
    • 0
    • 0
    • 0
    • 1.11k
    Comment on it

    PHP is one of the scripting language. In which session handling is one of the key thing mostly using in web application.

    Suppose you build a website and allow to login everyone in website, You need to track user every step until they log out our system, Its called Session tracking.

    Now the question is why we need to track the session, answer is very simple .HTTP state less protocol, When you refresh you page, Its lost every information, So this is the reason we need session handling.

    To handle session in php we need to a global variable $_SESSION and session_start()

    <?php
        // Start the session
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    // Set session variables
    $_SESSION["favcolor"] = "green";
    $_SESSION["favanimal"] = "cat";
    echo "Session variables are set.";
    ?>
    </body>
    </html>
    

    Here above, we have set two session variable $_SESSION[favcolor] and $_SESSION[favanimal], Now we can access these variable thought the website and get corresponding value.

    Another thing, if you want to destory session you need to use bellow methods.

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

    Setting up storing Sessions in a database
    I was first introduced to storing Sessions in a database. So in order to store Session data in a database, were need a table.
    Run the following SQL in your database to generate the table. Im using MySQL, but hopefully the following should be enough to get you started.

    CREATE TABLE IF NOT EXISTS `sessions` (
      `id` varchar(32) NOT NULL,
      `access` int(10) unsigned DEFAULT NULL,
      `data` text,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

 1 Comment(s)

  • You can use Redis as a PHP session handler to do this without this much effort. With redis, you only need to add the following to implement redis on your PHP site.
     
    <?php
    
    ini_set('session.save_handler', 'redis');
    
    ini_set('session.save_path', "tcp://localhost:6379");
    
    //echo ini_get('session.save_path');
    
    session_start();
    
    $count = isset($_SESSION['count']) ? $_SESSION['count'] : 1;
    
    echo $count;
    
    $_SESSION['count'] = ++$count;
    
    ?>
     
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: