dqnrk44682 2013-08-27 15: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-27 17: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部