传递PHP页面之间的信息 [英] Passing Information Between PHP Pages

查看:112
本文介绍了传递PHP页面之间的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在PHP页面之间传递信息?



例如,我有一个PHP脚本来处理来自表单的登录输入,然后是一个单独的PHP脚本处理用户的进一步输入。但是,我希望第二个PHP文件接收来自登录表单的输入。实质上,我不希望相同的脚本在登录时运行两次。

解决方案

您正在寻找POST和GET变量,它在HTML表单的方法参数中完成:

login.php

 < form name =myformaction =secondpage.phpmethod =post> 
< div>用户名:< input type =textname =usernamevalue =/>< / div>
< div>密码:< input type =passwordname =passwordvalue =/>< / div>
< / form>

然后在这个其他页面:

secondpage.php

  $ username = isset($ _ POST ['username'])? $ _POST ['username']:''; 
$ password = isset($ _ POST ['password'])? $ _POST ['password']:'';
if($ username!=''){
//在这里进行验证
}



解释



当您使用GET方法时,参数在URL中可见,所以假设我们更改method =GET在login.php中,您最终会得到类似于secondpage.php?username = jsmith& password = 1234的内容。然后,您可以使用 $ _ GET ['username']

获取值。使用POST可以发送更大量的数据(模糊限制与网址大小有关)并且它在URL中不可见。您应该注意,虽然仍然以明文形式发送,但并不意味着它是安全的。



POST和GET用于不同目的。应该使用GET来提取未来可能需要再次提取的信息,这些信息在这个瞬间并不是特别的。 mypage.php?product = 123是很有用的,因为您可能需要将此URL发送给朋友。当您修改数据状态时应该使用POST:更新产品,创建新用户,删除文章等。这是你想要发生的一次。



结构



总之,我只想补充一点,不一定要使用另一个PHP脚本来避免一些代码运行。因此,如果不知道项目的具体细节,我可以说你可能想要做类似的事情来从相同的代码中获益(比如表单的HTML)。



请注意它是简化的代码。



login.php

 <?php 

$ error = false;
$ username = isset($ _ POST ['username'])? $ _POST ['username']:'';
$ password = isset($ _ POST ['password'])? $ _POST ['password']:'';
// if,并且只有当某些东西被发布时...所以不是第一次显示
if($ username!=''){
//在这里做你的验证
if($ properlyLogged){
session_start();
$ _SESSION ['loggedAt'] = time();
header('Location:http://localhost/secondpage.php');
exit();
} else {
$ error = true;
}
}

?>

<?php if($ error):?>登录失败。请重试。<?php endif; ?>
< form name =myformaction =login.phpmethod =post>
< div>用户名:< input type =textname =usernamevalue =<?php echo($ username)?> />< / DIV>
< div>密码:< input type =passwordname =passwordvalue =/>< / div>
< / form>

secondpage.php

 <?php 
session_start();
if(!isset($ _ SESSION ['loggedAt'])){
//如果没有正确登录,返回用户登录
header('Location:http:// localhost /的login.php');
exit();
}
?>
您现在已登录!

希望这就是您要找的!


How do I pass information between PHP pages?

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. However, I want the second PHP file to receive the input from the login form. 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:

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

$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. And then you could get the values using $_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. 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. 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. It's useful to have mypage.php?product=123 because you'll potentially want to send this URL to a friend. 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. 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. 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).

Please note it's simplified code.

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
    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!

这篇关于传递PHP页面之间的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆