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