Pass variables between two PHP pages without using a form or the URL of page(在不使用表单或页面 URL 的情况下在两个 PHP 页面之间传递变量)
问题描述
我想将几个变量从一个 PHP 页面传递到另一个页面.我没有使用表格.变量是目标页面出现问题时将显示的一些消息.如何将这些变量传递给其他 PHP 页面,同时保持它们不可见?
例如假设我有这两个变量:
//原始页面$message1 = "一条消息";$message2 = "另一条消息";
我想将它们从 page1.php 传递到 page2.php.我不想通过 URL 传递它们.
//我不要'page2.php?message='.$message1.'&message2='.$message2
有没有办法(也许通过 $_POST?)发送变量?如果有人想知道为什么我希望它们不可见,我只是不想要一个带有&message=Problem while uploading your file. This is not a valid .zip file"这样的参数的大 URL 地址,我不想要有很多时间来改变我的页面的重定向以避免这个问题.
Sessions 将是您不错的选择.从PHP手册中查看这两个示例:
<块引用>page1.php 代码
第2页';?>
<块引用>
page2.php代码
清理事情 - SID 是 PHP 的预定义常量,其中包含会话名称及其 ID.SID 值示例:
PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1
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?
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.
//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.
Sessions would be good choice for you. Take a look at these two examples from PHP Manual:
Code of 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
<?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 constant which contains session name and its id. Example SID value:
PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1
这篇关于在不使用表单或页面 URL 的情况下在两个 PHP 页面之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!