Hi,
I am working on PHP/MYSQL register/login system for like a week and iam truly having issue with php password_hash and password_verify function…The register worked and the login with password_verify keeps failing and i do not understand why…Could somebody help out? i really am desperate.
Login part…what i am trying to achieve 1-its check if email is empty or not and if is valid email…2–its check if password is empty or not. 3-if both $email and $password are ok…it makes connection to db… then a-it checks if the email exists in table users, if not it requires to register…b-if the user exists in db, then it verify if the $password is same as passsword in db…if it is valid password…it echoes “valid”…and if not valid password…it echoes “invalide email/password”…that is what i am trying to achieve…
Here i am posting the full code:
db design
[php]id(auto_increment/primary key)
email(unique, varchar)
password(varchar, 255)[/php]
register.php
[php]<?php
$email=$password="";
$emailErr=$passwordErr="";
if (isset($_POST[‘submit’])) {
if (empty($_POST[‘email’])) {
$emailErr=“Enter your email”;
}
elseif (filter_var($_POST[‘email’], FILTER_VALIDATE_EMAIL) === FALSE) {
$emailErr = “Invalid email”;
}
else
{
$email= trim($_POST[‘email’]);
}
if (empty($_POST[‘password’])) {
$passwordErr = “Enter your password”;
}
elseif (strlen($_POST[‘password’]) < 3) {
$passwordErr = “password must 4 length least”;
}
else
{
$password = trim($_POST[‘password’]);
}
// if everything is filled correct connect
if ($email && $password)
{
include_once’connect.php’;
$sql = “SELECT COUNT(users.email) FROM users WHERE email = :email”;
$s = $pdo->prepare($sql);
$s->bindValue(’:email’, $email);
$s->execute();
$result = $s->fetch(PDO::FETCH_NUM);
$resultvalue = $result[0];
//if email exist, stop the script
if ($resultvalue > 0) {
echo “Email already exist”;
exit();
}
// if email not exist insert it
else
{
$sql = “INSERT INTO users (email,password) VALUES (:email, :password)”;
$stmt = $pdo->prepare($sql);
$stmt->bindValue(’:email’, $email);
$stmt->bindValue(’:password’, password_hash($password, PASSWORD_DEFAULT));
$stmt->execute();
if ($stmt) {
echo "Values inserted";
exit();
}
else
{
echo "Insert values failed";
exit();
}
}
}
//if everything is not filled correct connect
else
{
$proceedErr = “Could not proceed”;
}
}//submit
?>
Register page form p label { display: block; }em
{
color: red;
font-style: normal;
}
Email : <?php if(isset($emailErr)) echo $emailErr;?>
Password : <?php if(isset($passwordErr)) echo $passwordErr;?>
[/php]login.php
[php]<?php
$emailErr=$passwordErr="";
$email=$password="";
if (isset($_POST[‘submit’])) {
if (empty($_POST[‘email’])) {
$emailErr=“Enter your email”;
}
elseif (filter_var($_POST[‘email’], FILTER_VALIDATE_EMAIL) === FALSE) {
$emailErr = “Enter valid email”;
}
else
{
$email = trim($_POST[‘email’]);
}
if (empty($_POST[‘password’])) {
$passwordErr=“Enter your password”;
}
else
{
$password= trim($_POST[‘password’]);
}
if ($email && $password)
{
include_once’connect.php’;
$sql = “SELECT user_id,email, password FROM users WHERE email = :email”;
$s = $pdo->prepare($sql);
$s->bindValue(’:email’, $email);
$s->execute();
$result = $s->fetch(PDO::FETCH_ASSOC);
$resultvalue = count($result[‘email’]);
print_r($result);
//if email do not exist, stop the script
if ($resultvalue < 1) {
echo “Your email do not exist, please register”;
exit();
}
elseif (password_verify($password, $result[‘password’])) {
echo “valide password / email”;
exit();
}
else
{
echo “InValid email / password”;
exit();
}
}
else
{
echo "Email / password do not match";
}
}// end submit
?>
Login page form p label { display: block; }em
{
color: red;
font-style: normal;
}
Email : <?php if(isset($emailErr)) echo $emailErr;?>
Password : <?php if(isset($passwordErr)) echo $passwordErr;?>
[/php]