php问号传值,PHP无法通过干净网址上的问号传递

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;

}

?>

test

parse_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()是尝试防止可能的安全问题的一种方法 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值