OP的代码中有两个错误,遗憾的是官方接受的答案也反映出来 . 需要将分号附加到使用sleep()的语句中,并且在测试$ _GET ['template']的语句中需要一个额外的括号 .
实际上,不需要使用sleep()提供的信号处理使代码复杂化,以便延迟提交POSTed数据以确定$ _GET ['template']的值 . 可以省略sleep()并稍微改变代码,如下所示:
if( isset($_POST['submit']) )
{
$mess = htmlentities($_POST['mess']);
$subject = htmlentities($_POST['subject']);
header("Location: http://localhost/exp/create_newsletter.php?template=new");
exit;
}
else
if( isset( $_GET['template']))
{
echo "hello robert now you are working on the template";
exit;
}
另外,使用$ _SERVER ['QUERY_STRING'],而不是使用$ _GET,如下所示:
$qs = parse_url($_SERVER['PHP_SELF'], PHP_URL_QUERY);
if( $qs == 'template=new' ){
$template = split("=",$qs)[1];
echo "hello robert now you are working on the template";
exit;
}
else
if(isset($_POST['submit']))
{
sleep(2);
$mess = htmlentities($_POST['mess']);
$subject = htmlentities($_POST['subject']);
header("Location: http://localhost/exp/create_newsletter.php?template=new");
exit;
}
?>
testparse_url()的组件参数使此函数能够返回查询字符串 . 也可以选择使用parse_str(),如下所示:
$queries = "";
parse_str($_SERVER['QUERY_STRING'], $queries);
if( isset($queries['template']) && ($queries['template'] == 'new'))
{
$template = $queries;
echo "hello robert now you are working on the template";
exit;
}
else
if(isset($_POST['submit']))
{
sleep(2);
$mess = htmlentities($_POST['mess']);
$subject = htmlentities($_POST['subject']);
header("Location: http://localhost/exp/create_newsletter.php?template=new");
exit;
}
?>
注意:始终将POST或GET中的数据视为污染非常重要,而不是直接将数据分配给变量并使用该变量 . 使用htmlentities()是尝试防止可能的安全问题的一种方法 .