如何在会话到期时自动删除表的所有记录

Could you tell me how delete all the records from the following table when the user session expires or the user close the browser.

tablename: test

id  sessions_id     value
1    admin_1         12 
2    admin_1         12 

I know about Temporary table, but I don't want to use it... because I cannot fetch data after sometime I create the table.

Thanks :)

What I have tried:

 function logout(){

   //My query to delete the table
    $this->session->sess_destroy();
   redirect('login');

  }

the above solution works fine but I was wondering what would happen if any user didn't logout..just simply closed the window.

Try this:

  • add a timestamp column to your table
  • on every page, UPDATE this column with the current timestamp for every row related to the current user
  • use a cron calling a PHP script in which every hour you DELETE the rows where the timestamp is too old.

Have a quick read of http://www.tonymarston.net/php-mysql/session-handler.html. It explains how to save sessions in a database and gives you a complete solution.

   <?php 

  // $sessions_id is your session id

      if(empty($sessions_id) == true ){

    //execute query 

      // for ex   delete from test where sessions_id = '$sessions_id';     



     }



     ?>