使用 header() 将数据从一个 PHP 页面传递到另一页面
我想将数据从一个 PHP 文件发送到第一个 PHP 文件所在子文件夹中的另一个 PHP 文件。我有一个名为 folder1
的文件夹,其中包含一个名为 file1.php
的 PHP 文件,我想在folder1
的子文件夹,名为 folder2
。我在 file1.php
中使用这样的 header()
函数:
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'folder1/folder2/file2.php';
header("location:http://$host$uri/$extra?sms=".$msg."&num=".$msg_num);
数据未传递。有没有使用header()
的解决方案?由于某些限制,我无法使用 cURL
。
I want to send data from one PHP file to another PHP file in a subfolder where the first PHP file is present. I have a folder named folder1
which has contains a PHP file named file1.php
and I want to call another file named file2.php
in a subfolder of folder1
named folder2
. I am using the header()
function like this in file1.php
:
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'folder1/folder2/file2.php';
header("location:http://$host$uri/$extra?sms=".$msg."&num=".$msg_num);
Data is not passing. Is there any solution using header()
? I can't use cURL
because of some restrictions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下代码有效:
file1.php
:inner/file2.php
:访问
http://localhost/testing/file1.php
的结果> 是到http://localhost/testing/inner/file2.php?x=1&y=2&z=3
的重定向,其中显示:我建议复制这些测试文件并证明对自己来说,重定向的基本概念是通过价值观正在发挥作用。然后,围绕已知良好的内核构建其余代码。祝你好运!
The following code works:
file1.php
:inner/file2.php
:The result of visiting
http://localhost/testing/file1.php
is a redirect tohttp://localhost/testing/inner/file2.php?x=1&y=2&z=3
which displays:I would suggest copying over these test files and proving to yourself that the basic concept of redirecting with passed values is working. Then, build up the rest of your code around a known-good kernel. Good luck!
如果没有要发送的变量的示例,则很难判断问题可能是什么,但可能的问题可能是变量中的字符。
为了确保没有无效字符,您可以使用
urlencode()
,也许与htmlentities()
结合使用,请参阅 手册:Without an example of the variables you want to send, it's kind of hard to tell what the problem might be, but a possible problem could be the characters in the variables.
To make sure there are no invalid characters, you can use
urlencode()
, perhaps in combination withhtmlentities()
, see the manual: