在不使用表单或页面 URL 的情况下在两个 PHP 页面之间传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16778425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:41:40  来源:igfitidea点击:

Pass variables between two PHP pages without using a form or the URL of page

phpvariablespost

提问by Lefteris008

I want to pass a couple of variables from one PHP page to another. I am not using a form. The variables are some messages that the target page will display if something goes wrong. How can I pass these variables to the other PHP page while keeping them invisible?

我想将几个变量从一个 PHP 页面传递到另一个页面。我没有使用表格。变量是目标页面出现问题时将显示的一些消息。如何将这些变量传递给其他 PHP 页面,同时保持它们不可见

e.g. let's say that I have these two variables:

例如,假设我有这两个变量:

//Original page
$message1 = "A message";
$message2 = "Another message";

and I want to pass them from page1.php to page2.php. I don't want to pass them through the URL.

我想将它们从 page1.php 传递到 page2.php。我不想通过 URL 传递它们。

//I don't want
'page2.php?message='.$message1.'&message2='.$message2

Is there a way (maybe through $_POST?) to send the variables? If anyone is wondering why I want them to be invisible, I just don't want a big URL address with parameters like "&message=Problem while uploading your file. This is not a valid .zip file" and I don't have much time to change the redirections of my page to avoid this problem.

有没有办法(也许通过 $_POST?)发送变量?如果有人想知道为什么我希望它们不可见,我只是不想要一个带有“&message=Problem while uploading your file. This is not a valid .zip file”这样的参数的大 URL 地址,我没有太多是时候更改我的页面的重定向以避免这个问题了。

回答by Daniel Kmak

Sessions would be good choice for you. Take a look at these two examples from PHP Manual:

Sessions 将是您不错的选择。看看PHP 手册中的这两个示例:

Code of page1.php

page1.php代码

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Code of page2.php

page2.php代码

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>

To clear up things - SID is PHP's predefined constantwhich contains session name and its id. Example SID value:

澄清一下 - SID 是 PHP 的预定义常量,其中包含会话名称及其 ID。SID 值示例:

PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1

回答by Nick

Here are brief list:

以下是简要列表:

  • JQuery with JSON stuff. (http://www.w3schools.com/xml/xml_http.asp)

  • $_SESSION - probably best way

  • Custom cookie - will not*always* work.

  • HTTP headers - some proxy can block it.

  • database such MySQL, Postgres or something else such Redis or Memcached (e.g. similar to home-made session, "locked" by IP address)

  • APC - similar to database, will not*always* work.

  • HTTP_REFERRER

  • URL hash parameter , e.g. http://domain.com/page.php#param- you will need some JavaScript to collect the hash. - gmail heavy use this.

  • 带有 JSON 内容的 JQuery。( http://www.w3schools.com/xml/xml_http.asp)

  • $_SESSION - 可能是最好的方法

  • 自定义 cookie -不会*总是*工作。

  • HTTP 标头 - 某些代理可以阻止它。

  • 数据库,如 MySQL、Postgres 或其他类似 Redis 或 Memcached 的数据库(例如,类似于自制会话,由 IP 地址“锁定”)

  • APC - 类似于数据库,不会*总是*工作。

  • HTTP_REFERRER

  • URL 散列参数,例如http://domain.com/page.php#param- 您需要一些 JavaScript 来收集散列。- gmail 大量使用这个。

回答by user2406160

<?php
session_start();

$message1 = "A message";
$message2 = "Another message";

$_SESSION['firstMessage'] = $message1;
$_SESSION['secondMessage'] = $message2; 
?>

Stores the sessions on page 1 then on page 2 do

将会话存储在第 1 页然后在第 2 页执行

<?php
session_start();

echo $_SESSION['firstMessage'];
echo $_SESSION['secondMessage'];
?>

回答by ramonovski

Have you tried adding both to $_SESSION?

您是否尝试将两者都添加到$_SESSION

Then at the top of your page2.php just add:

然后在 page2.php 的顶部添加:

<?php
session_start();

回答by Putr

Use Sessions.

使用会话

Page1:

第1页:

session_start();
$_SESSION['message'] = "Some message"

Page2:

第2页:

session_start();
var_dump($_SESSION['message']);