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

时间:2022-06-06 10:16:53

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:

我想将数据从一个PHP文件发送到存在第一个PHP文件的子文件夹中的另一个PHP文件。我有一个名为folder1的文件夹,其中包含一个名为file1.php的PHP文件,我想在名为folder2的folder1的子文件夹中调用另一个名为file2.php的文件。我在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);

Data is not passing. Is there any solution using header()? I can't use cURL because of some restrictions.

数据没有通过。有没有使用header()的解决方案?由于一些限制,我无法使用cURL。

2 个解决方案

#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:

访问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"
}

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!

我建议复制这些测试文件,并向自己证明使用传递值重定向的基本概念是有效的。然后,围绕已知良好的内核构建其余代码。祝你好运!

#2


1  

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:

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

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

#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:

访问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"
}

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!

我建议复制这些测试文件,并向自己证明使用传递值重定向的基本概念是有效的。然后,围绕已知良好的内核构建其余代码。祝你好运!

#2


1  

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:

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

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