Home arrow PHP arrow Page 2 - Building a Logout Class

Recording the Logout Session - PHP

The main function of the logout class is to log out a user and then redirect the user to the login page. On a programmatic level, the logout class terminates and destroys the session variables that the login class created for the user and also logs the exit time to the database. This article will show you how to create a logout class. It is part of a series on application framework components.

TABLE OF CONTENTS:
  1. Building a Logout Class
  2. Recording the Logout Session
  3. The Database Tables
  4. Testing the Classes
By: Chris Neeman
Rating: starstarstarstarstar / 6
September 05, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The function does not take any parameters, but uses those declared and initialized in the constructor function:

  function log_exit(){
   
//connect to the db server with the user provided dbpath
   
$dbcon = new DBAL($this->dbp);
   
//update table with logout date
   
$query_updt = "UPDATE logtbl SET end_sess='".$this->cdate."'
WHERE u_id='".$this->userid."' AND logid='".$this->newid."'";
   
$result_updt =$dbcon->a_query($query_updt);
   
if(!$result_updt){
     
$this->errmsg="UPDATE query for the end_sess column
returned the following error: ".$dbcon->showError() . "n";
   
}else{
     
//subtract the dates and update the duration column
     
$sub = "SELECT date_sub(start_sess, INTERVAL end_sess  HOURS.MICROSECONDS ) as dur from logtbl WHERE u_id='".$this-
>userid."' AND logid='".$this->newid."'";
     
$result=$dbcon->a_query($sub);
     
if($result){
       
//fetch the dur row that has been returned
       
$row=$result->fetchrow();
       
//update the duration column...
       
$q_update = "UPDATE logtbl SET duration = '".$row->dur."'
WHERE u_id='".$this->userid."' AND logid='".$this->newid."'";
       
$result_q=$dbcon->a_query($q_update);
       
if(!$result_q){
         
$this->errmsg="UPDATE query for the duration column
returned the following error: ".$dbcon->showError() . "n";
       
}
     
}
   
}//first else
  }//endfunction
}//end class
?>

The fist couple of lines in the function connect to the database.

//connect to the db server with the user provided dbpath
$dbcon = new DBAL($this->dbp);

Then it sets up the first of three SQL queries. The query is basically an update query that adds today's date and time to the user with the given userid:

//update table with logout date
$query_updt = "UPDATE logtbl SET end_sess='".$this->cdate."' WHERE u_id='".$this->userid."' AND logid='".$this->newid."'";
$result_updt =$dbcon->a_query($query_updt);

After executing the query, the code checks to see if the result that is returned is false. If so, a very elaborate error message is generated, stating which query caused the error since there are three of them in this function:

if(!$result_updt){
 
$this->errmsg="UPDATE query for the end_sess column returned the following error: ".$dbcon->showError() . "n";

If the update query result is true, then the next query is executed. This query's aim is to calculate the time difference between the two datetime columns and store the result in a row called dur. To make sure that the right date time column values are retrieved, the query uses the WHERE clause in which it specifies which user and logid should be checked. The user is identified by the userid variable and the logid identifies the precise record in the log table that should be checked:

}else{
 
//subtract the dates and update the duration column
 
$sub = "SELECT date_sub(start_sess, INTERVAL end_sess  HOURS.MICROSECONDS ) as dur from logtbl WHERE u_id='".$this-
>userid."' AND logid='".$this->newid."'";
 
$result=$dbcon->a_query($sub);

If the result of the query is true, then the duration column of the log table is updated. Again the userid and log table id are used to make sure that the correct row is updated:

if($result){
 
//fetch the dur row that has been returned
 
$row=$result->fetchrow();
 
//update the duration column...
 
$q_update = "UPDATE logtbl SET duration = '".$row->dur."' WHERE
u_id='".$this->userid."' AND logid='".$this->newid."'";
 
$result_q=$dbcon->a_query($q_update);

If the user or log id was not found, the following error message is stored in the errmsg variable:

    if(!$result_q){
 
$this->errmsg="UPDATE query for the duration column returned
the following error: ".$dbcon->showError() . "n";
      }
    }
  }//first else
}//endfunction

Just a quick word of caution: I've used a function called DATE_SUB() that might not be available in your version of MYSQL, so check to make sure that it is available before running this code. I believe DATE_SUB() is also vendor specific, so it might not work in other database environments.

That's all there is to the logout class.  Next we will look at the database tables behind the two classes.



 
 
>>> More PHP Articles          >>> More By Chris Neeman
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: