Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
使用post从php读取文本文件_Php_File_Post_Text_Flat File - Fatal编程技术网

使用post从php读取文本文件

使用post从php读取文本文件,php,file,post,text,flat-file,Php,File,Post,Text,Flat File,我的代码有什么问题?我在同一个文件夹中有两个文件:index.php和pass.txt: qwerty 这是pass.txt: qwerty 这是index.php: <?php $password=file_get_contents('pass.txt'); session_start(); if (isset($_SESSION['timeout'])) { if ($_SESSION['timeout'] + 10 < time()) { se

我的代码有什么问题?我在同一个文件夹中有两个文件:index.php和pass.txt:

qwerty
这是pass.txt:

qwerty
这是index.php:

<?php 

$password=file_get_contents('pass.txt');

session_start();
if (isset($_SESSION['timeout'])) {
    if ($_SESSION['timeout'] + 10 < time()) {
        session_destroy(); } }
else {
    $_SESSION['pass']="" ;  $_SESSION['timeout']=time(); }


if (isset($_POST["pass"])) {
    $_SESSION['pass']=$_POST['pass'] ; 
}

if($_SESSION['pass'] == $password)  {
    echo 'you are logged in';
} else {
    echo'<form method="POST" action="">
        <input type="password" name="pass">
        </form>';
}

?>

问题:当我在输入字段中写入'qwerty'并提交时,它不会显示“您的ale登录”

这只是一个有待进一步发展的语法问题,并不是为了保护任何东西


其他回答的问题并没有解决我的问题

在index.php启动会话的开头
session_start()
要在php中比较字符串,请使用
strcmp()

if(strcmp($\u会话['pass'],$password)){
回显“您已登录”;
}否则{
回声'
';
}
使用此代码-

<?php 

$password=file_get_contents(realpath( __DIR__.'/pass.txt' ),FILE_TEXT | FILE_SKIP_EMPTY_LINES);

if (isset($_POST["pass"])) {
    $_SESSION['pass']=$_POST['pass'] ; 
}

if($_SESSION['pass'] == $password)  {
    echo 'you are logged in';
} else {
    echo'<form method="POST" action="">
        <input type="password" name="pass">
        </form>';
}

?>

我认为问题可能是调用了
文件获取内容
——我尝试了以下方法,它似乎运行正常。(哎呀,本例中忘记了
会话\u start()



问题出在哪里?您没有在问题中说明这一点。虽然它已被理解、修改,但很抱歉,xxx您在
echo($\u会话['pass'])中得到
qwerty
?您已经在页面的最顶端找到了
会话\u start()
,是吗?是的,我在问题中添加了完整的代码已经添加到问题中,似乎不是问题。。。必须与file reading.bingo有关,尽管对meIt来说相当复杂,但它本质上与您的代码相同-除了
文件内容
。您认为哪些位特别“复杂”?它不起作用,因为您替换的方法是
$password=file\u get\u contents('pass.txt',true)
当@RamRaider以
$password=file\u get\u内容(realpath(\uu DIR\uu.'./pass.txt')、file\u TEXT | file\u SKIP\u空行)接受答案时
<?php
        session_start();

        if( isset( $_SESSION['timeout'] ) && $_SESSION['timeout'] + 10 < time() ) session_destroy();
        else {
            $_SESSION['pass']="" ;
            $_SESSION['timeout']=time();
        }

        $password=file_get_contents( realpath( __DIR__.'/pass.txt' ), FILE_TEXT | FILE_SKIP_EMPTY_LINES );
        echo 'The password from the text file: '. $password;


        if( isset( $_POST["pass"] ) ) $_SESSION['pass']=$_POST['pass'] ; 

        if( strlen( $password ) > 0 && trim( $_SESSION['pass'] ) === trim( $password ) )  {
            echo 'you are logged in';
        } else {
            /* for dev I use a local file, aliased as /stackoverflow/ */
            echo'<form method="POST" action="">
                    <input type="password" name="pass">
                    <input type="submit" value="login">
                </form>';
        }
?>