Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save session in databse using php?

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 170
    Comment on it

    Saving session in database is best practice for website security. so if we want more security then we need to save session in database.
    Here are few steps to save session in database

    step 1--> Create a table name as sessionsdata

    step 2--> create a function for open database connection

    1. function _open()
    2. {
    3. global $_sess_db;
    4.  
    5. $db_user = $_SERVER['DB_USER'];
    6. $db_pass = $_SERVER['DB_PASS'];
    7. $db_host = 'localhost';
    8.  
    9. if ($_sess_db = mysql_connect($db_host, $db_user, $db_pass))
    10. {
    11. return mysql_select_db('sessions', $_sess_db);
    12. }
    13.  
    14. return FALSE;
    15. }

    step 3--> Function for close connection

    1. function _close()
    2. {
    3. global $_sess_db;
    4.  
    5. return mysql_close($_sess_db);
    6. }

    Step 4--> Function for read session from database

    1. function _read($id)
    2. {
    3. global $_sess_db;
    4.  
    5. $id = mysql_real_escape_string($id);
    6.  
    7. $sql = "SELECT data
    8. FROM sessionsdata
    9. WHERE id = '$id'";
    10.  
    11. if ($result = mysql_query($sql, $_sess_db))
    12. {
    13. if (mysql_num_rows($result))
    14. {
    15. $record = mysql_fetch_assoc($result);
    16.  
    17. return $record['data'];
    18. }
    19. }
    20.  
    21. return '';
    22. }

    Step 5--> write session data to database

    1. function _write($id, $data)
    2. {
    3. global $_sess_db;
    4.  
    5. $access = time();
    6.  
    7. $id = mysql_real_escape_string($id);
    8. $access = mysql_real_escape_string($access);
    9. $data = mysql_real_escape_string($data);
    10.  
    11. $sql = "REPLACE
    12. INTO sessionsdata
    13. VALUES ('$id', '$access', '$data')";
    14.  
    15. return mysql_query($sql, $_sess_db);
    16. }

    step 6--> Destroy database

    1. function _destroy($id)
    2. {
    3. global $_sess_db;
    4.  
    5. $id = mysql_real_escape_string($id);
    6.  
    7. $sql = "DELETE
    8. FROM sessionsdata
    9. WHERE id = '$id'";
    10.  
    11. return mysql_query($sql, $_sess_db);
    12. }

    step 7--> Create a _clean function for remove Garbage data

    1. function _clean($max)
    2. {
    3. global $_sess_db;
    4.  
    5. $old = time() - $max;
    6. $old = mysql_real_escape_string($old);
    7.  
    8. $sql = "DELETE
    9. FROM sessionsdata
    10. WHERE access < '$old'";
    11.  
    12. return mysql_query($sql, $_sess_db);
    13. }

    Now call your session handler function

    1. session_set_save_handler('_open',
    2. '_close',
    3. '_read',
    4. '_write',
    5. '_destroy',
    6. '_clean');

 2 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: