PHPackages                             maisto/phpauth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. maisto/phpauth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

maisto/phpauth
==============

A secure user authentication class for PHP websites, using a powerful password hashing system and attack blocking to keep your website and users secure.

112PHP

Since Sep 19Pushed 8y ago1 watchersCompare

[ Source](https://github.com/SMaisto/PHPAuth)[ Packagist](https://packagist.org/packages/maisto/phpauth)[ RSS](/packages/maisto-phpauth/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Stories in Ready](https://camo.githubusercontent.com/02f2aa5af27ca7661054b03dabb6240ccda82d8b480a39ac36080300ddd8fb25/68747470733a2f2f62616467652e776166666c652e696f2f504850417574682f504850417574682e706e673f6c6162656c3d7265616479267469746c653d5265616479)](https://waffle.io/PHPAuth/PHPAuth)[![Build Status](https://camo.githubusercontent.com/0ebef1d53e1e983e54c4a460c126c940f30009d53757e74faddfb208fe2bfd64/68747470733a2f2f6170692e7472617669732d63692e6f72672f504850417574682f504850417574682e706e67)](https://api.travis-ci.org/PHPAuth/PHPAuth)[![ghit.me](https://camo.githubusercontent.com/7ddd5ffaee9663defc576dc2c50fabcf376f9a431e4376f6b67ad25427119365/68747470733a2f2f676869742e6d652f62616467652e7376673f7265706f3d706870617574682f70687061757468)](https://ghit.me/repo/phpauth/phpauth)[![Minimum PHP Version](https://camo.githubusercontent.com/9580f88e287abaff3a940e594a6e81e31b69a18b10323c473c2245d101686cc6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e342d3838393242462e7376673f7374796c653d666c61742d636972636c65)](https://php.net/)

PHPAuth
=======

[](#phpauth)

What is it
----------

[](#what-is-it)

PHPAuth is a secure user authentication class for PHP websites, using a powerful password hashing system and attack blocking to keep your website and users secure.

PHPAuth is work in progress, and not meant for people that doesn't know how to program, its meant for people that know what they are doing.. We cannot help everyone because they dont understand this class..

IT'S NOT FOR BEGINNERS!

Features
--------

[](#features)

- Authentication by email and password combination
- Uses [bcrypt](http://en.wikipedia.org/wiki/Bcrypt) to hash passwords, a secure algorithm that uses an expensive key setup phase
- Uses an individual 128 bit salt for each user, pulled from /dev/urandom, making rainbow tables useless
- Uses PHP's [PDO](http://php.net/manual/en/book.pdo.php) database interface and uses prepared statements meaning an efficient system, resilient against SQL injection
- Blocks (or verifies) attackers by IP for any defined time after any amount of failed actions on the portal
- No plain text passwords are sent or stored by the system
- Integrates easily into most existing websites, and can be a great starting point for new projects
- Easy configuration of multiple system parameters
- Allows sending emails via SMTP or sendmail
- Blocks disposable email addresses from registration

User actions
------------

[](#user-actions)

- Login
- Register
- Activate account
- Resend activation email
- Reset password
- Change password
- Change email address
- Delete account
- Logout

Requirements
------------

[](#requirements)

- PHP 5.4
- MySQL / MariaDB database or PostGreSQL database

Composer Support
----------------

[](#composer-support)

PHPAuth can now be installed with the following command:

`composer require phpauth/phpauth`

Then: `require 'vendor/autoload.php';`

Configuration
-------------

[](#configuration)

The database table `config` contains multiple parameters allowing you to configure certain functions of the class.

- `site_name` : the name of the website to display in the activation and password reset emails
- `site_url` : the URL of the Auth root, where you installed the system, without the trailing slash, used for emails.
- `site_email` : the email address from which to send activation and password reset emails
- `site_key` : a random string that you should modify used to validate cookies to ensure they are not tampered with
- `site_timezone` : the timezone for correct datetime values
- `site_activation_page` : the activation page name appended to the `site_url` in the activation email
- `site_password_reset_page` : the password reset page name appended to the `site_url` in the password reset email
- `cookie_name` : the name of the cookie that contains session information, do not change unless necessary
- `cookie_path` : the path of the session cookie, do not change unless necessary
- `cookie_domain` : the domain of the session cookie, do not change unless necessary
- `cookie_secure` : the HTTPS only setting of the session cookie, do not change unless necessary
- `cookie_http` : the HTTP only protocol setting of the session cookie, do not change unless necessary
- `cookie_remember` : the time that a user will remain logged in for when ticking "remember me" on login. Must respect PHP's [strtotime](http://php.net/manual/en/function.strtotime.php) format.
- `cookie_forget` : the time a user will remain logged in when not ticking "remember me" on login. Must respect PHP's [strtotime](http://php.net/manual/en/function.strtotime.php) format.
- `bcrypt_cost` : the algorithmic cost of the bcrypt hashing function, can be changed based on hardware capabilities
- `smtp` : `0` to use sendmail for emails, `1` to use SMTP
- `smtp_host` : hostname of the SMTP server
- `smtp_auth` : `0` if the SMTP server doesn't require authentication, `1` if authentication is required
- `smtp_username` : the username for the SMTP server
- `smtp_password` : the password for the SMTP server
- `smtp_port` : the port for the SMTP server
- `smtp_security` : `NULL` for no encryption, `tls` for TLS encryption, `ssl` for SSL encryption
- `verify_password_min_length` : minimum password length, default is `3`
- `verify_email_min_length` : minimum EMail length, default is `5`
- `verify_email_max_length` : maximum EMail length, default is `100`
- `verify_email_use_banlist` : use banlist while checking allowed EMails (see `/files/domains.json`), default is `1` (`true`)
- `attack_mitigation_time` : time used for rolling attempts timeout, default is `+30 minutes`. Must respect PHP's [strtotime](http://php.net/manual/en/function.strtotime.php) format.
- `attempts_before_verify` : maximum amount of attempts to be made within `attack_mitigation_time` before requiring captcha. Default is `5`
- `attempt_before_ban` : maximum amount of attempts to be made within `attack_mitigation_time` before temporally blocking the IP address. Default is `30`
- `password_min_score` : the minimum score given by [zxcvbn](https://github.com/bjeavons/zxcvbn-php) that is allowed. Default is `3`

The rest of the parameters generally do not need changing.

CAPTCHA Implementation
----------------------

[](#captcha-implementation)

If `isBlocked()` returns `verify`, then a CAPTCHA code should be displayed. The method `checkCaptcha($captcha)` is called to verify a CAPTCHA code. By default this method returns `true`, but should be overridden to verify a CAPTCHA.

For example, if you are using Google's ReCaptcha NoCaptcha, use the following code:

```
    private function checkCaptcha($captcha)
    {
 try {

        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = ['secret'   => 'your_secret_here',
            'response' => $captcha,
            'remoteip' => $this->getIp()];

        $options = [
            'http' => [
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data)
            ]
        ];

        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return json_decode($result)->success;
    }
    catch (\Exception $e) {
        return false;
    }
}
```

If a CAPTCHA is not to be used, please ensure to set `attempt_before_block` to the same value as `attempts_before_verify`.

How to secure a page
--------------------

[](#how-to-secure-a-page)

Making a page accessible only to authenticated users is quick and easy, requiring only a few lines of code at the top of the page:

```
