PHP
  Home arrow PHP arrow Page 2 - Building a Logout Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building a Logout Class
By: Chris Neeman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-09-05


    Table of Contents:
  • Building a Logout Class
  • Recording the Logout Session
  • The Database Tables
  • Testing the Classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT