简体   繁体   English

在 PHP 页面之间传输变量

[英]Transfer variables between PHP pages

I want to get user input in one page, store that in a php variable and use it in another php page.我想在一个页面中获取用户输入,将其存储在一个 php 变量中并在另一个 php 页面中使用它。 I have tried using 'sessions' but it doesn't seem to be working.我试过使用“会话”,但它似乎不起作用。 Is there another safe alternative?还有其他安全的选择吗? This information is likely to be usernames and passwords.此信息可能是用户名和密码。

Try changing your session code as this is the best way to do this.尝试更改您的会话代码,因为这是执行此操作的最佳方法。

For example:例如:

index.php索引.php

<?php
session_start();

if (isset($_POST['username'], $_POST['password']) {
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    echo '<a href="nextpage.php">Click to continue.</a>';
} else {
    // form
}
?>

nextpage.php下一页.php

<?php
session_start();

if (isset($_SESSION['username'])) {
    echo $_SESSION['username'];
} else {
    header('Location: index.php');
}
?>

However I'd probably store something safer like a userid in a session rather than the user's login credentials.但是,我可能会在会话中存储更安全的内容,例如用户 ID,而不是用户的登录凭据。

I Agree with carson, sessions should work for this.我同意卡森,会议应该为此工作。 Make sure you are calling session_start() before anything else on any page you want to use the session variables.确保在要使用会话变量的任何页面上的任何其他内容之前调用session_start()

Also, I would not store password info directly, rather use some kind of authentication token mechanism.另外,我不会直接存储密码信息,而是使用某种身份验证令牌机制。 IMHO, it is not intrinsically unsafe to store password data in a session, but if there is no need to do so, you should probably try to avoid it.恕我直言,在会话中存储密码数据本质上并不是不安全的,但如果没有必要这样做,你应该尽量避免它。

There are several ways:有几种方式:

  • use sessions (but don't forget to call session_start() on every page you'll use the session data store ( $_SESSION ))使用会话(但不要忘记在您将使用会话数据存储( $_SESSION )的每个页面上调用session_start( ))
  • append your data to the query string of the "next" page ( $_GET )将您的数据附加到“下一个”页面的查询字符串( $_GET
  • post your data to the "next" page ( $_POST )将您的数据发布到“下一个”页面( $_POST

The session-way is the only way on which the data does not "leave" the server as it's stored on the server itself.会话方式是数据不会“离开”服务器的唯一方式,因为它存储在服务器本身上。 For all other ways mentioned above you have to take care of sanitizing and validating the data on the receiving page.对于上述所有其他方式,您必须注意清理和验证接收页面上的数据。

The most simple way would be最简单的方法是

//page1.php
session_start();
$_SESSION['user']='user';
$_SESSION['password']='password';

//page2.php
session_start();
echo $_SESSION['user'] . ' ' . $_SESSION['password'];

You can try using POST and GET methods for transferring user inputs within PHP scripts.您可以尝试使用 POST 和 GET 方法在 PHP 脚本中传输用户输入。

PHP GET PHP 获取

PHP POST PHP POST

I agree too, sessions are the best solution.我也同意,会话是最好的解决方案。 See this chapter from Web Database Applications with PHP & MySQL for some examples.有关一些示例,请参阅使用 PHP 和 MySQL 的 Web 数据库应用程序中的本章。

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

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