You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用RedBean库开发注册表单后无法实现账号登录,求排查解决

问题排查结果

现有代码错误点

  • PHP语法错误:所有条件分支的大括号配对完全混乱,多处存在多余的左括号、缺失的右括号,直接运行会触发语法报错
  • RedBean查询方法误用:R::find()返回匹配条件的用户对象数组,无法直接调用$user->password获取属性,需使用查询单条记录的R::findOne()
  • 表单属性错误:密码输入框的value错误绑定了用户名变量$data['login'],会导致用户输入密码时自动填充之前输入的用户名
  • 缺少登录态保存逻辑:密码校验通过的分支无任何执行代码,未写入session等登录标识,就算账号密码正确也无法完成登录
  • 文件路径不规范:表单action属性填写的sign in.php带空格,部分Web服务器无法正常解析,建议修改为无空格命名如sign_in.php

修正后可运行代码

<?php
require "db.php";
session_start(); // 开启session用于持久化登录状态
$data = $_POST;
$errors = array();

if (isset($data['do_login'])) {
    // 用findOne查询单个用户
    $user = R::findOne('users', 'login = ?', array($data['login']));
    if ($user) {        
        if(password_verify($data['password'], $user->password)){
            // 校验通过写入登录态
            $_SESSION['logged_user'] = $user;
            // 登录成功跳转至首页,可根据需求修改路径
            header('Location: /index.php');
            exit;
        }else{
            $errors[] = "密码错误!";
        }
    }else{
        $errors[] = "对应用户名不存在!";
    }

    if (!empty($errors)) {
        echo '<div style="color:red;">'.array_shift($errors).'</div>';
    }
}
?>
<link rel="stylesheet" type="text/css" href="stylel.css">
<div class="container">
    <form action="sign_in.php" method="POST">
        <h1 style="position:relative;left: 26%; top: -10%;">登录</h1>
        <h2><input type="text" name="login" placeholder="请输入用户名" value="<?php echo @$data['login'];?>"></h2>
        <h3><input type="password" name="password" placeholder="请输入密码" value=""></h3>   
        <h4><button type="submit"  class="RegB" name="do_login">登录</button></h4> 
    </form>
</div>

内容的提问来源于stack exchange,提问作者Misha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:21:01