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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway