【问题标题】:read text file from php with post使用 post 从 php 读取文本文件
【发布时间】:2015-12-11 17:07:41
【问题描述】:

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

这是 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”并提交时,它不显示“您的啤酒已登录”

这只是一个用于进一步开发的语法问题,并非旨在保护任何东西。

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

【问题讨论】:

  • 有什么问题?您没有在问题中说明这一点。
  • 以为是理解,修改,抱歉,XXX
  • 你在echo($_SESSION['pass']); 中得到qwerty 吗?
  • 你在页面顶部有session_start(),对吗?
  • 是的,我在问题中添加了完整代码

标签: php file post text flat-file


【解决方案1】:

使用此代码-

<?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>';
}

?>

【讨论】:

  • 当@RamRaider 用$password=file_get_contents(realpath( __DIR__.'/pass.txt' ),FILE_TEXT | FILE_SKIP_EMPTY_LINES); 接受答案时,由于您替换$password=file_get_contents('pass.txt',true); 的方法,它不起作用。
【解决方案2】:

我认为问题可能是对 file_get_contents 的调用 - 我尝试了以下操作,它似乎运行正常。 (哎呀,这个例子忘了session_start()

<?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>';
        }
?>

【讨论】:

  • 宾果游戏,虽然对我来说相当复杂
  • 它基本上和你的代码一样——除了file_get_contents。你觉得哪些部分特别“复杂”?
【解决方案3】:

在你的 index.php 开始会话session_start(); 要在 php 中比较字符串,请使用 http://php.net/manual/en/function.strcmp.php strcmp();

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

【讨论】:

  • 已经添加到问题中了,好像不是问题……一定是文件读取有关。
猜你喜欢
  • 2020-05-30
  • 2011-02-08
  • 1970-01-01
  • 2011-05-05
  • 2011-08-07
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
相关资源
最近更新 更多