Using PHPass Library in CodeIgniter

Before we start integrating PHPass in your CodeIgniter Project, download the file bellow:

Download PHPass

Then upload the phpass folder in your “application/libraries” directory.

Integration

Go to application/config/autoload.php then locate the code:

1
$autoload['libraries'] = array();

Then add the phpass library so that it gets autoloaded every time CodeIgniter is accessed.

1
$autoload['libraries'] = array('phppass/passwordhash');

If you do not want to autoload PHPass then you can construct the library in your Model or Controller like this:

1
$this->load->library('phppass/passwordhash');

Using the Library

PasswordHash class adds salt to the password and hashes it with 8 passes of MD5. MD5 is used by default because it’s supported on all platforms.

To generate a password you can do this:

1
echo $this->passwordhash->HashPassword('test');

Comparing plain password and hashed password:

1
2
3
4
5
6
7
8
$password_hashed = '$P$B55D6LjfHDkINU5wF.v2BuuzO0/XPk/';
$plain_password = 'test';

if($this->passwordhash->CheckPassword($plain_password, $password_hashed)) {
    echo "YES, Matched";
} else {
    echo "No, Wrong Password";
}


Do you need help with a project? or have a new project in mind that you need help with?

Contact Me