0

我的问题

?如果为真,我想检查 URL 是否包含一个,do something

我的 PHP

我目前正在使用这个脚本。

$lp_uri = $_SERVER['REQUEST_URI']; // or taken from another source //
    if( strpos($lp_uri, '?') !== false ){
    echo '<script>alert("one question mark");</script>';
}

我的问题

我不知道如何检查是否只?发生一次。你能帮忙吗?非常感谢

原因

我的网站上有一个联系表格,一旦有人提交它,它就会在 url 中添加一个参数,所以它看起来像website.com?msgsent=1. 这意味着消息已发送。但有时如果该email字段不正确,它仍然会?在 url 中添加一个,因此 url 看起来像website.com?.

现在,我slideDown在我的联系表单上使用 jQuery 延迟来吸引客户的注意力,每次页面刷新时,都需要几秒钟的时间slideDown。如果 url 中有一个,我想克服这个?问题,因为如果 url 看起来website.com?意味着,电子邮件地址是错误的。

希望这是有道理的。

4

2 回答 2

4

最初问题的解决方案(以防有人会在谷歌上找到这个问题)是使用substr_count

if(substr_count($text, '?') == 1){
    // do something
}

您的问题的解决方案是检查是否msgsent设置了参数:

if(!isset($_GET['msgsent'])){
    // message not sent - do something
}
于 2012-11-18T22:33:44.433 回答
0

使用偏移可选参数的可能解决方案

<?php
if( strpos($lp_uri, '?') !== false ){ 
   //1 or more question marks
   $pos = strpos($lp_uri, '?');  //Grab the position of it
   if(strpos($lp_uri, '?',$pos) === false) {
      //No more after the first, hence only 1
   }
}
于 2012-11-18T22:33:40.060 回答