Basic php class, Framework agnostic that manage users accounts, register, login, update, password encryption etc...
password_hash() and password_verify() function are used to encrypt and check the user password.
PHP 5 >= 5.5.0, PHP 7
Download the files ModelUsers.php
and table.sql
execute table.sql
in your DB
{
...
"require": {
"digitalway/basic-php-auth": "1.*"
}
}
CREATE TABLE `users` (
`id_user` int(11) NOT NULL,
`firstname` varchar(100) NOT NULL,
`lastname` varchar(100) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
See table.sql
for the complete structure including indexes
You must pass a PDO connection as a parameter when you instantiate the class
$user = new ModelUsers($pdoLink);
$user = new ModelUsers($pdoLink);
if($user->login('login', 'password')){
echo 'Login and password correct';
print_r($user->profile);
}
else{
echo 'Wrong login or password';
}
$user->get(5);
print_r($user->profile);
$user->getByMail('hello@kidnoize.be');
print_r($user->profile);
$user->getList(0,0, 'name', 'ASC');
print_r($user->list);
$user->profile['firstname'] = 'Kid';
$user->profile['lastname'] = 'Noize';
$user->profile['email'] = 'hello@kidnoize.be';
$user->profile['password'] = 'houbahouba';
$user->add();
Return -2 if the e-mail exists
$user->profile['firstname'] = 'Kid';
$user->profile['lastname'] = 'Noize';
$user->profile['email'] = 'hello@kidnoize.be';
$user->profile['id'] = 5;
$user->update();
Return -2 if the e-mail exists
$user->profile['password'] = 'houbahoubazitoko';
$user->profile['id'] = 5;
$user->updatePassword();
$user->del(5);