反正是否包含一个php文件中的某些变量并在另一个文件中接收它们?

时间:2017-07-14 20:59:20

标签: php variables

无论如何要包含一个php文件中的某些变量并在另一个文件中接收它们?我知道你可以包含一个完整的文件,但在我的情况下程序将无法工作,因为程序会尝试重定向到该文件中的user_details.php。所以我尝试在user_details.php中做include 'process.php.$username';,但这不起作用。

感谢任何帮助。

这是我的代码:

process.php     

    $username = $_POST["user"];
    $password = $_POST["pass"];

    $username = stripcslashes($username);
    $password = stripcslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    mysql_connect("localhost", "root", "");
    mysql_select_db("message_board");

    $result = mysql_query("select * from users where username = '$username' and password = '$password'") or die("Failed to query database ".mysql_error());

    $row = mysql_fetch_array($result);

    if ($row["username"] == $username and $row["password"] == $password) {
        echo "Login success! Welcome ".$row["username"], " and ".$row["user_permissions"];
        header('location: user_details.php');
    } else {
        echo "Failed to login! \n";
        echo '<a href="login.php">Back to Login</a>';
    }

user_details.php

<html>
<head>
    <title>user_details</title>
</head>
<body>
    <div id="main">
        <?php
            include 'process.php.$username';
            include 'process.php.$password';

            mysql_connect("localhost", "root", "");
            mysql_select_db("message_board");

            $result = mysql_query("select * from users where username = '$username' and password = '$password'") or die("Failed to query database ".mysql_error());

            $row = mysql_fetch_array($result);

            echo "user permissions: ".$row["username"].$row["user_permissions"];
        ?>
        </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

查看sessions,它允许您在请求之间传递变量(这意味着在&#34;文件和#34之间)。

例如:

<强> process.php:

<?php
session_start(); // very important - you need to start session!

// do something on database

// assign something to session variable
$_SESSION['data'] = $something;

header('Location: user_details.php');
?>

<强> user_details.php:

<?php
session_start(); // like before

// get variable from session
$data = $_SESSION['data'];

// do something with $data
?>

Here你可以找到更多相关信息。