dqnrk44682 2013-08-27 23:59
浏览 65
已采纳

PHP在foreach循环中查找密码

I have a very simple login script, it requires just a password and thats it. The user will attempt to login and when the user enters the wrong password the php script will append the password to a text file but first the script checks whether the password is already in the file and if it is, the script will then add 1 to the number of attempts in the file. My problem is i am trying to find a way to check every password and then add the password but not check each individual password and add the password right away. I am using a class file found at this website My code is below.

<!DOCTYPE html>
<html>
<head>
    <title>Website</title>
</head>
<body>
<form method="post" action="Assets/Scripts/PHP/Login.php">
    <table>
        <tr>
            <td>
                <label for="Password">Password:</label>
            </td>
            <td>
                <input type="password" name="password" placeholder="Password" id="Password" />
            </td>
        </tr>

        <tr>
            <td>

            </td>
            <td>
                <input type="submit" name="submit" value="Log In" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>

Login.php

<?php
$Password = $_POST['password'];
$Submit = $_POST['submit'];

if(isset($Submit))
{
    if($Password != '' && $Password == 'Password')
    {
        session_start();
        $_SESSION['Login'] = 'True';
        $Login = $_SESSION['Login'];
        if(isset($Login))
        {
            header("location:../../../main.php");
        }
        else
        {
            header("location:../../../index.html");
        }
    }
    else
    {
        include("../../../MyTXT.php");
        $File = new MyTXT("../../Texts/Passwords.txt");
        foreach($File->rows as $Row)
        {
            if($Row != $Password)
            {
                $File->add_row(array($Password, "1"));
                $File->save();
            }
            else
            {

            }
        }
        echo 'The Password Is Incorrect';
        echo '<br />';
        echo '<a href="../../../index.html">Go Back</a>';
    }
}
else
{
    header("location:../../../index.html");
}

Txt File

Password:|:Attempts

Test:|:26

Test123:|:2

Test456:|:5
  • 写回答

1条回答 默认 最新

  • dongshi9526 2013-08-28 01:00
    关注

    Is there a specific reason you're using the code from the link? It looks like a LOT of code for what is a simple task.

    Try these:

    Read contents of the file into a string: http://php.net/manual/en/function.file-get-contents.php

    Read contents of file into an array: http://php.net/manual/en/function.file.php

    I'm still not exactly sure how you're checking if their password is correct, as your file contains incorrect passwords, however, a quick example (v quick..) of how to check for a string in a file:

    $strPassword = '123test';
    $strFilename = 'pass.txt';
    $strFileContents = file_get_contents($strFilename);
    
    if(strpos($strFileContents, $strPassword)) 
      {
       //$strPassword is there, log them in/whatever
      }
    else
      {
        //password not there
      }
    

    However if you want/need to use that script from that site, then in the downloads there are examples of how to use it. You should study those and write code with them, and if you are stuck, ask specific questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输