关联数组检查登录组合是否正确

这是我在这个网站上的第一篇文章和问题。我现在正在做我的学校作业,我必须检查登录凭据是否与关联数组中列出的凭据相同。我到处搜索,但找不到答案。有人可以告诉我该怎么做吗?


这是我的 PHP 代码:


// Create associative array


$loginCombinations = array("Lucas"=>"lucas3284", "Bob"=>"bob9584", "Frits"=>"frits1842", "Kees"=>"kees1394", "Sjakie"=>"sjakie1953", "Bas"=>"bas6382", "Peter"=>"peter2391", "Robbie"=>"robbie1289", "Jan"=>"jan1462", "Tim"=>"tim9324");


// Create message (login succesful / login failed)


$message = "";


// Create foreach loop


foreach($loginCombinations as $username => $password)

{


}

这是我的 HTML 代码:


<form action="login.php" method="get">

    <table>

        <tr>

            <td>


            </td>

            <td>

                <?php

                echo $message;

                ?>

            </td>

        </tr>

        <tr>

            <td>

                <label for="username">username</label>

            </td>

            <td>

                <input type="text" id="username" name="username">

            </td>

        </tr>

        <tr>

            <td>

                <label for="password">password</label>

            </td>

            <td>

                <input type="password" id="password" name="password">

            </td>

        </tr>

        <tr>

            <td>


            </td>

            <td>

                <input type="submit">

            </td>

        </tr>

    </table>

</form>


函数式编程
浏览 62回答 2
2回答

RISEBY

首先,将GET方法更改为POSTas<form action="login.php" method="POST">以在请求有效负载中发送数据而不是作为安全的 GET 参数。因此,您可以将if条件放入foreach循环中以相应地检查和回显成功消息。<?php&nbsp;$found = false;foreach($loginCombinations as $username => $password){&nbsp; &nbsp; if($_POST['username'] == $username && $_POST['password'] == $password){&nbsp; &nbsp; &nbsp; &nbsp; echo "Yes, user found!!";&nbsp; &nbsp; &nbsp; &nbsp; $found = true;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}if(!$found){&nbsp; &nbsp; echo "No user found!!";}更新:name向您的提交按钮添加一个属性,submit比如<input type="submit" name="submit">。现在,您需要添加一个附加if条件来检查数据是否实际发布。<?php&nbsp;if(isset($_POST['submit'])){&nbsp; &nbsp; $found = false;&nbsp; &nbsp; foreach($loginCombinations as $username => $password){&nbsp; &nbsp; &nbsp; &nbsp; if($_POST['username'] == $username && $_POST['password'] == $password){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Yes, user found!!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(!$found){&nbsp; &nbsp; &nbsp; &nbsp; echo "No user found!!";&nbsp; &nbsp; }}更新#2:正如 所指出的@Nigel Ren,您可以简单地进行isset检查。if(isset($loginCombinations[$_POST['username']],$loginCombinations[$_POST['password']])){&nbsp; &nbsp;echo "user found";}else{&nbsp; &nbsp;echo "user not found";}

扬帆大鱼

由于您的数组由用户名索引,因此无需进行循环。首先检查是否设置了用户名元素,然后检查匹配的密码...$userName = $_GET['username'] ?? '';$message = "";if ( isset($loginCombinations[$userName]) &&&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $loginCombinations[$userName] === $_GET['password']) {&nbsp; &nbsp; $message = "user login correct";}else&nbsp; &nbsp; {&nbsp; &nbsp; $message = "user login incorrect";}
打开App,查看更多内容
随时随地看视频慕课网APP