PHP阅读TXT文件,并检查用户名密码存在 [英] PHP Read txt file and check if username password exists

查看:668
本文介绍了PHP阅读TXT文件,并检查用户名密码存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆存放在一个txt文件的密码和用户名,我需要检查匹配。我知道这是不是一个安全的方法,但其对靠在目的。

这code写的用户名和密码到一个txt文件

 如果(使用isset($ _ POST ['注册']))//
   {
    $ USER = $ _ POST [用户名];
    $密码= $ _ POST ['密码'] PHP_EOL。
    $跳频=的fopen(file.txt的,A +);
    FWRITE($ FH,$用户,$密码); //写txtfile注册表    FCLOSE($ FH);
 }

下面是遇到问题了code IM。

用户名和密码存储这样在结果

  USERNAME1 passwords1 USERNAME2密码2等。

所以,我需要检查,如果结果[0] = userinput而且如果它存在,如果检查结果[1] = passwordInput等。

 如果(使用isset($ _ POST ['LOGG']))
   {
   // $ _ SESSION [用户名] = $用户;
   // $ _ SESSION ['密码'] = $的密码;    $文件=的file_get_contents(file.txt的); //返回一个字符串    $结果=爆炸(,$文件);
    $ boolC = FALSE;    为($ I = 0; $ I<计数($结果); $ I ++)
    {
   如果($结果[$ i] === $用户)
   {
    $ boolC =真;
    $ I = $ I + 1;
    如果($ boolC ===真)
    {
        如果($结果[$ i] === $密码)
        {
            回声用户名和密码正确;
        }
    }   }
     }  }


解决方案

考虑在有密码的空间,因此任何符号的可能性。也许你应该各执一个换行符(\\ n)。

 如果($结果[$ i] === $用户放大器;&安培; $结果[$ I + 1] === $密码)

语句的第一部分具有用于第二个被评估为真。
$ i变量是从来没有改变过。

  $文件=USERNAME1 passwords1 USERNAME2密码2;
$结果=爆炸(,$文件);
$ USER =USERNAME1;
$密码=passwords1;为($ I = 0; $ I<计数($结果); $ I ++){    如果($结果[$ i] === $用户放大器;&安培; $结果[$ I + 1] === $密码)
    {        回声用户名和密码正确;
   }
}

I have a bunch of passwords and usernames stored in a txt file and i need to check for matches. I know this is not a safe method but its for leaning purposes.

This code writes the usernames and passwords to a txt file

  if(isset($_POST['register'])) //  
   {
    $user  = $_POST['username'];
    $password=$_POST['password'].PHP_EOL;
    $fh = fopen("file.txt","a+");
    fwrite($fh,$user." ".$password); //write to txtfile

    fclose($fh);
 }

Here's the code im having trouble with.

Username and Passwords are stored like this in results

      username1 passwords1 username2 password2 etc. 

So I need to check if result[0] = userinput AND if it exists check if result[1] = passwordInput and so on.

   if(isset($_POST['Logg']))
   {
   //$_SESSION['username']=$user; 
   //$_SESSION['password']=$password;

    $file = file_get_contents("file.txt"); // Returns a string

    $result = explode(" ",$file);
    $boolC= false;

    for($i=0; $i< count($result); $i++)
    {
   if($result[$i] === $user)
   {
    $boolC=true;
    $i =$i+1; 
    if($boolC === true)
    {
        if($result[$i]===$password)
        {
            echo 'Password and Username Correct';
        }
    }

   }
     }

  }

解决方案

Consider the possibility of having a space in the password so as any symbols. Perhaps you should split on a line break ("\n").

if( $result[$i] === $user && $result[$i+1] === $password )

The first part of the statement has to be true for the second one to be evaluated. The $i variable is never changed

$file = "username1 passwords1 username2 password2";
$result = explode(" ",$file);
$user = "username1";
$password = "passwords1";

for($i=0; $i< count($result); $i++){

    if($result[$i] === $user && $result[$i+1] === $password)
    {

        echo 'Password and Username Correct';
   }
}

这篇关于PHP阅读TXT文件,并检查用户名密码存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆