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.
The class has nine functions. Let's take a closer look, starting with the first one.
function authorization($email=null,$dbpath=null,$pass=null){ global $dbtype, $dbusername; global $dbpass, $dbhost; global $dbname, $dbtbl,$date_time; //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); } //set login status $this ->loginstatus=FALSE; //set variables received from user $this->email=$email; $this->password=$pass; $this->errmsg="Your login details could not be found. Please try again."; //set date time value $this->dt=$date_time; }
The first function is what we call a constructor function. Its main aim is to initialize some of the variables that are going to be used throughout the class. This function has the same name as the class itself and takes three parameters: the email address, database path and pass (shortened for password). So whenever the class is used, it will need these three parameters to be passed. Two of these parameters, as you've probably worked out by now, are supplied by the user during login. You will also notice some global variables that I've declared:
global $dbtype, $dbusername; global $dbpass, $dbhost; global $dbname, $dbtbl,$date_time;
Most of these deal with the database abstraction layer that we haven't discussed yet, so don't worry if you do not understand it. The next bit of the function simply sets up the user supplied and internal variables:
//set login status $this ->loginstatus=FALSE; //set variables received from user $this->email=$email; $this->password=$pass; $this->errmsg="Your login details could not be found. Please try again."; //set date time value $this->dt=$date_time;
The $this->errmsg variable will of course be overridden depending on the kind of error generated. The check_user() function is at the heart of the authorization class. It does the job of checking to see if a user exists:
First we declare a couple of global variables:
function check_user(){ global $userid,$uname,$access,$did,$err,$em;
Then we connect to the database server -- again, don't worry if you don't understand this part, we will get to it a bit later on. Notice that to connect to the database server you will need a URL coming in the form of dbpath that we initialized in the constructor function:
//connect to the db server with the user provided dbpath $dbcon = new DBAL($this->dbp);