|
|
|
|
|
|
| |
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 connect()
{
mysql_connect( $this->db['host'], $this->db['user'], $this->db['pass'] );
mysql_select_db( $this->db['database'] );
}
/***************************************************************
** Function: install
**
** Description: Installs the table required for the login script
**
** Usage: Only used outside the class.
***************************************************************/
function install()
{
$this->connect();
mysql_query( "create table logins ( user char(32), pasword char( 32 ) )" );
mysql_close();
}
/***************************************************************
** Function: encrypt
**
** Description: Encrypt any username/Passwd
**
** Usage: Only used inside this class. But can be used outside this class aswell
***************************************************************/
function encrypt( $string )
{
$crypt = crypt( md5( $string ), md5( $string ) );
return $crypt;
}
/***************************************************************
** Function: Add_user
**
** Description: Add a user to the database
**
** Usage: Only used outside the class
***************************************************************/
function Add_user( $username, $password )
{
$this->connect();
$password = $this->encrypt( $password );
$username = $this->encrypt( $username );
$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();
if ( $pass[0] == ( $this->encrypt( $password ) ) )
{
$auth = true;
}
return $auth;
}
} // End class login
?> | |
Examples on how to use:
Configure the variables in the class first!
Istallation example:
| <?php
include( 'auth.php' );
$auth = new Auth; // Be careful, PHP is case sensitive it's Auth not auth
$auth->install();
?> | |
User creation:
| <?php
include( 'auth.php' );
$auth = new Auth; // Be careful, PHP is case sensitive it's Auth not auth
$user = $somevar;
$pass = $someothervar;
$auth->Add_user( $user, $pass );
?> | |
Login script:
| <?php
include( 'auth.php' );
$new = new Auth; // Be careful, PHP is case sensitive it's Auth not auth
$user = $somevar;
$pass = $someothervar;
if( $auth->Login( $user, $pass ) )
{
echo( 'Login Successful' );
}
else
{
echo( 'Invalid Username/Password' );
}
?> | | |
|
| Use of bitmasks to represent permissions Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes | | | Access_user Class - an easy to use system for protecting pages and register users. Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication | | | .htpassword manager for apache Categories : PHP, PHP Classes, Authentication, Apache | | | .htpasswd class Categories : PHP, PHP Classes, Authentication | | | Password protection for Phorum 3.1.x with userlevels and log. Categories : PHP, MySQL, Authentication, Security | | | Form Elements Class Categories : PHP, PHP Classes, Form Processing | | | Filter - A simple class that lets you use multiple functions to create custom filters. Categories : PHP, PHP Classes, Strings | | | DbObject - A PHP wrapper for working with various databases Categories : Databases, PHP, PHP Classes | | | Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs | | | Browser Detecor Class Categories : PHP Classes, PHP, HTML | | | Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP, Filesystem, PHP Classes | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | PHP Zip Utility Categories : PHP, PHP Classes, Compression | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | |
|
|