Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Session Management and Authentication with PHPLIB
Now let's take a look at what the features do.

Authentication

PHP uses an authentication mechanism that is more flexible than basic authentication, as well as being reasonably secure. Here's how it works. At the top of your protected page, you have a page_open call which contains the statement "auth" => "auth_class" where auth_class is the name of your authentication class. The page_open function evaluates this, and initializes the authentication component. Authentication first checks to see if the user is already authenticated.

Let's assume that our user just arrived, and is not yet authenticated. PHPLIB will present the user with a login form (no popup windows!!!), which you may design yourself or use the included one. The user enters their username and password, and clicks submit. Simple, eh? What goes on behind the scenes is a bit more complicated...

If the user does not have a JavaScript capable browser, then authentication works much like you would suspect. The username and password are sent to the server, and compared against the values stored in the database. If the user does have a JavaScript capable browser, it's a bit different. PHPLIB will put a string in the form called a "challenge". When the user submits the form, their username, password, and the challenge are encrypted using an md5 encryption algorithm. The only thing that is passed back to the server is the username, and this encrypted hash (password is NOT transmitted). The server then takes the same challenge string, the username submitted, and the password associated with that username in the database. These values are also encrypted with md5. The two encrypted strings are then compared. If they match, the user submitted the correct password, and is allowed to proceed. So, the user is authenticated with the password never having been transmitted, so it cannot be sniffed. Very slick.

Session Management

Authentication ties in very closely with session management. Once the user is authenticated, their session begins. If the user has a browser that supports cookies, a session id is created by making an md5 hash of a php uniquid (a random id based on system time) and an arbitrary string. The cookie is a session cookie, meaning that it is never written to the user's hard disk, and it is removed once the session ends. If they do not support cookies, the same session id is stored as a get parameter in the URL.The md5 hash is done so that the session id is random, and it is not possible to guess one session id based on knowledge of another one. The session id is used to store information about the user in the system. This information can include if the user is authenticated, when their authentication exprires (Yep, you can do expiration!), what permissions they have (coming later), and any other information you want.

Aside: PHP and Object Oriented Programming
PHPLIB uses PHP's OOP feature extensively. This may be the first time you've seen it in use, so it's going to help a lot if you have an idea of what it all means. The first step is to not really think of it as object oriented. Rather, it's a way to group together data and functions for convenient access. It does not follow many OO conventions, and it is not truly object oriented. With that said, let's look at the database class to see what it means.
<?php $db = new MyDBClass; ?>
This line creates a new database object. Think of it as initializing all the functions in MyDBClass so that you can use them. $db is then a link to this database object. In a real OO language, creating this object would simply allocate memory for the data and functions. With PHP, what it does is essentially set up the object (in this case, basically set up the database connection, and select the database). To do anything useful with this object, you evoke methods on it. Methods are simply functions that manipulate an object's data. For example,
<?php $db->query($query); ?>
means to evoke the function query on the database object $db. The query method takes the string you pass it, and uses the data in the database object to execute that query. The -> symbol may confuse you a bit. In C, -> means to access the data of a pointer to a structure. In PHP, it means that the method on the right is called on the object on the left. Here, query is just one of the functions in the database class. All the other functions in that class can be accessed in the same way.
Now, when you set up your local.inc file, you do so by extending classes. Extending is an OO word for adding functionality to things. When you extend a class, all the functions and data in the class you are extending (called the super class) is available to the extending class (called the sub class). Your subclass can add functionality to the superclass, and it can also overwrite data and methods in the superclass. Let's take the database example. Here is a subclass of a database class:


class MyDBClass extends DB_Sql {
    $Host     = "localhost";
    $Database = "some_database";
    $User     = "some_db_user";
    $Password = "db_user_password";
}

Here we are creating a class called MyDBClass. This class can do everything that the database class can do. However, it has replaced some of the data of the database class with some of it's own. If you look in DB_Sql, you'll see the variables above are empty. The subclass provides these values, and it is the subclass you will use to create your database objects. You can call all the methods of DB_Sql on MyDBClass, because it is merely an extension. Think of it as MyDBClass IS a DB_Sql object with added functionality and/or data.
Next Page

[Page 1]  [Page 2]  


Comments:
RE: The specified procedure could not be foundchristo09/26/05 06:25
RE: How to use session control in php4 ?xcore09/15/05 22:40
SESSION - PHP WIN-XPARI POLINSKY03/04/05 21:30
php session erroryuhtwe12/29/04 02:56
RE: session variables not workingtmal512/12/04 10:20
RE: WRONG!!!!Robin Percy12/04/04 04:07
RE: session variables not workingvarna12/02/04 00:04
RE: Can't get values of session variablesbenji12/18/02 01:26
RE: how to pass variables in to other filesmetador12/12/02 15:06
session managementravi10/02/02 03:38
session variables not workingkiran09/19/02 03:48
Can't get values of session variablesToberius09/01/02 17:16
RE: Cannot get session variableWasif Zaidi of Pakistan08/23/02 00:22
RE: how to pass variables in to other filesWasif08/22/02 05:04
RE: how to pass variables in to other filesGreg08/20/02 08:19
RE: PHP Session variablesGreg08/20/02 08:15
PHP4 session securityBowie07/15/02 22:08
RE: PHP Session variablesKass07/13/02 05:11
how to pass variables in to other filessamant07/12/02 12:24
RE: PHP Session variablessamant07/12/02 12:22
RE: Cannot get session variablejhun05/09/02 00:31
Cannot get session variableMelody04/16/02 22:08
phplib configurationJarry04/12/02 17:04
phpib, help me pleaseJarry04/12/02 16:58
sessionShriya04/10/02 06:40
RE: PHP Session variableskandarp bhatt03/27/02 04:24
WhoIsOnline with PHPLibtc7703/06/02 07:37
Import to PostgreSQLRadek02/28/02 17:55
RE: getting warning message in sessionmontino02/22/02 08:32
WRONG!!!mcq01/31/02 21:02
getting warning message in sessionTuan01/22/02 12:53
RE: php sesson errorUttam12/24/01 21:22
RE: no sessions workingamitabh12/24/01 03:52
php sesson errorPagla12/19/01 15:17
PHP Session variablesBalaji12/19/01 00:47
Use of phplib authentication - browser closeJohn Lodge12/13/01 15:16
RE: How to detect Browser name using PHP?Cameron Green11/13/01 21:32
How to detect Browser name using PHP?Hitesh Patel10/23/01 06:16
The specified procedure could not be foundSunder Rajendran10/22/01 15:36
Sniffing/ PHPLIB SecurityCameron Green10/04/01 04:06
GET is overiding CookieDaniel09/12/01 06:17
RE: PHP Session variables and scalabiltyDouglas Forrest07/08/01 18:05
RE: not working with GETJim Henderson06/03/01 19:22
PHP Session variables and scalabiltyScot Braze05/24/01 08:33
Problem with PHPLIB sessions....Ekku05/21/01 04:09
RE: not working with GETVivek04/25/01 08:51
More details........GEP04/20/01 00:59
How to use session control in php4 ?Arm04/17/01 03:36
spellchecksomeone03/30/01 09:12
GET uses CookiesLars Vagt03/25/01 18:08
RE: not working with GETRichard Church02/09/01 14:00
ThanksLouis Zezeran12/03/00 00:21
PHPLib SlaveryAnderson Fortaleza11/21/00 11:07
Starting out....james maina08/22/00 13:15
this actually works..stephane schmit08/22/00 03:43
RE: not working with GETstephane schmit08/17/00 10:05
not working with GETDaan07/24/00 12:44
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.