Home arrow PHP arrow Page 4 - Application Framework Components: Login/Logout

The Remaining Login Class Functions - PHP

The first of the Framework components that we are going to be looking at is central authorization. This component contains two classes, the login and logout classes. Not only do these classes log a user in and out, they also record the times and dates that a user logged in and logged out.

TABLE OF CONTENTS:
  1. Application Framework Components: Login/Logout
  2. Login Class Functions
  3. Setting up the SQL Query
  4. The Remaining Login Class Functions
By: Chris Neeman
Rating: starstarstarstarstar / 10
September 04, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The remaining seven functions simply retrieve individual bits of information based on the outcome of the check_user () function. If you go back to the function, you will notice that we stored some global variables and assigned values from the SQL query. Those values can now be retrieved by the program as and when needed:

This function retrieves the user id:

function getid(){
 
return $this->userid;
}

This function retrieves the user name:

function getname(){
 
return $this->uname;
}

This function retrieves the user access level:

function getlevel(){
 
return $this->access;
}

Retrieves the error message:

function geterr(){
 
return $this->errmsg;
}

This function retrieves the department id of the user concerned:

function getdepid(){
 
return $this->did;
}

This function retrieves the new user id:

function getnewid(){
 
return $this->nid;
}

This function retrieves the user email address:

function getemail(){
 
return $this->em;
}

These seven functions will be used to set up the sessions for the user at login. The next function simply logs the time and date that the user logged in.

First we set some global variables:

function log_access(){
 
global $uid,$nid;

Then we connect to the database server with the database path that I provided:

//connect to the db server with the user provided dbpath
$dbcon = new DBAL($this->dbp);

We then build the SQL query. The query simply inserts the date and time of login for a particular user. To make sure that the log in date and time is inserted for the correct user, the userid variable is used. If this userid does not exists, the query will return an error:

$query_ins = "INSERT INTO logtbl SET u_id='".$this->userid."',
start_sess='".$this->dt."',end_sess='0000-00-00 00:00:00',duration='0'";

The query is then executed, like so:

$result =$dbcon->a_query($query_ins);

If the result of the query is false, we store  the error message as shown below:

if(!$result){
 
$this->errmsg=$dbcon->showError() . "n";
 
return FALSE;

Otherwise, if the result of the query is true, then we collect the new id of the record that we just inserted. This id will then be used when we log out the user:

    }else {
     
$this->nid=mysql_insert_id();
      
$dbcon->disconnect();
     
return TRUE;
    }
  }
}  //end class
?>

That covers the login class. Below is an outline of the configuration file for the login class. Some of the database variables that we used in the constructor function are stored here:

<?php
//db connection creds
$dbtype = 'mysql';
$dbhost = 'localhost';
$dbname = 'intranet';
$dbtbl = 'users';
$dbusername = 'root';
$dbpass = '';
$dbpath = "$dbtype://$dbusername:$dbpass@$dbhost/$dbname";
$date_time=date("Y-m-d G:i:s");
?>

Hopefully some of the code in the constructor function will begin to make sense to you. The configuration information above sets up various variables that are used in the login and logout classes.

Conclusion

In the follow-up article will look at the logout class before writing a test application to test the login class. We will also discuss the database tables that are going to store the information about intranet users. Both of these tables have very simple structures, but are very effective in their functions. The logout class will also contain a function to record the time and date that the user is going to log out. We will look at this function in great detail.



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