php登录页面-当我插入有效数据时,它不工作,但当我插入无效数据时,它工作得很好

php登录页面-当我插入有效数据时,它不工作,但当我插入无效数据时,它工作得很好,php,mysql,forms,session,login,Php,Mysql,Forms,Session,Login,我有个问题。我正在建立我自己的登录页面,我有一些麻烦。当我测试我的页面并插入无效的用户名和密码时,效果非常好。但当我插入数据库中存在的数据时,它只跳过if(isset($\u POST['But\u submit'])部分。这里可能出了什么问题? 这是我的密码: PHP: HTML 登录 我真的很高兴有人能帮助我:) 谢谢大家! 也许这样行得通?先调试是否缺少字段,然后执行SQL require_once 'config.php'; ini_set('display_errors',

我有个问题。我正在建立我自己的登录页面,我有一些麻烦。当我测试我的页面并插入无效的用户名和密码时,效果非常好。但当我插入数据库中存在的数据时,它只跳过
if(isset($\u POST['But\u submit'])
部分。这里可能出了什么问题? 这是我的密码:

PHP:


HTML


登录
我真的很高兴有人能帮助我:)
谢谢大家!

也许这样行得通?先调试是否缺少字段,然后执行SQL

require_once 'config.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (mysqli_connect_errno()) die(mysqli_connect_error());

# Debug what the issue is
foreach(array('but_submit', 'txt_uname', 'txt_pwd') as $arg)
    if(!isset($_POST[$arg]))
        die("{$arg} field is not set or empty");

# Prevent SQL injection
$stmt = $con->prepare("SELECT COUNT(*) as cntUser FROM users WHERE Username ? AND passwrd = ?");
$stmt->bind_param("ss", $_POST['txt_uname'], $_POST['txt_pwd']);
if(!$stmt->execute()){ mysqli_error($con); }
$stmt->store_result();

# Check the row count is more than 0
if($stmt->num_rows != 0) {
    session_start(); # Start session, if config.php already does this you can remove this
    $_SESSION['uname'] = $_POST['txt_uname']; # Think about injects here - use the database record not untrusted POST data
    header('Location: ' . sprintf("%s://%s/welcome.php", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']));
    exit();
}

# Nothing was returned
die('Incorrect username / password combination');
需要注意的是,您不应该在会话中存储用户名,这应该是一个在您的案例中作为参数提供的用户名:

$_SESSION['token'] = \Firebase\JWT\JWT::encode(array('username' => $_POST['uname']), 'your secret');
然后在Welcome.php文件中,您可以像这样对其进行解码(建议先构建某种控制器):


另一件需要注意的事情是,最好将数据从数据库中取出,这样您就可以使用他们的唯一ID创建一个令牌以及您希望包含的其他身份(远离敏感信息)。

如果(isset($\u POST['but\u submit']),它跳过是什么意思
?你没有输出?您不应该存储纯文本密码。
header('Location:welcome.php')需要
退出()之后当我插入有效数据时,程序在
if(isset($\u POST['but\u submit'])处给出false
。是的,我知道我不应该像那样存储密码..我会在发现这里出了什么问题后更改它。。。谢谢Jaquarh,谢谢,bit它仍然不起作用。
mysqli\u error($con)
将其添加到查询后返回什么?以及PHP的错误报告@Gandalf Sidenote:使用
@the_member_name
直接ping。
mysqli_real_escape_string()
需要连接作为第一个参数。但是,我不明白你为什么建议
$\u SESSION['uname']=mysqli\u real\u escape\u string($\u POST['txt\u uname']),不需要它。我已经指出,我把它放进去的唯一原因是停止像XSS一样注入会话,但如果做得好,他可以很容易地查询数据库并使用数据库中的记录,如果使用预先准备好的语句,已知该记录不包含注入,但我会立即编辑它@FunkFortyNinerI会更改
$stmt->execute()execute(){mysqli_error($con);}
可以抛出可能的错误,请随意更新和更改。我不是mysqli_*专家,我使用PDO,所以在回答@FunkFortyNinerOk时,大部分资源来自手册。首先谢谢你的回答。我知道我的提交按钮没有设置或是空的,我不知道如何解决这个问题。。。有什么建议吗?
require_once 'config.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (mysqli_connect_errno()) die(mysqli_connect_error());

# Debug what the issue is
foreach(array('but_submit', 'txt_uname', 'txt_pwd') as $arg)
    if(!isset($_POST[$arg]))
        die("{$arg} field is not set or empty");

# Prevent SQL injection
$stmt = $con->prepare("SELECT COUNT(*) as cntUser FROM users WHERE Username ? AND passwrd = ?");
$stmt->bind_param("ss", $_POST['txt_uname'], $_POST['txt_pwd']);
if(!$stmt->execute()){ mysqli_error($con); }
$stmt->store_result();

# Check the row count is more than 0
if($stmt->num_rows != 0) {
    session_start(); # Start session, if config.php already does this you can remove this
    $_SESSION['uname'] = $_POST['txt_uname']; # Think about injects here - use the database record not untrusted POST data
    header('Location: ' . sprintf("%s://%s/welcome.php", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']));
    exit();
}

# Nothing was returned
die('Incorrect username / password combination');
$_SESSION['token'] = \Firebase\JWT\JWT::encode(array('username' => $_POST['uname']), 'your secret');
$user = \Firebase\JWT\JWT::decode($_SESSION['token'], 'your secret', array('HS256'));
echo $user['username'];