Home arrow PHP arrow Building a Logout Class

Building a Logout Class

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 class starts off with a constructor function that initializes some of the variables that will be used throughout the class. All of the classes that we will create throughout the next couple of articles will have a constructor function such as the ones we've seen so far. This is because this function basically sets up the database connection for the classes involved. The logout class has two functions, one of which is the constructor function and the other is the log_exit() function, that logs user exit time and date to the database:

<?php

class logout{

The constructor function has two sections. One deals with the database setup and the other simply sets up some variables for use in the class. All the global variables declared here are stored in the configuration file of the login class.  This excludes the $newid and $date_time values that are also shown. The class takes three parameters: the current date and time, the userid and the newid. Both the userid and newid will be stored during user login. Because the logout class simply updates an existing record, it requires these three parameters to accurately set the new values for the right user:

function logout($cdate,$uid,$nwid){
 
global $dbtype, $dbusername;
  global $dbpass, $dbhost,$newid;
  global $dbname, $dbtbl,$date_time;

We then link the database connection credentials and set up a database path or URL. This URL will then be used in subsequent connections to the database:

//set database connection details:
$this->dbtbl=$dbtbl;
$this ->dbp=$dbpath;
if($dbpath == null){
 
$this ->dbp=sprintf("%s://%s:%s@%s/%s", $dbtype, $dbusername, $dbpass, $dbhost,$dbname);
}

We then initialize some of the internal variables with the globally declared ones.  The newid variable, if you recall, was generated during login and is retrieved by the getnewid() function declared in the authorization class:

  //set logout status
  $this ->logoutstatus=FALSE;
  //set variables received from user
  $this->cdate=$date_time;
  $this->userid=$uid;
  $this->errmsg="";
  $this->newid=$nwid;
}

That's it for the login class. The next function will do the actual recording of the users' logout session.



 
 
>>> 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: