This is a simple and fast user authentication. Not adviced for commercial sites. Best for learning purposes. Check examples for usage.
<?php
/***************************************************************
** User authentication Class
** class Auth
**
** Author...: leapinglangoor [ leapinglangoor@yahoo.co.in ]
** Updated..: 21 Apr 2005
** Version..: v 1.2
**
***************************************************************/
class Auth
{
// Mysql Server details
// Change the respective values here
var $db['host'] = 'localhost'; // Server Host name
var $db['database'] = 'login'; // Make sure the db exists first
var $db['user'] = 'root'; // Username for mysql
var $db['pass'] = ''; // Password for the user
/***************************************************************
** Function: connect();
**
** Description: Connect to the mysql server
**
** Usage: Only used inside this class. But can be used outside this class aswell
***************************************************************/
/***************************************************************
** Function: install
**
** Description: Installs the table required for the login script
**
** Usage: Only used outside the class.
***************************************************************/
/***************************************************************
** Function: encrypt
**
** Description: Encrypt any username/Passwd
**
** Usage: Only used inside this class. But can be used outside this class aswell
***************************************************************/
/***************************************************************
** Function: Add_user
**
** Description: Add a user to the database
**
** Usage: Only used outside the class
***************************************************************/
$sql = "insert into logins values ('$username', '$password')";
mysql_query( $sql ) or die ('Can't create user');
mysql_close();
}
/***************************************************************
** Function: Login
**
** Description: Check user authentcation and return if values exist in database
**
** Usage: Only used outside the classl
***************************************************************/
function Login( $user, $password )
{
$auth = false;
$user = $this->encrypt( $user );
$this->connect();
$sql = "select password from logins where user = '$user'";
$result = mysql_query( $sql ) or die( 'Cant login');;
$pass = mysql_fetch_row($result);
mysql_close();