Before I am crucified, I have been on this for a week and will appreciate it if anyone can help. I have also read all the articles concerning password_hash
and password_verify
on this platform, but none of the solutions worked for me, from string length and database length and more, so I decided to post my question.
I am writing a login script and I used password_verify
; I just learned security in PHP and wanted to improve my code.
Every time I try to login, it doesn't login.
public function create() {
// Don't forget your SQL syntax and good habits:
// - INSERT INTO table (key, key) VALUES ('value', 'value')
// - single-quotes around all values
// - escape all values to prevent SQL injection
/* $sql = "INSERT INTO users (username, password, first_name, last_name)
VALUES( ?, ?, ?, ?)";*/
$sql = "INSERT INTO users (username, password, email, date_created)
VALUES( :username, :password, :email, :date_created)";
try {
// prepare sql and bind parameters
$core = ConnectionManager::getInstance();
$stmt = $core->con->prepare($sql);
/*$stmt = $db->prepare("INSERT INTO users (id, username, password, first_name, last_name)
VALUES (:id, :username, :password, :first_name, :last_name");*/
//$stmt->bindParam(':id', 1);
$username = $this->username;
$password = password_hash($this->password, PASSWORD_BCRYPT);
$email = $this->email;
$date_created = strftime("%Y-%m-%d %H:%M:%S", time());
/*$username = "boiy";
$password = password_hash("redrum", PASSWORD_BCRYPT);
$email = "bdboiy@gmail.com";
$date_created = strftime("%Y-%m-%d %H:%M:%S", time());*/
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':date_created', $date_created, PDO::PARAM_STR);
if($stmt->execute()) {
$this->id = $core->con->lastInsertId();
echo "New records created successfully";
return true;
} else {
return false;
}
} catch (PDOException $e) {
echo "Insert Error: " . $e->getMessage();
}
}
That's to creat a new user. It worked.
Then I tried to login the user:
if($session->is_logged_in()) {redirect_to("admin.php");}
// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$YRJWA = "YRJWA";
$string = "$2y$10$$YRJWA/EJQGkmqev6VlpteOXHwF6DeQWcU1x1uGqmOdY4CDK5.oJYi";
// Check database to see if username/password exist.
$found_user = User::authenticate($username, $string);
if ($found_user) {
//if (password_verify($password, $found_user->password)) {
$session->login($found_user);
log_action('Login', "{$found_user->username} logged in.");
redirect_to("admin.php");
//}
} else {
// username/password combo was not found in the database
$message = "Username/password combination incorrect.";
}
} else { // Form has not been submitted.
$username = "";
$password = "";
}
When I do this, it does not work.
When I put password_verify
, it does not work, so I commented it out as seen in the code above.
I then created a new page and tested my code there:
$YRJWA = "YRJWA";
$string = "$2y$10$$YRJWA/EJQGkmqev6VlpteOXHwF6DeQWcU1x1uGqmOdY4CDK5.oJYi";
//$string = 'redrum';
echo "<hr>";
$found_user = User::authenticate('boiy', $string);
print_r($found_user);
echo "<br>";
/*echo strlen($found_user->password);*/
/*echo utf8_encode($found_user->password);
echo utf8_decode($found_user->password);*/
echo "<br/>";
//echo substr($found_user->password, 0, 60);
var_dump($found_user->password);
echo "<br>";
if(password_verify('redrum', $found_user->password)) {
echo "Valid";
} else {
echo "Invalid";
}
ConnectionManager::close();
When I use the password directly from database, it returns the right data and prints "Valid".
When I remove the comment and try "redum" it gives this error
Notice: Trying to get property of non-object in C:\xampp\htdocs\WealthPlus\private\index.php on line 36 NULL
Notice: Trying to get property of non-object in C:\xampp\htdocs\WealthPlus\private\index.php on line 39 Invalid
public static function authenticate($username="", $password="") {
$sql = "SELECT username, password FROM users WHERE username = :username AND password = :password LIMIT 1";
try {
$core = ConnectionManager::getInstance();
$stmt = $core->con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$stmt->execute([':username' => $username, ':password' => $password]);
$stmt->setFetchMode(PDO::FETCH_CLASS, "User");
$users = $stmt->fetch();
//if (password_verify($password, $users->password)) {
return $users;
//}
//return !empty($users) ? array_shift($users) : false;
}catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}