gpt4 book ai didi

php - 确认密码无效

转载 作者:行者123 更新时间:2023-11-28 01:20:46 24 4
gpt4 key购买 nike

我有这个 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

action/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 尝试了这两种方法)和标题)。

有什么建议吗?

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');
}
}
}

?>

最佳答案

尝试:

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

代替:

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

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

关于php - 确认密码无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33974472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com