从另一个文件获取变量

时间:2014-12-07 14:53:17

标签: php

我想从process.php获取变量username = $_POST["username"]以回显第二个文件(change.php),但它不起作用。错误是" Undefined index:用户名在C:\ xampp \ htdocs \ OOP \ Class User \ process.php的第2行和第34行;这很奇怪,因为change.php和home.php之间没有太大的差异,而在home.php中,这种方法很好。我做错了什么?

process.php

<?php
$username       = $_POST["username"];
$password       = $_POST["password"];
$profileUser    = "profiles/user/$username.txt";
$profileWorkman = "profiles/workman/$username.txt";

if ( isset ( $_REQUEST ["login"] ) && isset ( $_REQUEST ["username"] ) && isset ( $_REQUEST ["password"] ) ) {
    if ( file_exists( $profileUser ) ) {
        $fpUser      = fopen( $profileUser, "a+" );
        $accountUser = file_get_contents( $profileUser );
        if ( strstr( $accountUser, $username ) && @strstr( $accountUser, $password ) ) {
            require "home.php";
        } else {
            echo "<p>Incorrect Password</p>";
        }
    } else if ( file_exists( $profileWorkman ) ) {
        $fpWorkman      = fopen( $profileWorkman, "a+" );
        $accountWorkman = file_get_contents( $profileWorkman );
        if ( strstr( $accountWorkman, $username ) && @strstr( $accountWorkman, $password ) ) {
            require "home.php";
        } else {
            require "index.html";
            echo "<p>Incorrect Password</p>";
        }
    } else {
        require "index.html";
        echo "<p>Incorrect Username and/or Password</p>";
    }
}

if ( isset ( $_POST ['register'] ) ) {
    require 'register.html';
}

change.php

<?php

$username ["username"];
echo $username;

home.php

<html>
<head>
    <title>Home</title>
    <link rel="stylesheet" href="styles/home.css"/>
</head>
<body>
<?php
$username = $_POST["username"];
echo "<p>Hello $username</p>";
?>
<a href="change.html" name="changeProfile">Change profile</a>

<form action="comments.php">
    <textarea name="comment_box" cols="30" rows="10" placeholder="Write a comment..."></textarea>
    <input type="submit" name="post_comment" value="Submit"/>
</form>
<h2>Comments:</h2>
</body>
</html>

0 个答案:

没有答案