【问题标题】:PHP - Allow Access to Page Only If Variable Exists in UrlPHP - 仅当 URL 中存在变量时才允许访问页面
【发布时间】:2020-04-03 05:34:00
【问题描述】:

很抱歉,我想不出一个更合适的标题,但这是希望通过 PHP 实现的目标:

  • 我有一个网址为 www.foo[.]com/mypage

  • 的页面
  • 我只希望带有?user=$email 的页面或网址可以访问,因此如果有人尝试在没有?user=$email 的情况下访问该网址,他们会被重定向到其他地方。

  • 如何定义该条件?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我尝试过使用以下方法 - if (!$_SERVER['REQUEST_METHOD'] === 'GET') 和 $user = $_GET['user']; if ($user == '') // 如果 $user 作为字符串为空

标签: php html if-statement conditional-statements


【解决方案1】:

您好@Edwin,这是您问题的解决方案。

<?php
    if(!isset($_GET['user']) && $_GET['user']!='' && $_GET['user']!=null) {
        header("Location: http://www.foo[.]com");
    } else {
        header("Location: http://www.foo[.]com/somepage");
    }
?>

【讨论】:

    【解决方案2】:

    在您的 mypage 页面中 TOP(Very First line) 只需编写以下代码

    <?php
       if(!isset($_GET['user']) || (isset($_GET['user']) && $_GET['user'] == "") ){
           header("Location: http://www.foo[.]com");
       }
    ?>
    

    在这里,我不仅检查 $_GET['user'] 是否可用,还检查是否必须传递一些值。在这里您也无需编写 else{} 即可继续。

    【讨论】:

      【解决方案3】:

      试试这个脚本!!!

      if(!isset($_GET['user'])){
       header("location:./");
      }
      elseif(isset($_GET['user']) && $_GET['user']==''){
       header("location:./");
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-14
        • 1970-01-01
        • 2018-12-19
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多