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.18k
    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()

    1. <?php
    2. // Start the session
    3. session_start();
    4. ?>
    5. <!DOCTYPE html>
    6. <html>
    7. <body>
    8.  
    9. <?php
    10. // Set session variables
    11. $_SESSION["favcolor"] = "green";
    12. $_SESSION["favanimal"] = "cat";
    13. echo "Session variables are set.";
    14. ?>
    15. </body>
    16. </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.

    1. <?php
    2. session_start();
    3. ?>
    4. <!DOCTYPE html>
    5. <html>
    6. <body>
    7.  
    8. <?php
    9. // remove all session variables
    10. session_unset();
    11.  
    12. // destroy the session
    13. session_destroy();
    14. ?>

    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.

    1. CREATE TABLE IF NOT EXISTS `sessions` (
    2.   `id` varchar(32) NOT NULL,
    3.   `access` int(10) unsigned DEFAULT NULL,
    4.   `data` text,
    5.   PRIMARY KEY (`id`)
    6. ) 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
Reset Password
Fill out the form below and reset your password: