Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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文件进行身份验证的Loginscript_Php_Nosql - Fatal编程技术网

Php 使用txt文件进行身份验证的Loginscript

Php 使用txt文件进行身份验证的Loginscript,php,nosql,Php,Nosql,我想通过使用文本文件使用NoSQL创建一个简单的登录脚本。 目前,用户凭据存储在代码中,而我无法将其存储在外部 我的代码: <?php $error = ""; if(isset($_POST['username'],$_POST['password'])){ $user = array( "user" => "demo", "pass"=>"demo"

我想通过使用文本文件使用NoSQL创建一个简单的登录脚本。 目前,用户凭据存储在代码中,而我无法将其存储在外部

我的代码:

<?php
$error = "";
if(isset($_POST['username'],$_POST['password'])){

    $user = array(
                    "user" => "demo",
                    "pass"=>"demo"          
            );
    $username = $_POST['username'];
    $pass = $_POST['password'];
    if($username == $user['user'] && $pass == $user['pass']){
        session_start();
        $_SESSION['simple_login'] = $username;
        header("Location: home.php");
        exit();
    }else{
        $error = '<div class="alert alert-danger">Invalid Login</div>';
    }
}
?>


任何建议都将不胜感激,谢谢

既然OP说安全不是问题,那么他/她可以使用此代码。
但此代码不应在实时网页上使用,因为它有许多安全缺陷。

您可以使用file_get_contents()读取文本磁贴。
在我的示例中,我使用
作为用户名和密码之间的分隔符,因此我可以使用
分解它们。
我使用array_combine()创建与关联命名键相同的数组。
别说了

<?php
$error = ""; 
if (isset($_POST['username'],$_POST['password'])){
    $arr = array("user", "pass");
    $file_data = file_get_contents("path_and_filename.txt");

    $user = array_combine($arr, explode(",", $file_data));
    /*$user = array(
                     "user" => "demo",
                     "pass"=>"demo"          
            );*/
    $username = $_POST['username'];
    $pass = $_POST['password'];
    if($username == $user['user'] && $pass == $user['pass']){
       session_start();
        $_SESSION['simple_login'] = $username;
        header("Location: home.php");
        exit();
    }else{
        $error = '<div class="alert alert-danger">Invalid Login</div>';
    }
}
?>


请注意,
和单词之间没有空格。

因此,您希望将用户名和密码从“无人”的php文件中移动,但您可以打开并将其放置在任何人都可以打开的文本文件中(使用正确的url)?是的,我想将其移动到文本文件-安全性不会成为问题。
demo,demo
//username,password