Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的登录屏幕登录,即使输入不正确_Php_Html_Session_Login - Fatal编程技术网

Php 我的登录屏幕登录,即使输入不正确

Php 我的登录屏幕登录,即使输入不正确,php,html,session,login,Php,Html,Session,Login,我的后端登录屏幕在我填写某些内容时登录。这没有意义,我连接到数据库,检查表单是否已提交,然后执行查询,甚至有一个会话检查我是否已登录。也许是因为我用的是POST而不是GET?老实说,我不知道。我做错了什么 <?php if( !isset( $_SESSION ) ) session_start(); $msg=''; *db information* $conn = new mysqli( $dbhost, $dbuser, $dbpass, $dbname ); if ( $conn

我的后端登录屏幕在我填写某些内容时登录。这没有意义,我连接到数据库,检查表单是否已提交,然后执行查询,甚至有一个会话检查我是否已登录。也许是因为我用的是POST而不是GET?老实说,我不知道。我做错了什么

<?php
if( !isset( $_SESSION ) ) session_start();

$msg='';
*db information*
$conn = new mysqli( $dbhost, $dbuser, $dbpass, $dbname );
if ( $conn->connect_error ) die("Connection failed");


if( isset( $_POST['submit'] ) ) {

$uname = $_POST['username'];
$wwoord = $_POST['wachtwoord'];
$query = "SELECT * FROM Medewerkers WHERE medewerker_username='$uname' && medewerker_password='$wwoord'";

$result = $conn->query( $query );

if( $result ) {
    $_SESSION['ingelogd'] = true;
    header("location: adminpanel.php");
} else {
    $msg="Inloggegevens incorrect.";
}
$conn->close();
}
?>

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin login</title>
<link rel="stylesheet" type="text/css" href="tables.css">
</head>
<body>
<div id="content">
<ul>
    <li><a href="index.php">Admin panel</a></li>
</ul>
<h1>Admin login</h1>
<?php
echo $msg;
?>
<form role="form" method="post" action="index.php" class="contactForm">
    <table>
        <tr>
            <td><label for="username">Username</label></td>
            <td><input type="text" name="username" class="" id="username">  <br><br></td>
        </tr>
        <tr>
            <td><label for="wachtwoord">Wachtwoord</label></td>
            <td><input type="password" name="wachtwoord" class="" id="wachtwoord"><br><br></td>
        </tr>
        <tr>
            <td><button type="submit" name="submit"     class="button">Inloggen</button><br></td>
        </tr>
    </table>
</form>
</div>
</body>
</html>

管理员登录
管理员登录 用户名

瓦赫特伍德

Inloggen

您只检查查询是否正常工作,而不检查它是否返回行

而不是:

//...
$result = $conn->query( $query );

if( $result ) {
    $_SESSION['ingelogd'] = true;
//...
这样做:

//...
$result = $conn->query( $query );

if($result->num_rows == 1) {
    $_SESSION['ingelogd'] = true;
//...

当然,您的代码中还有其他需要改进的地方,比如SQL注入部分,您可以在不转义POST数据的情况下获取POST数据,但我将重点放在您的确切问题上。

您只检查查询是否工作,而不检查它是否返回行

而不是:

//...
$result = $conn->query( $query );

if( $result ) {
    $_SESSION['ingelogd'] = true;
//...
这样做:

//...
$result = $conn->query( $query );

if($result->num_rows == 1) {
    $_SESSION['ingelogd'] = true;
//...

当然,您的代码中还有其他需要改进的地方,比如SQL注入部分,您可以在不转义POST数据的情况下获取POST数据,但我将重点放在您的确切问题上。

我认为您需要在执行SQL后在php中进行检查,以确保返回的是有效的记录集,而不是布尔值$result

<?php
    if( !isset( $_SESSION ) ) session_start();
    if( isset( $_SESSION['ingelogd'] ) ) header("location: adminpanel.php");

    $msg='';

    $conn = new mysqli( $dbhost, $dbuser, $dbpass, $dbname );
    if ( $conn->connect_error ) die("Connection failed");


    if( isset( $_POST['submit'] ) ) {

        $uname = $_POST['username'];
        $wwoord = $_POST['wachtwoord'];
        $query = "SELECT * FROM `Medewerkers` WHERE `medewerker_username`='$uname' and `medewerker_password`='$wwoord' limit 1";

        $result = $conn->query( $query );

        if( $result ) {

            /* check that there is a valid recordset with exactly 1 record */
            if( $result->num_rows==1 ) {
                $_SESSION['ingelogd'] = true;
                header("location: adminpanel.php");
            }
            $msg='login failed';
        } else {
            $msg="Inloggegevens incorrect.";
        }
        $conn->close();
    }
?>


<?php
    /*
        if sql injection is a concern ( which it is ) then rather than embedding
        variables directly within the sql it is better to utilise prepared statements.
    */


    session_start();
    if( isset( $_SESSION['ingelogd'] ) ) header("location: adminpanel.php");

    $msg='';
    $conn = new mysqli( $dbhost, $dbuser, $dbpass, $dbname );
    if ( $conn->connect_error ) die("Connection failed");


    if( isset( $_POST['submit'] ) ) {

        $uname = $_POST['username'];
        $wwoord = $_POST['wachtwoord'];

        /* to avoid sql injection use prepared statements - add ? as placeholders for variables */
        $sql='select * from `medewerkers` where `medewerker_username`=? and `medewerker_password`=? limit 1';

        /* prepare the sql statement for execution */
        $stmt=$conn->prepare( $sql );

        /* If there is a problem preparing the statement it will return false */
        if( $stmt ) {

            /* assign variables to the placeholders */
            $stmt->bind_param( 'ss', $uname, $wwoord );

            /* execute the query */
            $result=$stmt->execute();

            /* The statement must have executed successfully, test recordset next */
            if( $result ) {

                /* store recordset so we can use access other functions / properties */
                $stmt->store_result();

                /* check that there is a valid recordset with exactly 1 record */
                if( $stmt->num_rows==1 ) {

                    /* we have a valid logon - one record retrieved */
                    $conn->close();

                    $_SESSION['ingelogd'] = true;
                    header("location: adminpanel.php");
                }
                $msg='login failed';
            } else {
                $msg="Inloggegevens incorrect.";
            }
            $conn->close();
        }
    }
?>

我认为在执行sql之后,需要在php中进行检查,以确保返回的是有效的记录集,而不是布尔值$result

<?php
    if( !isset( $_SESSION ) ) session_start();
    if( isset( $_SESSION['ingelogd'] ) ) header("location: adminpanel.php");

    $msg='';

    $conn = new mysqli( $dbhost, $dbuser, $dbpass, $dbname );
    if ( $conn->connect_error ) die("Connection failed");


    if( isset( $_POST['submit'] ) ) {

        $uname = $_POST['username'];
        $wwoord = $_POST['wachtwoord'];
        $query = "SELECT * FROM `Medewerkers` WHERE `medewerker_username`='$uname' and `medewerker_password`='$wwoord' limit 1";

        $result = $conn->query( $query );

        if( $result ) {

            /* check that there is a valid recordset with exactly 1 record */
            if( $result->num_rows==1 ) {
                $_SESSION['ingelogd'] = true;
                header("location: adminpanel.php");
            }
            $msg='login failed';
        } else {
            $msg="Inloggegevens incorrect.";
        }
        $conn->close();
    }
?>


<?php
    /*
        if sql injection is a concern ( which it is ) then rather than embedding
        variables directly within the sql it is better to utilise prepared statements.
    */


    session_start();
    if( isset( $_SESSION['ingelogd'] ) ) header("location: adminpanel.php");

    $msg='';
    $conn = new mysqli( $dbhost, $dbuser, $dbpass, $dbname );
    if ( $conn->connect_error ) die("Connection failed");


    if( isset( $_POST['submit'] ) ) {

        $uname = $_POST['username'];
        $wwoord = $_POST['wachtwoord'];

        /* to avoid sql injection use prepared statements - add ? as placeholders for variables */
        $sql='select * from `medewerkers` where `medewerker_username`=? and `medewerker_password`=? limit 1';

        /* prepare the sql statement for execution */
        $stmt=$conn->prepare( $sql );

        /* If there is a problem preparing the statement it will return false */
        if( $stmt ) {

            /* assign variables to the placeholders */
            $stmt->bind_param( 'ss', $uname, $wwoord );

            /* execute the query */
            $result=$stmt->execute();

            /* The statement must have executed successfully, test recordset next */
            if( $result ) {

                /* store recordset so we can use access other functions / properties */
                $stmt->store_result();

                /* check that there is a valid recordset with exactly 1 record */
                if( $stmt->num_rows==1 ) {

                    /* we have a valid logon - one record retrieved */
                    $conn->close();

                    $_SESSION['ingelogd'] = true;
                    header("location: adminpanel.php");
                }
                $msg='login failed';
            } else {
                $msg="Inloggegevens incorrect.";
            }
            $conn->close();
        }
    }
?>



请使用PHP处理密码安全问题。如果您使用的PHP版本低于5.5,则可以使用
密码\u散列()
。您需要进行一些基本的错误检查,假设您的查询正常工作。这是因为您没有测试$result
If(!isset($\u SESSION))SESSION\u start()的有效性将为您提供问题。只需执行
session_start()您正在检查查询是否执行,它执行了,只是没有找到任何结果。请使用PHP来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用
密码\u散列()
。您需要进行一些基本的错误检查,假设您的查询正常工作。这是因为您没有测试$result
If(!isset($\u SESSION))SESSION\u start()的有效性将为您提供问题。只需执行
session_start()您正在检查查询是否执行了,它执行了,只是没有找到任何结果。您需要修复代码顶部的会话检查。没错-尽管我发现在通过ajax包含文件或调用文件时,会话中发生了奇怪的事情,但这似乎解决了这一问题。无论如何,这不是重点-op需要检查记录集的结果,而不是对$resultSigh的布尔值进行操作,true,但是代码,正如它所显示的,可能会在以后出现问题。如果测试因任何原因失败,所有登录功能都将被跳过。@RamRaider我刚刚尝试了你的代码,它工作了!编辑:但是Jay Blanchard告诉我应该替换if(!isset($_SESSION))SESSION_start();对于会话_start();啊,因为你没有检查num_rows()@Lucafraser,因为RamRaider添加了
如果($result->num_rows==1)
-我刚刚注意到,你和他的区别。你需要修复代码顶部的会话检查。没错——虽然我发现在包含文件或通过ajax调用文件时,会话中发生了奇怪的事情,但这似乎解决了这个问题。无论如何,这不是重点-op需要检查记录集的结果,而不是对$resultSigh的布尔值进行操作,true,但是代码,正如它所显示的,可能会在以后出现问题。如果测试因任何原因失败,所有登录功能都将被跳过。@RamRaider我刚刚尝试了你的代码,它工作了!编辑:但是Jay Blanchard告诉我应该替换if(!isset($_SESSION))SESSION_start();对于会话_start();啊,因为你没有检查num_rows()@Lucafraser,因为RamRaider添加了
if($result->num_rows==1)
-我刚刚注意到了你和他之间的区别。我知道我应该使用real_escape_字符串,但我现在只关注登录部分!很高兴你知道这件事。我的解决方案对你有用吗?真奇怪,在尝试了我的解决方案后,发生了什么?我们需要反馈来帮助你进步。好吧,当我使用你的代码@Phiter Fernandes时,我不能再登录了。我知道我应该使用真正的转义字符串,但我现在只关注登录部分!很高兴你知道这件事。我的解决方案对你有用吗?真奇怪,在尝试了我的解决方案后,发生了什么?我们需要反馈来帮助你进步。好吧,当我使用你的代码@Phiter Fernandes时,我不能再登录了。它不仅能防止错误的输入,还能防止正确的输入。