【问题标题】:PHP: Writing an if statement for different custom headsPHP:为不同的自定义头编写 if 语句
【发布时间】:2017-11-29 15:53:58
【问题描述】:

我正在创建一个客户数据库。我在搜索功能中创建错误检查,以便在用户不存在时将用户重定向回搜索页面。我创建一个这样的标题:

header("Location: search.php?search=not-found");

我想在search.php页面上写一个if statement,这样如果URL末尾有"?search=not-found",那么它会显示一个错误,用户可以通过搜索框看到。比如(我知道这是不正确的):

if(url contains ['search.php?search=not-found']){
    echo 'That user doesn't exist';
} 
else {
    echo 'Please search for a user';
}

我该如何写这个 if 语句?

【问题讨论】:

标签: php sql database if-statement header


【解决方案1】:

你可以使用$_SERVER['QUERY_STRING']:

if($_SERVER['QUERY_STRING'] == 'search=not-found'){...}

【讨论】:

    【解决方案2】:

    使用$_SERVER['REQUEST_URI']获取URL,然后使用preg_matchstrpos查看是否包含所述字符串,即未找到

    但请注意,该变量并不完全安全。因此,请务必进行一些安全检查。

    if((strpos($_SERVER['REQUEST_URI'],"?search=not-found")) === false){
       // Do this
    }else{
       // Do this 
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多