Php 显示意外结果的var_dump

Php 显示意外结果的var_dump,php,windows,Php,Windows,我正准备创建一个简单的应用程序,使用基于文件的身份验证来测试登录和注销,但我遇到了一点麻烦,我无法理解 下面是我用来测试我的应用程序的文本文件的内容,名为filetest_02.txt 我还使用PHP脚本进行测试 下面是login.php的内容 下面是authenticate.inc.php的内容 问题是,当我单击login.php上的login按钮时,会得到以下输出 为什么var_dump输出显示字符串“david”的长度在修剪后等于8到8?是什么导致了这一切? 谢谢。var\u dump可

我正准备创建一个简单的应用程序,使用基于文件的身份验证来测试登录和注销,但我遇到了一点麻烦,我无法理解

下面是我用来测试我的应用程序的文本文件的内容,名为filetest_02.txt

我还使用PHP脚本进行测试 下面是login.php的内容

下面是authenticate.inc.php的内容

问题是,当我单击login.php上的login按钮时,会得到以下输出

为什么var_dump输出显示字符串“david”的长度在修剪后等于8到8?是什么导致了这一切?
谢谢。

var\u dump可能向您展示了带有空格的david的初始化,但您可以看到它随后识别出了它的剪切。这是否在代码中做了不必要的事情

此外,如果这是一个实际的网站,你只是不玩,我不会存储在你的文件中的密码,没有他们被盐和散列

另外,请参考PHP手册:

下面是几句话:。。。 string16示例字符串 string11你好,世界

这是几句话:。。。 以下是几句话: 细绳 细绳 string14示例字符串


var_dump显示了初始化和之后的trims

是的,我在玩,但正如你从我写的代码中看到的,我应该用我的表单中的用户名和密码测试文件中的用户名和密码是否相等,我认为没有设置会话的原因是因为文件中的用户名和表单中的用户名明显不相等,因为存在长度。请调试并跟踪您的代码。我有一个登录系统,但它看起来和你的太不一样了,无法共享。我会调试它,但我没有文件。
david, codeslave
chris, bigboss
    <?php

     $error = '';

    if ( isset($_POST['login']) ) {
    session_start();
    $username = trim( $_POST['username'] );
    $password = trim( $_POST['pwd']  );
    // location of usernames and passwords
    $userlist = 'C:/private/filetest_02.txt';
    // location to redirect on success
    $redirect = 'http://localhost/php/powers/solutions/ch09/menu.php';
    require_once './includes/authenticate.inc.php';
    }

   ?>
   <!DOCTYPE HTML>
   <html>
    <head>
        <meta charset="utf-8">
        <title>Login</title>
    </head>
    <body>
        <?php if ( $error ) : ?>
        <p><?php echo $error; ?></p>
        <?php endif; ?>
        <form id="form1" method="post" action="">
            <p>
                <label for="username">Username:</label>
                <input type="text" name="username" id="username">
            </p>
            <p>
                <label for="pwd">Password:</label>
                <input type="password" name="pwd" id="pwd">
            </p>
            <p>
                <input name="login" type="submit" id="login" value="Log in">
            </p>
        </form>
    </body>
</html>
<?php

if ( !file_exists($userlist) || !is_readable($userlist) ) {
    $error = 'Login facility unavailable. Please try later.';
} else {
    // read the file into an array called $users
    $users = file( $userlist );
    // loop through the array to process each line
    for ( $i = 0; $i < count($users); $i++ ) {
        // separate each element and store in a temporary array
        $tmp = explode( ', ', $users[$i] );
        var_dump($tmp);
        $name = trim($tmp[0]);
        $pwd = trim($tmp[1]);
        var_dump( $name, $pwd );
        var_dump( $_POST );
        // check for a matching record
        if ( $name == $username && $pwd == $password ) {
            $_SESSION['authenticated'] = 'Jethro Tull';
            session_regenerate_id();
            break;
        }
    }
    // if the session variable has been set, redirect
    if ( isset($_SESSION['authenticated']) ) {
        /* header( "Location: $redirect" );
        exit; */
    } else {
        $error = 'Invalid username or password.';
    }
}