PHP Classes

PHP MySQL Secure Log In: Register and login users in a MySQL database

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (3 days ago) RSS 2.0 feedNot enough user ratingsTotal: 819 This week: 10All time: 4,206 This week: 22Up
Version License PHP version Categories
php-mysql-secure-log 1.10GNU General Publi...5.0PHP 5, Databases, User Management, Lo...
Description 

Author

This package can register and login users in a MySQL database.

It provides several classes that can register users by creating the respective MySQL database records, authenticate registered users, and manage sessions of authenticated storing session data also in a MySQL database table.

MySQL database SQL queries are logged to a file.

Picture of Radovan Janjic
  Performance   Level  
Name: Radovan Janjic <contact>
Classes: 14 packages by
Country: Serbia Serbia
Innovation award
Innovation award
Nominee: 3x

Example

<?php

define
('MYSQL_HOST', 'localhost');
define('MYSQL_USERNAME', 'root');
define('MYSQL_PASSWORD', '');
define('MYSQL_DATABASE', 'users');
define('MYSQL_TABLE_USERS', 'users');

include
'MySQL_wrapper.class.php';
include
'MySQL_Session_Handler.class.php';
include
'User_Validation.class.php';
include
'User_Log_In.class.php';



$db = MySQL_wrapper::getInstance(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);

// Logging only for testing!
$db->logQueries = TRUE;

// Log errors
$db->logErrors = TRUE;

// Create object
$GLOBALS['MYSQL_SESSION'] = new MySQL_Session_Handler($db, 'sessions');

$a = new User_Log_In;

$a->db = &$db;

echo
'<pre>';

echo
'Session before login check:' . PHP_EOL, print_r($_SESSION, TRUE);

$loggedin = FALSE;
if (
$a->loggedIn()) {
   
$loggedin = TRUE;
    if (!empty(
$_POST['logout'])) { // Log out
       
$a->logOut('example.php?loggedOutLocation');
    }
    echo
'<b>Logged in!</b>' . PHP_EOL;
   
} else {
   
$loggedin = FALSE;
    if (isset(
$_POST['login'])) {
        if (
$a->logInUser($_POST['email'], $_POST['password'])) { // Login
           
header('Location: example.php?loggedInLocation');
           
$loggedin = TRUE;
        } else {
            echo
'<b>Woops!</b>' . PHP_EOL;
            echo
'<b>Email or password is wrong!</b>' . PHP_EOL;
        }
    }
}


echo
'Session after login check:' . PHP_EOL, print_r($_SESSION, TRUE);


echo
PHP_EOL . PHP_EOL . 'POST:' . PHP_EOL . print_r($_POST, TRUE) . PHP_EOL;

/* */
$hash = $a->hash("foobar");
echo
"Hashed pass: " . $hash . PHP_EOL;
if (
$a->validatePassword("foobar", $hash)) {
    echo
'Valid password' . PHP_EOL;
} else {
    echo
'Wrong password' . PHP_EOL;
}

?>
</pre>

<?php if ($loggedin): ?>
<form method="post">
        <input type="hidden" name="logout" value="1" />
        Log out
        <input type="submit" />
    </form>
<?php else: ?>
<form method="post">
        <input type="hidden" name="login" value="1" />
        <input type="text" name="email" value="rade@it-radionica.com" /><br />
        <input type="text" name="password" value="foobar" />
        <input type="submit" />
    </form>
<?php endif; ?>


  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file log-mysql.txt Output Sample output
Plain text file MySQL_Session_Handler.class.php Class Class source
Plain text file MySQL_wrapper.class.php Class Class source
Accessible without login Plain text file users.sql Data Auxiliary data
Plain text file User_Log_In.class.php Class Class source
Plain text file User_Register.class.php Class Class source
Plain text file User_Validation.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:819
This week:10
All time:4,206
This week:22Up