Building a Logout Class - Recording the Logout Session
(Page 2 of 4 )
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.
Next: The Database Tables >>
More PHP Articles
More By Chris Neeman