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 (2 months ago) RSS 2.0 feedStarStarStar 58%Total: 824 This week: 1All time: 4,188 This week: 560Up
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 is available for providing paid consulting. Contact Radovan Janjic .
Classes: 14 packages by
Country: Serbia Serbia
Age: 35
All time rank: 2382 in Serbia Serbia
Week rank: 34 Up1 in Serbia Serbia Up
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:824
This week:1
All time:4,188
This week:560Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:-
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:58%StarStarStar
Rank:1500