使用 header() 将数据从一个 PHP 页面传递到另一页面

发布于 2024-12-17 13:45:10 字数 543 浏览 0 评论 0原文

我想将数据从一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-12-24 13:45:10

以下代码有效:

file1.php:

<?php

header( 'Location: inner/file2.php?x=1&y=2&z=3' );

?>

inner/file2.php:

<?php

print '<pre>';

var_dump( $_GET );

print '</pre>';

?>

访问 http://localhost/testing/file1.php 的结果> 是到 http://localhost/testing/inner/file2.php?x=1&y=2&z=3 的重定向,其中显示:

array(3) {
  ["x"]=>
  string(1) "1"
  ["y"]=>
  string(1) "2"
  ["z"]=>
  string(1) "3"
}

我建议复制这些测试文件并证明对自己来说,重定向的基本概念是通过价值观正在发挥作用。然后,围绕已知良好的内核构建其余代码。祝你好运!

The following code works:

file1.php:

<?php

header( 'Location: inner/file2.php?x=1&y=2&z=3' );

?>

inner/file2.php:

<?php

print '<pre>';

var_dump( $_GET );

print '</pre>';

?>

The result of visiting http://localhost/testing/file1.php is a redirect to http://localhost/testing/inner/file2.php?x=1&y=2&z=3 which displays:

array(3) {
  ["x"]=>
  string(1) "1"
  ["y"]=>
  string(1) "2"
  ["z"]=>
  string(1) "3"
}

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!

等数载,海棠开 2024-12-24 13:45:10

如果没有要发送的变量的示例,则很难判断问题可能是什么,但可能的问题可能是变量中的字符。

为了确保没有无效字符,您可以使用 urlencode(),也许与 htmlentities() 结合使用,请参阅 手册

header("location:http://$host$uri/$extra?sms=".urlencode($msg)."&num=".urlencode($msg_num));

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 with htmlentities(), see the manual:

header("location:http://$host$uri/$extra?sms=".urlencode($msg)."&num=".urlencode($msg_num));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文