简体   繁体   English

在PHP中将值从页面传递到另一个

[英]pass value from page to another in PHP

I am sending login status = fail, back to my login page.Here is my code- 我正在发送登录状态=失败,返回我的登录页面。这是我的代码-

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

but that is sending through URL like- 但这是通过URL发送,例如-

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

is there any way to pass value without showing in URL? 有什么方法可以传递值而不显示在URL中? And how to get this value on the second page? 以及如何在第二页上获得此值?

You are passing that value via a GET request, which is why it appears in the URL. 您正在通过GET请求传递该值,这就是它出现在URL中的原因。 In order to pass a value without showing it in the URL, you want to pass it via a POST request. 为了传递一个值而不在URL中显示它,您想通过POST请求传递它。

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形式正在处理用户单击“登录”按钮后的登录过程,都将决定向用户显示什么。

In PHP post variables can be accessed by the global $_POST object - 在PHP中,可以通过全局$ _POST对象访问post变量-

$_POST['username'];

Would get the value with the name "username" that you passed via POST: 将获得通过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>

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" 为了动态保存错误并向用户显示错误,您可以将它们存储在会话中,例如,有一个名为“ errors.php”的文件

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

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

And in your php that checks the login, do: 并在检查登录的PHP中执行以下操作:

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. 如果您没有任何错误,它将不会显示任何内容,并且登录页面将看起来很正常。

Note : In any php form that you use a session_start(), it HAS TO BE THE FIRST THING in the form. 注意 :在任何使用session_start()的php表单中,它必须是表单中的第一件事。

Other ways are to use session or hidden fields but you what you are doing is fine for the purpose. 其他方法是使用会话隐藏字段,但是您正在执行的操作可以达到目的。 You can later retrieve the value like this: 您以后可以像这样检索值:

if ($_GET['login'] === 'fail')
{
  // failed.......
}

there are several ways to accomplish your task 有几种方法可以完成您的任务

  • Modern AJAX way. 现代AJAX方式。 Form being sent using AJAX. 使用AJAX发送表格。 No page reload until password is correct. 密码正确之前,不会重新加载页面。 Errors shown in place. 显示的错误。 Requres javascript. Requres javascript。
  • Post/Redirect/Get pattern. 发布/重定向/获取模式。 Form being sent using regular POST. 使用常规POST发送表格。 No redirect on errors, shown in place. 错误不会重定向,显示在适当位置。
  • sessions, when we store an error in the session 会话中,当我们在会话中存储错误时

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

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