在两个PHP页面之间传递变量,而不使用表单或页面URL [英] Pass variables between two PHP pages without using a form or the URL of page

查看:73
本文介绍了在两个PHP页面之间传递变量,而不使用表单或页面URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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";

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

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

是否有一种方法(也许通过$ _POST?)来发送变量?如果有人想知道为什么我希望它们不可见,我只是不想在上传文件时使用& message =问题"之类的大URL地址,这不是有效的.zip文件,而且我也不想有很多时间来更改页面的重定向,以避免出现此问题.

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.

推荐答案

会话将是您的理想选择.请看 PHP手册中的两个示例:

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

page1.php的代码

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>';
?>

page2.php的代码

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>';
?>

要清除内容- SID是PHP的预定义常量,其中包含会话名称及其ID.示例SID值:

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

PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1

这篇关于在两个PHP页面之间传递变量,而不使用表单或页面URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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