WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Simple and fast user authentication
Categories : PHP, PHP Classes, Authentication Click here to Update Your Picture
leaping langoor
Date : Apr 27th 2005
Grade : 3 of 5 (graded 3 times)
Viewed : 7231
File : No file for this code example.
Images : No Images for this code example.
Search : More code by leaping langoor
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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' );
}
?>



Access_user Class - an easy to use system for protecting pages and register users.
Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication
.htpasswd class
Categories : PHP, PHP Classes, Authentication
Use of bitmasks to represent permissions
Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes
.htpassword manager for apache
Categories : PHP, PHP Classes, Authentication, Apache
Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate.
Categories : Authentication, PHP
very simple ftp class
Categories : PHP, PHP Classes, FTP
Authorize.net AIM Interface Class v1.0.0
Categories : PHP, PHP Classes, Ecommerce, Payment Gateways
Function to remember password
Categories : PHP, Authentication, Personalization and Membership
PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Authentication script to authenticate users in Active Directory through LDAP.
Categories : LDAP, Authentication, Cookies, PHP