PHP - 代码在2个文件中工作,但不能在一个文件中工作

时间:2017-10-25 00:03:14

标签: php

终于让我的代码工作了。我不知道如果没有执行if / else语句中的else,我可以做什么来定义变量$ username / $ password。当页面加载时,它会显示消息"哎呀,出错了再试一次!"我意识到这样做是因为我声明$ username / $密码为NULL。我从代码中取出了那部分,但是我收到了错误消息" Undefined variable:username"现在我不确定我能做些什么来避免这两个错误。有没有人有任何想法?提前致谢。

<html>
<body>
<?php
echo "<form action='oneFile.php' method='post'>";
echo "<table border='0'>";
echo "<tr bgcolor='#cccccc'>";
echo "<td width='150'>Username: </td>";
echo "<td width='20'><input type='text' name='username' size='15'
maxlength='15'></td>";
echo "</tr><tr><tr bgcolor='#cccccc'>";
echo "<td width='150'>Password: </td>";
echo "<td width='20'><input type='password' name='password' size='15'
maxlength='15'></td>";
echo "</tr></tr><tr><td colspan='2' align='center'>
<input type='submit' value='Login'></td>
</tr>
</table>
</form>
</body>
</html>";
?>
<?php
$usernameArr = array( 1 => "elliez",
2 => "greatGuy",
3 => "blogger",
4 => "bob",
5 => "mike",
6 => "jane",
7 => "joe",
8 => "rachel",
9 => "james",
10 => "pizzaman2000");
$passwordArr = array( 1 => "tr789ial",
2 => "abc123",
3 => "23seventeen23",
4 => "12345",
5 => "Password123",
6 => "P@ssword123",
7 => "pass123",
8 => "123pass",
9 => "123P@ssword",
10 => "54321");

$varBool = false;


if(isset($_POST['submit'])){
$username = htmlspecialchars(trim($_POST['username']));
$password = htmlspecialchars(trim($_POST['password']));
trim($username);
trim($password);
}

for ( $i = 1; $i <= 10; $i++){
if ($username == $usernameArr[$i] && $password === $passwordArr[$i]){
$varBool = true;
}
}
If ($varBool){
echo "You have successfully logged in!";
}
else{
echo "Oops something went wrong try again!";
}

?>

2 个答案:

答案 0 :(得分:2)

请不要在生产中这样做。如果站点包含敏感数据,硬编码密码在许多管辖区域都是非常危险和非法的。

完全解决了这个问题,为什么你的代码不能正常工作的答案很简单。你需要输入这段代码:

for ( $i = 1; $i <= 10; $i++){
if ($username == $usernameArr[$i] && $password === $passwordArr[$i]){
$varBool = true;
}
}
If ($varBool){
echo "You have successfully logged in!";
}
else{
echo "Oops something went wrong try again!";
}

在这个区块内,在底部:

if(isset($_POST['submit'])){
$username = htmlspecialchars(trim($_POST['username']));
$password = htmlspecialchars(trim($_POST['password']));
trim($username);
trim($password);
}

因为您只想检查用户是否已提交表单。它在2个文件中工作的原因是因为第2个文件只是在提交时被调用,所以用户名&amp;密码不是空的。

另请查看code indentationforeach loops&amp; password hashing。这里有更多的错误(例如,为什么回应HTML而不仅仅是编写HTML?)但这些应该可以帮助您提高代码的可读性。

你最终会得到类似的东西:

<!DOCTYPE html>
<html>
  <body>
    <form action='<?= $_SERVER['PHP_SELF']; ?>' method='POST'>
      <table border='0'>
        <tr bgcolor='#cccccc'>
          <td width='150'>Username: </td>
          <td width='20'><input type='text' name='username' size='15'
maxlength='15'></td>
        </tr>
        <tr bgcolor='#cccccc'>
          <td width='150'>Password: </td>
          <td width='20'><input type='password' name='password' size='15'
maxlength='15'></td>
        </tr>
        <tr>
          <td colspan='2' align='center'>
            <input type='submit' value='Login' name='login'>
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

<?php
// IF THIS IS A PRODUCTION SITE USE PASSWORD HASHING AND A DATABASE.
$credentials = [
  'username' => 'password',
  ...
];

if (isset($_POST['login'])) {
  $loginSuccess = false;

  foreach ($credentials as $username => $password) {
    if ($_POST['username'] == $username && $_POST['password'] = $password) {
      $loginSuccess = true;
  }

  echo $loginSuccess ? "Successfully logged in." : "Failed to log in.";
}

哪个仍然不完美,但我希望你同意,比你在这里的内容更具可读性。

答案 1 :(得分:1)

这里有一些问题。

首先,大括号应使用isset()条件语句封装整个代码,并且您的提交按钮未命名,以便永远不会执行操作。

请参阅代码中的注释。

HTML stickler:你在</tr>中也有一个迷路</tr></tr><tr><td colspan='2' align='center'>。它不会破坏任何东西,但拥有适当的HTML标记是一件好事。

使用<!DOCTYPE>代替<html>也是不错的做法,因为有些浏览器会发出警告,例如:Start tag seen without seeing a doctype first. Expected "<!DOCTYPE html>"

<html>
    <body>
        <?php

            echo "<form action='oneFile.php' method='post'>";
            echo "<table border='0'>";
            echo "<tr bgcolor='#cccccc'>";
            echo "<td width='150'>Username: </td>";
            echo "<td width='20'><input type='text' name='username' size='15'
            maxlength='15'></td>";
            echo "</tr><tr><tr bgcolor='#cccccc'>";
            echo "<td width='150'>Password: </td>";
            echo "<td width='20'><input type='password' name='password' size='15'
            maxlength='15'></td>";
            echo "</tr><tr><td colspan='2' align='center'>
            <input type='submit' name='submit' value='Login'></td>
            </tr>
            </table>
            </form>
            </body>
            </html>";
        ?>
        <?php
            $usernameArr = array( 1 => "elliez",
            2 => "greatGuy",
            3 => "blogger",
            4 => "bob",
            5 => "mike",
            6 => "jane",
            7 => "joe",
            8 => "rachel",
            9 => "james",
            10 => "pizzaman2000");
            $passwordArr = array( 1 => "tr789ial",
            2 => "abc123",
            3 => "23seventeen23",
            4 => "12345",
            5 => "Password123",
            6 => "P@ssword123",
            7 => "pass123",
            8 => "123pass",
            9 => "123P@ssword",
            10 => "54321");

            $varBool = false;


            if(isset($_POST['submit'])){
                $username = htmlspecialchars(trim($_POST['username']));
                $password = htmlspecialchars(trim($_POST['password']));
                trim($username);
                trim($password);
                // } // this brace is misplaced

                for ( $i = 1; $i <= 10; $i++){
                    if ($username == $usernameArr[$i] && $password === $passwordArr[$i]){
                        $varBool = true;
                    }
                }
                If ($varBool){
                    echo "You have successfully logged in!";
                }
                else{
                    echo "Oops something went wrong try again!";
                }

            } // brace moved here

        ?>      

旁注:没有必要两次使用trim()