PHP Classes

File: userchecker.complex-example.php

Recommend this page to a friend!
  Classes of Jon Gjengset   User Checker   userchecker.complex-example.php   Download  
File: userchecker.complex-example.php
Role: Example script
Content type: text/plain
Description: Complex example showing all methods
Class: User Checker
Authenticate users with records in a MySQL table
Author: By
Last change: Proper revision
Date: 17 years ago
Size: 2,304 bytes


Class file image Download
Available functions:
    function __construct($user, $pass, $passcheck = 'md5', $session = FALSE)
    function set_db_connection($serveraddr, $serveruser, $serverpass, $serverdbname)
    function set_table($dbtable)
    function set_fields($userfield, $passfield)
    function validate()
    function print_query()
    function get_user_info()

//Special super-strong hash encryption function
function stronghash($plain)
$hash = sha1(crypt(md5($plain), sha1($plain)));

//Get user data
$username = $_POST['username'];
$password = $_POST['password'];

//Set MySQL information
$serveraddr = '';
$serveruser = 'root';
$serverpass = 'toor';
$serverdbname = 'users';

//Pass username, password and DB information
//Tell the script that the password is hashed using a home-made function
$user = new User($username, $password, 'stronghash');
$user->set_db_connection($serveraddr, $serveruser, $serverpass, $serverdbname);
$user->set_fields('username', 'passwordhash');

//Start output
<title>Simple login page</title>

//If the user was in fact created (ie. there was a username and password) then $user will not be false
if ($user && $user->validate() > 0)
//A single user is found
if ($user->validate() == 1)
"Congratulations, your credentials have been accepted!";
//User is accepted, lets find some info about her and print it
$information = $user->get_user_info();
"Here is your info:";
nl2br(print_r($information, True));
//Multiple users, something is wrong (Mark that calling validate() will not connect to the database again)
else if ($user->validate() == 2)
"Are you trying to hack us?");
//If user was not found or user has not submitted the form yet
else if ($user->validate() == 0 || !$user)
?><form action="" method="post"><input type="text" name="username" /><input type="password" name="password" /><input type="submit" value="Login" /></form><?php
//An error occured, print query and die
"An error occured using query: \"".$user->print_query()."\", see error log for details");