在PHP中将值从页面传递到另一个 [英] pass value from page to another in PHP

查看:71
本文介绍了在PHP中将值从页面传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送登录状态=失败,返回到我的登录页面.这是我的代码-

I am sending login status = fail, back to my login page.Here is my code-

header("location:index.php?login=fail");

但这是通过类似-

http://localhost/303/index.php?login=fail

有没有办法在不显示URL的情况下传递值?以及如何在第二页上获得此值?

is there any way to pass value without showing in URL? And how to get this value on the second page?

推荐答案

您正在通过GET请求传递该值,这就是它出现在URL中的原因.为了传递一个值而不在URL中显示它,您想通过POST请求传递它.

You are passing that value via a GET request, which is why it appears in the URL. In order to pass a value without showing it in the URL, you want to pass it via a POST request.

为此,您不需要将值返回"到您的登录页面.取而代之的是,无论任何php形式正在处理的用户单击登录"按钮后的登录过程,都将决定向用户显示什么.

In order to do this you aren't going to want to "return" the value to your login page. Instead, whatever php form is handling the process of logging in the user after they click the "login" button, will decide what to show the user.

在PHP中,发布变量可以由全局$ _POST对象访问-

In PHP post variables can be accessed by the global $_POST object -

$_POST['username'];

将获得您通过POST传递的名称为用户名"的值:

Would get the value with the name "username" that you passed via POST:

<form method="post" action="checkLogin.php">
Username:
<input type="text" name="username" maxlength="25" />
Password: 
</td><td><input type="password" name="password" />
<input type="submit" name="submit" value="Login">
</form>

为了动态保存错误并向用户显示错误,您可以将它们存储在会话中,例如,有一个名为"errors.php"的文件

In order to dynamically save and show errors to the user, you can store them in the session, for example have a file called "errors.php"

<?php
if (isset($_SESSION['errors']))
{
    echo $_SESSION['errors'];
}

unset($_SESSION['errors'])
?>

然后在检查登录的php中,执行以下操作:

And in your php that checks the login, do:

session_start();
$_SESSION['errors'] = "Invalid username or password.";

然后重定向到您的登录页面(不传递任何变量),并且在表单上始终具有以下字段:

Then redirect to your login page (don't pass any variables) and on your form always have this field:

<?php include("errors.php"); ?>

如果没有任何错误,它将不会显示任何内容,并且登录页面将看起来正常.

If you didn't have any errors, it won't show anything and the login page will look normal.

注意:在任何使用session_start()的php表单中,它必须是该表单中的第一件事.

Note: In any php form that you use a session_start(), it HAS TO BE THE FIRST THING in the form.

这篇关于在PHP中将值从页面传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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