当前位置: 首页 > 知识库问答 >
问题:

尝试用php&msql建立注册表单

孟新知
2023-03-14

我制作了一个简单的注册表单,并将其连接到数据库。如果我是第一次注册,它允许我登录并进入主页。然而,当我注销并尝试再次登录时,它总是显示“错误的密码或电子邮件”,即使我把一切都正确。尝试重置密码,密码成功重置,但当我尝试登录时,它只是再次显示相同的错误。

下面是im使用的注册php代码

//if user signup button 
if(isset($_POST['signup'])){ 
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
if($password !== $cpassword){
    $errors['password'] = "Confirm password not matched!";
}
$email_check = "SELECT * FROM usertable WHERE email = '$email'";
$res = mysqli_query($con, $email_check);
if(mysqli_num_rows($res) > 0){
    $errors['email'] = "Email that you have entered is already exist!";
}
if(count($errors) === 0){
    $encpass = $password;
    $code = rand(999999, 111111);
    $status = "notverified";
    $insert_data = "INSERT INTO usertable (name, email, password, code, status)
                    values('$name', '$email', '$encpass', '$code', '$status')";
    $data_check = mysqli_query($con, $insert_data);
    if($data_check){
        $subject = "Email Verification Code";
        $message = "Your verification code is $code";
        $sender = "From: blahblah@example.com";
        if(mail($email, $subject, $message, $sender)){
            $info = "We've sent a verification code to your email - $email";
            $_SESSION['info'] = $info;
            $_SESSION['email'] = $email;
            $_SESSION['password'] = $password;
            header('location: user-otp.php');
            exit();
        }else{
            $errors['otp-error'] = "Failed while sending code!";
        }
    }else{
        $errors['db-error'] = "Failed while inserting data into database!";
    }
}

}

这是登录php脚本im使用

//if user click login button
if(isset($_POST['login'])){
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);
    $check_email = "SELECT * FROM usertable WHERE email = '$email'";
    $res = mysqli_query($con, $check_email);
    if(mysqli_num_rows($res) > 0){
        $fetch = mysqli_fetch_assoc($res);
        $fetch_pass = $fetch['password'];
        if(password_verify($password, $fetch_pass)){
            $_SESSION['email'] = $email;
            $status = $fetch['status'];
            if($status == 'verified'){
              $_SESSION['email'] = $email;
              $_SESSION['password'] = $password;
                header('location: index.php');
            }else{
                $info = "It's look like you haven't still verify your email - $email";
                $_SESSION['info'] = $info;
                header('location: user-otp.php');
            }
        }else{
            $errors['email'] = "Incorrect email or password!";
        }
    }else{
        $errors['email'] = "It's look like you're not yet a member! Click on the bottom link to signup.";
    }
}

共有1个答案

太叔豪
2023-03-14

当您从注册页面注册新用户时,您将简单的密码存储到用户表中。假设,当您放入password=123时,它将把123存储到usertable中的password列中。

但是,当您尝试使用相同的密码123login时,您的login页面逻辑会显示密码应该使用password_verify方法来验证。

此方法使用散列算法检查密码。这就是您看到incorect message的原因。您可以更改登录页面逻辑

if(password_verify($password,$fetch_pass))if($password==$fetch_pass)

或者正确使用password_verify方法。

 类似资料:
  • 问题内容: 我尝试了很多不同的事情,并且进行了很多搜索,但都没有解决方案。我正在尝试使用html表单将数据提交到sql表。 这是我的register.php文件的代码。 我的HTML文件是: 我为那些奇怪的命名变量预先表示歉意,我担心其他文件会打扰我在这里试图做的事情。 问题答案:

  • 我正在处理一些注册过程的Android应用程序(原生Android),使用AWS放大和Cognito用户池从这个链接https://aws-amplify.github.io/docs/android/authentication#signup。 我已经做了,直到放大添加验证和放大推送。我的原始文件夹中有配置文件。在实现注册功能后,我在尝试从我的应用程序注册时遇到以下错误: 2019-10-23

  • 有可能从代码发送邮件吗? 我想注册,凭据应该再次发送给用户。 但根本没有电子邮件 我到底做错了什么 这是整个代码 我非常感谢你的帮助 编辑1我将phpmailer添加到我的文件中,并将其加载到代码中,但我如何使用它发送电子邮件?

  • 源代码 ENS注册表是ENS系统中的核心合约,所有的ENS查询都从注册表开始。注册表负责管理域名列表,记录每个域名的所有者、解析器和TTL,并允许域名的所有者对这些数据进行更改。 ENS注册表的详细信息请参阅EIP137。 获取所有者 function owner(bytes32 node) external view returns (address); 1 以上函数返回node所标识域名的所有

  • 新鲜的操作系统,新鲜与2015,我所做的就是创建具有个人身份验证的新mvc项目,一旦我尝试注册它,它就会给我那个错误(我什么都不编辑,我什么都不删除) '/'应用程序中的服务器错误。 系统找不到指定的文件 描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:系统。组件模型。Win32异常:系统找不到指定的文件 源错误: 第1