Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,165,435 members, 7,861,207 topics. Date: Saturday, 15 June 2024 at 07:02 AM

Fellow PHP Developers, Help Me Look In This. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Fellow PHP Developers, Help Me Look In This. (1145 Views)

PHP Developers, How True Is This Statements. / 10 Best PHP Frameworks All PHP Developers Should Check Out / Pls Programmers And Web Developers Help Out (2) (3) (4)

(1) (Reply) (Go Down)

Fellow PHP Developers, Help Me Look In This. by Sammyskills(m): 8:04pm On Jul 23, 2015
I'm creating a website, which will require users to register and log in. My choice of password security was Bcrypt (password hashing + salt), and I downloaded ircmaxell's password_compact codehttps://github.com/ircmaxell/password_compat.

After requiring it in my functions file, the password_hash() functions works very well. But the challenge now is to verify a user's password when logging in.
Here's my code for login
if(!isset($error)){
//Use the input username and password and check against 'users' table

$query = mysql_query("SELECT * FROM users
WHERE username = '".mysql_real_escape_string($user_input)."' OR email = '".mysql_real_escape_string($user_input)."' LIMIT 1"wink
or die(mysql_error());
$count_row = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);


if($count_row == 1){
$hashed_password = $row['password'];
if(!password_verify($password, $hashed_password)){
$error[] = "Login failed! Please check your entered email and/or password";
} else if($row['active'] < 1){
$error[] = "Account has not been activated.";
}else if($row['active'] == 1){
// Do nothing
} else {
//write user data into PHP SESSION


I don't know what exactly I am doing wrong. Please help.
Re: Fellow PHP Developers, Help Me Look In This. by FincoApps(m): 8:45pm On Jul 23, 2015
If the encrypted password is what is stored in the database, then you need to first encrypt the user's input and check the encrypted user input against the database
Sammyskills:
I'm creating a website, which will require users to register and log in. My choice of password security was Bcrypt (password hashing + salt), and I downloaded ircmaxell's password_compact codehttps://github.com/ircmaxell/password_compat.

After requiring it in my functions file, the password_hash() functions works very well. But the challenge now is to verify a user's password when logging in.
Here's my code for login
if(!isset($error)){
//Use the input username and password and check against 'users' table

$query = mysql_query("SELECT * FROM users
WHERE username = '".mysql_real_escape_string($user_input)."' OR email = '".mysql_real_escape_string($user_input)."' LIMIT 1"wink
or die(mysql_error());
$count_row = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);


if($count_row == 1){
$hashed_password = $row['password'];
if(!password_verify($password, $hashed_password)){
$error[] = "Login failed! Please check your entered email and/or password";
} else if($row['active'] < 1){
$error[] = "Account has not been activated.";
}else if($row['active'] == 1){
// Do nothing
} else {
//write user data into PHP SESSION


I don't know what exactly I am doing wrong. Please help.

1 Like

Re: Fellow PHP Developers, Help Me Look In This. by Sammyskills(m): 8:52pm On Jul 23, 2015
FincoApps:
If the encrypted password is what is stored in the database, then you need to first encrypt the user's input and check the encrypted user input against the database

Thanks for your comment.
If I have to encrypt the user's input, it will return a different hash and salt (even if the inputs are the same). That's the way BCRYPT works.
Re: Fellow PHP Developers, Help Me Look In This. by FincoApps(m): 9:00pm On Jul 23, 2015
Oh okay, I just read the SELECT statement well now self. What problem are you having now ?
Sammyskills:


Thanks for your comment.
If I have to encrypt the user's input, it will return a different hash and salt (even if the inputs are the same). That's the way BCRYPT works.
Re: Fellow PHP Developers, Help Me Look In This. by omoelu1(m): 10:10pm On Jul 23, 2015
Sammyskills:


Thanks for your comment.
If I have to encrypt the user's input, it will return a different hash and salt (even if the inputs are the same). That's the way BCRYPT works.
if that is really how the bcrypt work, then I think there is no way for you to test if the two are equal.
well, maybe if there's a way to decrypt the already encrypted password, then check the entered password against the decrypted one.
but if there's no way of decrypting, then to the best of my knowledge, there is no actual way of testing true equality
Re: Fellow PHP Developers, Help Me Look In This. by Nobody: 10:25pm On Jul 23, 2015
@op, did you not read how to verify a password right on the same page

Verifying Password Hashes

To verify a hash created by password_hash, simply call:

if (password_verify($password, $hash)) {
/* Valid */
} else {
/* Invalid */
}
That's all there is to it.

https://github.com/ircmaxell/password_compat

The writer of the script has given you everything, abeg no disturb boys for here jor unless you no fit read.

1 Like

Re: Fellow PHP Developers, Help Me Look In This. by FincoApps(m): 6:52am On Jul 24, 2015
So you can be serious ?
dhtml18:
@op, did you not read how to verify a password right on the same page



https://github.com/ircmaxell/password_compat

The write of the script has given you everything, abeg no disturb boys for here jor unless you no fit read.
Re: Fellow PHP Developers, Help Me Look In This. by Sammyskills(m): 6:54am On Jul 24, 2015
I have been able to get the password_verify function to work. The challenge was that I was verifying with a different variable.

In my registration page, I had

$password = password_hash($password, PASSWORD_BCRYPT);


And in my login page, I had

password_verify($password, $hashed_password);


So after changing to

$hashed_password = password_hash($password, PASSWORD_BCRYPT);
in my registration page, the password_verify function now works perfectly.

Thank you @FincoApps, @omoelu1 and @dhtml18. I'm very grateful.

1 Like

Re: Fellow PHP Developers, Help Me Look In This. by Nobody: 7:51am On Jul 24, 2015
FincoApps:
So you can be serious ?
Chisox! what do you people take me for? A comedian or a programmer?
Re: Fellow PHP Developers, Help Me Look In This. by maekhel(m): 8:09am On Jul 24, 2015
dhtml18:

Chisox! what do you people take me for? A comedian or a programmer?
A mix in between smiley

(1) (Reply)

Join The Unilag Programmers Club! / Excellent Java Developers Quizzes For Interview Prepration / Wow!!! Mark Zuckerberg Shared This Video Of His ‘crazy Virtual Reality’

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.