简体   繁体   English

在 PHP 页面之间传递信息

[英]Passing Information Between PHP Pages

How do I pass information between PHP pages?如何在 PHP 页面之间传递信息?

For example, I have a PHP script to process login input from a form, and then a separate PHP script to process further input for the user.例如,我有一个 PHP 脚本来处理来自表单的登录输入,然后有一个单独的 PHP 脚本来处理用户的进一步输入。 However, I want the second PHP file to receive the input from the login form.但是,我希望第二个 PHP 文件接收来自登录表单的输入。 In essence, I do not want the same script being run twice for the login.本质上,我不希望相同的脚本为登录运行两次。

You are looking for POST and GET variables, it's done in the method parameter of your HTML form:您正在寻找 POST 和 GET 变量,它在您的 HTML 表单的方法参数中完成:

login.php login.php

<form name="myform" action="secondpage.php" method="post">
    <div>Username: <input type="text" name="username" value="" /></div>
    <div>Password: <input type="password" name="password" value="" /></div>
</form>

Then in this other page:然后在另一个页面中:

secondpage.php第二页.php

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
if ($username != '') {       
    // do your validations here
}

Explanation解释

When you use the GET method, the parameters are visible in the URL, so let's say we change the method="GET" in login.php, you'll end up with something like secondpage.php?username=jsmith&password=1234. When you use the GET method, the parameters are visible in the URL, so let's say we change the method="GET" in login.php, you'll end up with something like secondpage.php?username=jsmith&password=1234. And then you could get the values using $_GET['username'] .然后您可以使用$_GET['username']获取值。

Using POST makes it possible to send larger quantity of data (there is a vague limit to the size of a URL) and it's not visible in the URL.使用 POST 可以发送更大量的数据(对 URL 的大小有一个模糊的限制),并且在 URL 中不可见。 You should note though that it's still sent in clear text , so it does not means it's secure.您应该注意,它仍然以明文形式发送,因此并不意味着它是安全的。

POST and GET were made for different purposes. POST 和 GET 是为了不同的目的而制作的。 GET should be use to extract information that you could want to extract again in the future, information that is not special to this very instant.应该使用 GET 来提取您将来可能想要再次提取的信息,这些信息对于这一刻并不特殊。 It's useful to have mypage.php?product=123 because you'll potentially want to send this URL to a friend.拥有 mypage.php?product=123 很有用,因为您可能希望将此 URL 发送给朋友。 A POST should be used when you'll modify the state of data: updating a product, creating a new user, deleting an article and so on.当您要修改数据的 state 时,应使用 POST:更新产品、创建新用户、删除文章等。 It's something you want to happen once.这是你想要发生一次的事情。

Structure结构

In conclusion, I just want to add that normally you wouldn't necessarily want to use another PHP script just to avoid some code to run or not.总之,我只想补充一点,通常您不一定要使用另一个 PHP 脚本来避免某些代码运行或不运行。 So without knowing the specifics of your project, I can nevertheless say that you would probably want to do something like that to benefit from the same code (such as the form's HTML).因此,在不知道您的项目细节的情况下,我仍然可以说您可能想要做类似的事情以从相同的代码(例如表单的 HTML)中受益。

Please note it's simplified code.请注意它是简化的代码。

login.php login.php

<?php

    $error = false;
    $username = isset($_POST['username']) ? $_POST['username'] : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    // if, and only if something was posted... so not on first display
    if ($username != '') {       
        // do your validations here
        if ($properlyLogged) {
            session_start();
            $_SESSION['loggedAt'] = time();
            header('Location: http://localhost/secondpage.php');
            exit();
        } else {
            $error = true;
        }
    }

?>

<?php if($error): ?>Login failed. Please try again.<?php endif; ?>
<form name="myform" action="login.php" method="post">
    <div>Username: <input type="text" name="username" value="<?php echo($username) ?>" /></div>
    <div>Password: <input type="password" name="password" value="" /></div>
</form>

secondpage.php第二页.php

<?php
    session_start();
    if (!isset($_SESSION['loggedAt'])) {
        // if not properly logged in, return user to login
        header('Location: http://localhost/login.php');
        exit();
    }
?>
You are now logged in!

Hope that's what you were looking for!希望这就是你要找的!

You can pass information between pages using GET or POST methods.您可以使用 GET 或 POST 方法在页面之间传递信息。 GET would append the information you wish to pass as a querystring on the url such as: GET 将 append 您希望作为查询字符串传递给 url 的信息,例如:

loginprocess.php?id=JSmith&pword=HelloThere (this isn't exactly recommended for private information) loginprocess.php?id=JSmith&pword=HelloThere (不完全推荐用于私人信息)

The other method is to send the information via POST so that it is hidden from the querystring.另一种方法是通过 POST 发送信息,使其对查询字符串隐藏。

More examples can be seen here: http://www.tizag.com/phpT/postget.php更多示例可以在这里看到: http://www.tizag.com/phpT/postget.php

If the data isn't that large you could redirect the user to the 2nd page with the data passed via the URL (GET variables).如果数据不是那么大,您可以使用通过 URL(GET 变量)传递的数据将用户重定向到第二页。 Otherwise, just run the seconds method in the same page, and use a function to do the final parsing of the data which can be included as the above user suggests.否则,只需在同一页面中运行 seconds 方法,并使用 function 对可以包含的数据进行最终解析,如上述用户建议的那样。

Just a small extra to what was written before: the limit on the GET (parametrize URL) is a full URL, which means 1024 characters.只是对之前编写的内容进行了一点补充:GET(参数化 URL)的限制是完整的 URL,这意味着 1024 个字符。 If you need more than that, you have to use post.如果您需要更多,则必须使用 post。

You can take advantage of PHP sessions to share data amongst your PHP scripts.您可以利用 PHP 会话在 PHP 脚本之间共享数据。 Basic example below, read more here .下面的基本示例,请在此处阅读更多内容。

login.php: login.php:

<?php

// initializes the session //
session_start();

// save user name and password to session //
$_SESSION["username"] = 'someuser';
$_SESSION["password"] = 'somepassword';
$_sESSION["valid"] = true;

?>

secondpage.php:第二页.php:

<?php

// start session handler //
session_start();

// check for a valid session //
if (!isset($_SESSION["valid"])) header("Location: login.php\n\n");

// continue page code here //

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM