Php 使用header:location时变量无效
我有一个php部分,如果在下面的当前状态下为true,用户将被发送回mail.php,Php 使用header:location时变量无效,php,html,css,Php,Html,Css,我有一个php部分,如果在下面的当前状态下为true,用户将被发送回mail.php,$mailErrorMsg和$mailErrorDisplay都能正常工作 php原版 if ($sql_recipient_num == 0){ $mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />'; $mailErrorDisplay = ''; }
$mailErrorMsg
和$mailErrorDisplay
都能正常工作
php原版
if ($sql_recipient_num == 0){
$mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
$mailErrorDisplay = '';
}
您认为header()命令的作用类似于require_once()其中新脚本被“注入”到当前脚本中它实际上是在向浏览器发送一个http头,上面写着“Location:mail.php?tid=3”。然后,浏览器会重定向到mail.php页面,就像单击链接一样 您在其下的任何内容仍将在后台运行,但persons浏览器现在已关闭到新页面如果要传递$mailErrorMsg和/或$mailErrorDisplay,则需要将它们存储在会话变量或cookie中,并将这些声明放在头重定向上方,如下所示:
if ($sql_recipient_num == 0){
$mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
$mailErrorDisplay = '';
header('Location: mail.php?tid=3');
}
if($sql\u recipient\u num==0){
$mailErrorMsg='错误:
收件人不存在。
';
$mailErrorDisplay='';
标题('Location:mail.php?tid=3');
}
您认为header()命令的作用类似于require\u once(),其中新脚本被“注入”到当前脚本中它实际上是在向浏览器发送一个http头,上面写着“Location:mail.php?tid=3”。然后,浏览器会重定向到mail.php页面,就像单击链接一样
您在其下的任何内容仍将在后台运行,但persons浏览器现在已关闭到新页面如果要传递$mailErrorMsg和/或$mailErrorDisplay,则需要将它们存储在会话变量或cookie中,并将这些声明放在头重定向上方,如下所示:
if ($sql_recipient_num == 0){
$mailErrorMsg = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
$mailErrorDisplay = '';
header('Location: mail.php?tid=3');
}
if($sql\u recipient\u num==0){
$mailErrorMsg='错误:
收件人不存在。
';
$mailErrorDisplay='';
标题('Location:mail.php?tid=3');
}
使用标头不会传递任何这些变量。你应该做的是使用会话
session_start(); // put this on the top of each page you want to use
if($sql_recipient_num == 0){
$_SESSION['mailErrorMsg'] = "your message";
$_SESSION['mailErrorDisplay'] = "whatever";
// header
}
然后在要打印这些错误消息的页面上
session_start();
print $_SESSION['mailErrorMsg'];
// then you want to get rid of the message
unset($_SESSION['mailErrorMsg']; // or use session_destroy();
使用标头不会传递这些变量中的任何一个。你应该做的是使用会话
session_start(); // put this on the top of each page you want to use
if($sql_recipient_num == 0){
$_SESSION['mailErrorMsg'] = "your message";
$_SESSION['mailErrorDisplay'] = "whatever";
// header
}
然后在要打印这些错误消息的页面上
session_start();
print $_SESSION['mailErrorMsg'];
// then you want to get rid of the message
unset($_SESSION['mailErrorMsg']; // or use session_destroy();
将浏览器重定向到该页面。所有变量都是空的。我将使用会话变量来存储信息
session_start(); //must be before any output
if ($sql_recipient_num == 0){
header('Location: mail.php?tid=3');
$_SESSION['mailErrorMsg'] = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
$_SESSION['mailErrorDisplay'] = '';
}
这应该能满足你的需要
将浏览器重定向到该页面。所有变量都是空的。我将使用会话变量来存储信息
session_start(); //must be before any output
if ($sql_recipient_num == 0){
header('Location: mail.php?tid=3');
$_SESSION['mailErrorMsg'] = '<u>ERROR:</u><br />The Recipient does not exist.<br />';
$_SESSION['mailErrorDisplay'] = '';
}
这应该可以满足您的需要。好的,我现在就试试,尽管我有一个问题:
$mailErrorDisplay='display:none;'
在php的最顶端设置默认值,因此直到出现错误时才会显示错误,如何使用会话变量实现这一点?因为在剩下的时刻,错误只是不断地显示,而在它没有显示之前,你可以说:$mailErrorDisplay=(isset($\u SESSION['mailErrorDisplay'])?“显示:可见”:“显示:无”代码>嗨,刚刚试过,它仍然显示错误,我在顶部添加了你上面的注释代码,就像你说的那样。这是我在phpif($sql\u recipient\u num==0){$\u SESSION['mailErrorMsg']=“ERROR:
收件人不存在。
“;$\u SESSION['mailErrorDisplay']=“header('Location:mail.php?tid=3');
这是我在css中的内容。{mail\u errors{height:30px;width:767px;}
好的,我现在就来试试,尽管我有一个问题,$mailErrorDisplay='display:none;'
在php的最顶端设置默认值,因此直到出现错误时才会显示错误,如何使用会话变量实现这一点?因为在剩下的时刻,错误只是不断地显示,而在它没有显示之前,你可以说:$mailErrorDisplay=(isset($\u SESSION['mailErrorDisplay'])?“显示:可见”:“显示:无”代码>嗨,刚刚试过,它仍然显示错误,我在顶部添加了你上面的注释代码,就像你说的那样。这是我在phpif($sql\u recipient\u num==0){$\u SESSION['mailErrorMsg']=“ERROR:
收件人不存在。
“;$\u SESSION['mailErrorDisplay']=“header('Location:mail.php?tid=3');
这是我在css中的内容。{mail\u errors{height:30px;width:767px;}