Welcome. Here we'll discuss how to make a basic authentication system that can be used on a web site, web script, or applications.
If a high security environment is required this may not be the right solution for you, but if you're looking for an easy way to add
authentication and rights you should find this tutorial to be very useful.
We will need a database table to hold the users information. For this example we will be using MySQL, but it could easily be adapted for
other databases.
Let's make the table. You can either manually create the table or use a utility like phpMyAdmin to import the table. Create the table matching the MySQL code below.
CREATE TABLE authUsers (
ID int(10) NOT NULL AUTO_INCREMENT,
timestamp int(12) NOT NULL DEFAULT '1202175463',
fullName varchar(60) NOT NULL DEFAULT '',
username varchar(60) NOT NULL DEFAULT '',
password varchar(60) NOT NULL DEFAULT '',
email text NOT NULL,
isSuperUser SET('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (ID)
) TYPE=MyISAM ;
Now that we've created the authUsers table let's create the login file. Create a file on your server and name it login.inc.php. This will be the include file
used to check if a user is logged in. You will include this file on all pages that need authentication. We'll talk more about this later.
At the top of the login.inc.php file we just created, add the following code to start the php session handler. We will be using sessions to track if a user is logged in or not.
<?php
session_start();
?>
The variables below are settings that will be used to configure this login script. The host, user, pass and database settings are your database connection settings. Change the values to match your mysql settings.
$cfg['host'] = "localhost";
$cfg['user'] = "db_username";
$cfg['pass'] = "db_password";
$cfg['database'] = "db_name";
$cfg['usersTable'] = "authUsers";
Now lets add under the database settings a couple other settings we'll need later. Right under the database setting add the following variables.
$cfg['onSuccess'] = "./secure.php";
$cfg['onCancel'] = "./cancel.php";
The onSuccess setting is the location the user is sent to when successfully logged in, and onCancel is the location the user is sent if they click the cancel button or link.
And now to connect to the database add the code below. Notice the host, user, and pass variables are used here.
mysql_connect($cfg['host'],$cfg['user'],$cfg['pass']);
mysql_select_db($cfg['database']);