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

PHP数据库账号注册与登录验证功能登录失败问题排查

登录失败的核心原因及修复方案

1. 核心错误:查询结果绑定顺序不匹配

登录功能的SQL查询语句定义的返回字段顺序为 username、password、user_type:

$sql = "SELECT username, password, user_type FROM users WHERE username = ? ";

但你绑定结果变量时的顺序完全错位:

// 错误写法:第二个位置绑定了用户类型,第三个位置才绑定密码哈希
mysqli_stmt_bind_result($stmt, $username, $user_type, $hashed_password);

这会导致原本应该存入$hashed_password的密码哈希值被存到了$user_type变量里,而$hashed_password拿到的是用户类型字段的值,password_verify验证必然失败,自然提示密码无效。

修复方法:调整绑定顺序和查询字段顺序保持一致即可:

mysqli_stmt_bind_result($stmt, $username, $hashed_password, $user_type);

2. 其他需修复的次要问题

  • 注册页确认密码输入框类型错误:代码写的是<input type="password2" name="password2">,password2不是合法的输入框类型,浏览器会默认按文本框处理,输入时不会隐藏内容,建议修改为type="password"。
  • 登录逻辑中header跳转前有echo输出:echo "You are logged in!"会导致header跳转失效,因为HTTP头必须在任何输出前发送,建议删除对应echo语句,或者开启输出缓存。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 14:27:00