确认密码无效


Confirm password not working

我有这个html表单:

<form class="signup-form" action="action/register.php" method="post">
                            <input type="text" class="input" name="user" id="user_name" autocomplete="off" placeholder="Username" required="true">
                            <input type="text" class="input" name="nome" id="name" autocomplete="off" placeholder="Nome" required="true">
                            <input type="password" class="input" name="pass" id="user_pass" autocomplete="off" placeholder="Password" required="true">
                            <input type="password" class="input" name="cpass" id="user_cpass" autocomplete="off" placeholder="Confirme a Password" required="true">
                            <input type="submit" class="button" name="submit" value="Sign Up">
                        </form

操作/register.php如下所示:

global $db;
    $username=trim($_POST['user']);
    $nome=trim($_POST['nome']);
    $password=trim($_POST['pass']);
    $cpassword=trim($_POST['cpass']);

    if(strcmp($password,$cpassword)!==0) {
        echo "<script> window.location.assign('../errors/password_error.php'); </script>";
        //header('Location: ../errors/password_error.php');
    }
    $stmt = $db->prepare('SELECT username FROM utilizador');
    $stmt->execute();
    $result = $stmt->fetchAll();
    foreach ($result as $row) {
        if($row['username'] === $username){
            header('Location: ../errors/username_error.php');
        }
    }

当我插入一个已经存在的用户名时,它会成功地将我重定向到errors/username_error.php,但当我插入与pass不同的cpass时,它接受它,而它不应该接受它(我已经尝试过echo和header)。

有什么建议吗?

PS:我已经试过if($password!==$cpassword)

PPS:我想我解决了,但我不明白为什么,是吗?这是我拥有的整个register.php:

<?php
include_once('../database/connection.php'); // connects to the database
session_start();                         // starts the session
function NewUser()
{
    global $db;
    $username=trim($_POST['user']);
    $nome=trim($_POST['nome']);
    $password=trim($_POST['pass']);
    $cpassword=trim($_POST['cpass']);
    if($password !== $cpassword) {
        //echo "<script> window.location.assign('../errors/password_error.php'); </script>";
        header('Location: ../errors/password_error.php');
    }
    else{
        $stmt = $db->prepare('SELECT username FROM utilizador');
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach ($result as $row) {
            if($row['username'] === $username){
                header('Location: ../errors/username_error.php');
            }
        }

        $img = rand(0,10);
        $stmt = $db->prepare("INSERT INTO utilizador (username,nome,password,img) VALUES (:username,:nome,:password,:img)");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':nome', $nome);
        $stmt->bindParam(':password', $password);
        $stmt->bindParam(':img', $img);
        if($stmt->execute()){  
            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $username;
            $_SESSION['img'] = $img;
            header('Location: ../main.php');
        }
    }
}

if(isset($_POST['submit'])){
NewUser();
}
?>

然后我把它改成了这个(去掉了这个功能),它成功了

<?php

include_once('../database/connection.php'); // connects to the database
session_start();                         // starts the session
if(isset($_POST['submit'])){
global $db;
    $username=trim($_POST['user']);
    $nome=trim($_POST['nome']);
    $password=trim($_POST['pass']);
    $cpassword=trim($_POST['cpass']);
    if($password !== $cpassword) {
        //echo "<script> window.location.assign('../errors/password_error.php'); </script>";
        header('Location: ../errors/password_error.php');
    }
    else{
        $stmt = $db->prepare('SELECT username FROM utilizador');
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach ($result as $row) {
            if($row['username'] === $username){
                header('Location: ../errors/username_error.php');
            }
        }

        $img = rand(0,10);
        $stmt = $db->prepare("INSERT INTO utilizador (username,nome,password,img) VALUES (:username,:nome,:password,:img)");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':nome', $nome);
        $stmt->bindParam(':password', $password);
        $stmt->bindParam(':img', $img);
        if($stmt->execute()){  
            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $username;
            $_SESSION['img'] = $img;
            header('Location: ../main.php');
        }
    }
}
?>

try:

if(strcmp($password,$cpassword)!=0)

而不是:

if(strcmp($password,$cpassword)!==0)

我不能保证工作,因为我对php 有点陌生

线索1:数据库中的密码是否已加密?如果是,你应该在与输入密码进行任何比较之前对其进行加密

线索2:

您可以使用相同的技术来查找您的用户名和密码。你可以做这样的事。

$stmt = $db->prepare('SELECT username, password FROM utilizador');
    $stmt->execute();
    $result = $stmt->fetchAll();
    foreach ($result as $row) {
        if($row['username'] === $username){
            header('Location: ../errors/username_error.php');
        }
        if($row['password'] === $password){
            header('Location: ../errors/password_error.php');
        }
}

线索3:您是否尝试清除register.php中的浏览器缓存?

<?php
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>