Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php密码作为数据库保存在txt文件中_Php_Database_Text_Passwords_Flat File - Fatal编程技术网

php密码作为数据库保存在txt文件中

php密码作为数据库保存在txt文件中,php,database,text,passwords,flat-file,Php,Database,Text,Passwords,Flat File,我有这个登录码(我意识到它确实不安全) 如何在.txt文件中存储多个密码 <?php $passwords = file('pass.txt'); # Check for session timeout, else initiliaze time session_start(); if (isset($_SESSION['timeout'])) { if ($_SESSION['timeout'] + 10 < time()) { session

我有这个登录码(我意识到它确实不安全)

如何在.txt文件中存储多个密码

   <?php 

$passwords = file('pass.txt');


# Check for session timeout, else initiliaze time
session_start();
if (isset($_SESSION['timeout'])) {
    if ($_SESSION['timeout'] + 10 < time()) {
        session_destroy(); } }
else {
    $_SESSION['pass']="" ;  $_SESSION['timeout']=time(); }

# Store POST data in session variables
if (isset($_POST["pass"])) {
    $_SESSION['pass']=hash('sha256',$_POST['pass']) ; }

# Check Login Data. Password is hashed (SHA256). In this case it is 'admin'.
$flag = 0;
foreach ($passwords as $pass) {
    if ($pass == $_SESSION['pass']) {
      $flag = 1;
    }
}

if ($flag == 1) {
echo 'session';}
else {
    echo'<form method="POST" action=""><input type="password" name="pass"></form>';}

?>

我先说,是的-你说得很对,它不安全。特别是如果密码存储在可通过web访问的txt文件中

我猜密码不必与用户名匹配,因此您可以简单地将密码存储在一个普通的txt文件中,或者(为了更安全起见)将其作为一个数组存储在包含的PHP文件中(如果其位置被破坏,则不会显示为纯文本)

对于纯文本,将文件读入数组

$passwords=file('path/to/file.txt')

或者将PHP文件包含在数组中(在本例中,该数组存储在名为$passwords的数组中)

然后设置一个标志并运行数组检查,并将最终条件替换为测试标志的条件

$flag = 0;
foreach ($passwords as $pass) {
    if ($pass == $_SESSION['pass']) {
      $flag = 1;
    }
}

if ($flag == 1) {
echo 'session';}
else {
    echo'<form method="POST" action=""><input type="password" name="pass"></form>';}
$flag=0;
foreach($pass作为$pass的密码){
如果($pass=$\会话['pass']){
$flag=1;
}
}
如果($flag==1){
回显“会话”;}
否则{
回音“;}

首先,我要说,是的-你很正确,这是不安全的。特别是如果密码存储在可通过web访问的txt文件中

我猜密码不必与用户名匹配,因此您可以简单地将密码存储在一个普通的txt文件中,或者(为了更安全起见)将其作为一个数组存储在包含的PHP文件中(如果其位置被破坏,则不会显示为纯文本)

对于纯文本,将文件读入数组

$passwords=file('path/to/file.txt');

或者将PHP文件包含在数组中(在本例中,该数组存储在名为$passwords的数组中)

然后设置一个标志并运行数组检查,并将最终条件替换为测试标志的条件

$flag = 0;
foreach ($passwords as $pass) {
    if ($pass == $_SESSION['pass']) {
      $flag = 1;
    }
}

if ($flag == 1) {
echo 'session';}
else {
    echo'<form method="POST" action=""><input type="password" name="pass"></form>';}
$flag=0;
foreach($pass作为$pass的密码){
如果($pass=$\会话['pass']){
$flag=1;
}
}
如果($flag==1){
回显“会话”;}
否则{
回音“;}

首先,我要说,是的-你很正确,这是不安全的。特别是如果密码存储在可通过web访问的txt文件中

我猜密码不必与用户名匹配,因此您可以简单地将密码存储在一个普通的txt文件中,或者(为了更安全起见)将其作为一个数组存储在包含的PHP文件中(如果其位置被破坏,则不会显示为纯文本)

对于纯文本,将文件读入数组

$passwords=file('path/to/file.txt');

或者将PHP文件包含在数组中(在本例中,该数组存储在名为$passwords的数组中)

然后设置一个标志并运行数组检查,并将最终条件替换为测试标志的条件

$flag = 0;
foreach ($passwords as $pass) {
    if ($pass == $_SESSION['pass']) {
      $flag = 1;
    }
}

if ($flag == 1) {
echo 'session';}
else {
    echo'<form method="POST" action=""><input type="password" name="pass"></form>';}
$flag=0;
foreach($pass作为$pass的密码){
如果($pass=$\会话['pass']){
$flag=1;
}
}
如果($flag==1){
回显“会话”;}
否则{
回音“;}

首先,我要说,是的-你很正确,这是不安全的。特别是如果密码存储在可通过web访问的txt文件中

我猜密码不必与用户名匹配,因此您可以简单地将密码存储在一个普通的txt文件中,或者(为了更安全起见)将其作为一个数组存储在包含的PHP文件中(如果其位置被破坏,则不会显示为纯文本)

对于纯文本,将文件读入数组

$passwords=file('path/to/file.txt');

或者将PHP文件包含在数组中(在本例中,该数组存储在名为$passwords的数组中)

然后设置一个标志并运行数组检查,并将最终条件替换为测试标志的条件

$flag = 0;
foreach ($passwords as $pass) {
    if ($pass == $_SESSION['pass']) {
      $flag = 1;
    }
}

if ($flag == 1) {
echo 'session';}
else {
    echo'<form method="POST" action=""><input type="password" name="pass"></form>';}
$flag=0;
foreach($pass作为$pass的密码){
如果($pass=$\会话['pass']){
$flag=1;
}
}
如果($flag==1){
回显“会话”;}
否则{
回音“;}

我个人不建议您将密码存储在.txt中,但我仍然可以帮助您..如果要使用文件处理存储多个密码,可以使用json函数来实现

function writeToFile($filename, $msg)
 { 
   $msgArray=array();
   if(file_exists($filename))
        {
        $arrMsg=json_decode(file_get_contents($filename),true);
        foreach ($arrMsg as $ob)
          {
             array_push($msgArray,$ob);
          }
        unlink($filename);
        }
   array_push($msgArray,$msg);
   $msgArrayJSON=json_encode($msgArray);
   // open file
   $fd = fopen($filename, "a");
   // write string
   fwrite($fd, $msgArrayJSON. PHP_EOL);
   // close file
   fclose($fd);
}
通过使用上述功能,您可以添加类似于

writeToFile('user.json', array("username"=>$id,"password"=>$name));
最后,您可以从文件中获取用户,如下所示

$user_array=json_decode(file_get_contents('user.json'),true);

我个人不建议您将密码存储在.txt中,但我仍然可以帮助您..如果要使用文件处理存储多个密码,可以使用json函数来实现

function writeToFile($filename, $msg)
 { 
   $msgArray=array();
   if(file_exists($filename))
        {
        $arrMsg=json_decode(file_get_contents($filename),true);
        foreach ($arrMsg as $ob)
          {
             array_push($msgArray,$ob);
          }
        unlink($filename);
        }
   array_push($msgArray,$msg);
   $msgArrayJSON=json_encode($msgArray);
   // open file
   $fd = fopen($filename, "a");
   // write string
   fwrite($fd, $msgArrayJSON. PHP_EOL);
   // close file
   fclose($fd);
}
通过使用上述功能,您可以添加类似于

writeToFile('user.json', array("username"=>$id,"password"=>$name));
最后,您可以从文件中获取用户,如下所示

$user_array=json_decode(file_get_contents('user.json'),true);

我个人不建议您将密码存储在.txt中,但我仍然可以帮助您..如果要使用文件处理存储多个密码,可以使用json函数来实现

function writeToFile($filename, $msg)
 { 
   $msgArray=array();
   if(file_exists($filename))
        {
        $arrMsg=json_decode(file_get_contents($filename),true);
        foreach ($arrMsg as $ob)
          {
             array_push($msgArray,$ob);
          }
        unlink($filename);
        }
   array_push($msgArray,$msg);
   $msgArrayJSON=json_encode($msgArray);
   // open file
   $fd = fopen($filename, "a");
   // write string
   fwrite($fd, $msgArrayJSON. PHP_EOL);
   // close file
   fclose($fd);
}
通过使用上述功能,您可以添加类似于

writeToFile('user.json', array("username"=>$id,"password"=>$name));
最后,您可以从文件中获取用户,如下所示

$user_array=json_decode(file_get_contents('user.json'),true);

我个人不建议您将密码存储在.txt中,但我仍然可以帮助您..如果要使用文件处理存储多个密码,可以使用json函数来实现

function writeToFile($filename, $msg)
 { 
   $msgArray=array();
   if(file_exists($filename))
        {
        $arrMsg=json_decode(file_get_contents($filename),true);
        foreach ($arrMsg as $ob)
          {
             array_push($msgArray,$ob);
          }
        unlink($filename);
        }
   array_push($msgArray,$msg);
   $msgArrayJSON=json_encode($msgArray);
   // open file
   $fd = fopen($filename, "a");
   // write string
   fwrite($fd, $msgArrayJSON. PHP_EOL);
   // close file
   fclose($fd);
}
通过使用上述功能,您可以添加类似于

writeToFile('user.json', array("username"=>$id,"password"=>$name));
最后,您可以从文件中获取用户,如下所示

$user_array=json_decode(file_get_contents('user.json'),true);

您可以像这样使用PHP函数fopen()和fwrite():

<?php
    /* the a is used to place the writer at the end of the file, w would place it
    at the beginning of the file overwriting already stored data*/
    $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
    $txt = "My Mother\n";
    fwrite($myfile, $txt);
    // or direct input
    fwrite($myfile, "My Sister\n");
    fclose($myfile);
?>

请注意\n转到文本文件中的下一行


当然,数据库是最好的方法,但是如果你想使用文本文件,我强烈建议你还是尝试创建一些结构。看看XML或JSON。

你可以像这样使用PHP函数fopen()和fwrite():

<?php
    /* the a is used to place the writer at the end of the file, w would place it
    at the beginning of the file overwriting already stored data*/
    $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
    $txt = "My Mother\n";
    fwrite($myfile, $txt);
    // or direct input
    fwrite($myfile, "My Sister\n");
    fclose($myfile);
?>

请注意\n转到文本文件中的下一行

当然,数据库是最好的方法,但是如果你想用文本文件,我强烈建议你还是尝试创建一些结构