如何将信息传递给使用 header() 调用的 php 脚本?
How can I pass information to a php script called with header()?
我在 运行 一个 php 脚本时遇到问题,使用 header().
调用第二个脚本
主脚本 (a.php) 调用,而 运行,第二个脚本 (b.php)。
当使用此表单时,一切都按预期进行(执行了 ActionA 和 ActionB):
https://example.com/a.php
使用其他形式时,预期的行为(ActionAX 和 ActionBX)没有发生。而是执行 ActionAX 和 ActionB:
https://example.com/a.php?V=X
也就是说b.php没有得到V=X位的信息。
文件 a.php 如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META HTTP-EQUIV="content-type" CONTENT="text/html; CHARSET=UTF-8">
<TITLE>MyWebApp</TITLE>
<STYLE>
INPUT[type='Submit'] {font-size: 19px;}
</STYLE>
</HEAD>
<BODY bgcolor="#A1E3D2">
<?php
session_start();
.....
function getVarStr()
{/* Beginning of getVarStr */
if ($_GET['V'] == 'X') performActionAX();
else performActionA();
}/* End of getVarStr */
.....
getVarStr();
.....
if ($_GET['V'] == 'X') header("Location: ./b.php?V=X");
header("Location: ./b.php");
.....
?>
</BODY>
</HTML>
文件 b.php 如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Extra Page</title>
<STYLE>
INPUT[type='Submit'] {font-size: 19px;}
</STYLE>
</head>
<body>
<?php
session_start();
.....
function getVarStr()
{/* Beginning of getVarStr */
if ($_GET['V'] == 'X') performActionBX();
else performActionB();
}/* End of getVarStr */
.....
getVarStr();
.....
?>
</BODY>
</HTML>
谁能指出我犯的错误?
我也尝试过使用会话变量来处理这个问题,但我遇到了完全相同的问题。
在这种情况下,我将 b.php 中的 getVarStr() 更改为:
function getVarStr()
{/* Beginning of getVarStr */
if ($_SESSION['V'] == 'X') performActionBX();
else performActionB();
}/* End of getVarStr */
如果 "php 脚本使用 header()" 调用,您指的是代码中的 header("Location....
行,那么你对这里真正发生的事情的了解是错误的。
location header 只是添加到 http 响应中的字符串; header()
或多或少与 echo
相同,除了它打算更早发生,在任何 http 回复的 body 被写入之前发生(稍后会详细介绍)。
在功能上,Location header really 的作用,只是让用户的网络浏览器知道,你会喜欢他们访问另一个 page/url ,它(在大多数情况下)会遵守。这与发送 a
没有什么不同
<script> window.location = 'https://....';</script>
在您的 html 回复中;除了 http header 不依赖于 java-script 被启用。
现在,实际问题是您构建代码的方式。在尝试向 http 回复添加 header 之前,您不能编写 any 输出。这样做可以防止 PHP 成为 allowed/able 添加 header.
你的 header()
和 session_start()
(间接也使用 header()
放置 cookie)都位于太远的地方您的代码才能正常运行。
事实上,在您的 a.php
和 b.php
文件中,第一行 (<!DOCTYPE...
) 已经产生输出,这是一个很大的 no-no.
总是将 php 源放在 html 上面,特别是如果他们修改 cookie 或 session 以防止遇到“警告:无法修改 Header 信息 – Header已发送”问题,你也可以把它放在任何地方,这样你就知道你应用的数据实际上正在被应用
echo "SESSION NAME: " . session_name();
echo "<br>";
echo "SESSION ID: " . session_id();
echo "<br>";
echo "TIME: " . time();
我在 运行 一个 php 脚本时遇到问题,使用 header().
调用第二个脚本主脚本 (a.php) 调用,而 运行,第二个脚本 (b.php)。
当使用此表单时,一切都按预期进行(执行了 ActionA 和 ActionB):
https://example.com/a.php
使用其他形式时,预期的行为(ActionAX 和 ActionBX)没有发生。而是执行 ActionAX 和 ActionB:
https://example.com/a.php?V=X
也就是说b.php没有得到V=X位的信息。
文件 a.php 如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META HTTP-EQUIV="content-type" CONTENT="text/html; CHARSET=UTF-8">
<TITLE>MyWebApp</TITLE>
<STYLE>
INPUT[type='Submit'] {font-size: 19px;}
</STYLE>
</HEAD>
<BODY bgcolor="#A1E3D2">
<?php
session_start();
.....
function getVarStr()
{/* Beginning of getVarStr */
if ($_GET['V'] == 'X') performActionAX();
else performActionA();
}/* End of getVarStr */
.....
getVarStr();
.....
if ($_GET['V'] == 'X') header("Location: ./b.php?V=X");
header("Location: ./b.php");
.....
?>
</BODY>
</HTML>
文件 b.php 如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Extra Page</title>
<STYLE>
INPUT[type='Submit'] {font-size: 19px;}
</STYLE>
</head>
<body>
<?php
session_start();
.....
function getVarStr()
{/* Beginning of getVarStr */
if ($_GET['V'] == 'X') performActionBX();
else performActionB();
}/* End of getVarStr */
.....
getVarStr();
.....
?>
</BODY>
</HTML>
谁能指出我犯的错误?
我也尝试过使用会话变量来处理这个问题,但我遇到了完全相同的问题。
在这种情况下,我将 b.php 中的 getVarStr() 更改为:
function getVarStr()
{/* Beginning of getVarStr */
if ($_SESSION['V'] == 'X') performActionBX();
else performActionB();
}/* End of getVarStr */
如果 "php 脚本使用 header()" 调用,您指的是代码中的 header("Location....
行,那么你对这里真正发生的事情的了解是错误的。
location header 只是添加到 http 响应中的字符串; header()
或多或少与 echo
相同,除了它打算更早发生,在任何 http 回复的 body 被写入之前发生(稍后会详细介绍)。
在功能上,Location header really 的作用,只是让用户的网络浏览器知道,你会喜欢他们访问另一个 page/url ,它(在大多数情况下)会遵守。这与发送 a
没有什么不同 <script> window.location = 'https://....';</script>
在您的 html 回复中;除了 http header 不依赖于 java-script 被启用。
现在,实际问题是您构建代码的方式。在尝试向 http 回复添加 header 之前,您不能编写 any 输出。这样做可以防止 PHP 成为 allowed/able 添加 header.
你的 header()
和 session_start()
(间接也使用 header()
放置 cookie)都位于太远的地方您的代码才能正常运行。
事实上,在您的 a.php
和 b.php
文件中,第一行 (<!DOCTYPE...
) 已经产生输出,这是一个很大的 no-no.
总是将 php 源放在 html 上面,特别是如果他们修改 cookie 或 session 以防止遇到“警告:无法修改 Header 信息 – Header已发送”问题,你也可以把它放在任何地方,这样你就知道你应用的数据实际上正在被应用
echo "SESSION NAME: " . session_name();
echo "<br>";
echo "SESSION ID: " . session_id();
echo "<br>";
echo "TIME: " . time();