【问题标题】:PHP continue statement?PHP continue 语句?
【发布时间】:2011-10-09 18:56:28
【问题描述】:

如果用户未登录并且我遇到麻烦,我会尝试创建重定向

这是我使用的方法

<?php
if ( is_user_logged_in() ) {
?>

/*My Content*/

<?php
    } else {
        echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.*****.com/non-member.php">';
    }
?>

这工作正常,但它被延迟并且我能够在重定向之前看到页面几秒钟。

我想做的是这样的。

<?php
if ( is_user_logged_in() ) {
     /*IGNORE NEXT STATEMENT*/
} else {
        echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.****.com/non-member.php">';
    }
?>

我不确定这是否可行,但我会假设那里有某种方法。

【问题讨论】:

    标签: php redirect


    【解决方案1】:

    你为什么不试试这个

    <?php 
    if (!is_user_logged_in() ) {
        echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.****.com/non-member.php">';
    } else {
        Do your thing
    }
    ?>
    

    放在条件开头的"!"会检查条件是否未被验证​​。

    【讨论】:

      【解决方案2】:

      试试这个:

      <?php
      if (!is_user_logged_in() ) {
            die(header("Location: http://www.****.com/non-member.php"));
      }
      ?>
      

      【讨论】:

        【解决方案3】:

        如果 is_user_logged_in() 为假,那么 if 语句中的代码实际上并没有运行并且几乎被忽略(它仍然被解析为错误)。您的延迟可能来自您的重定向。尝试使用 header() 重定向。例如:

        header('位置:http://www.google.com');

        【讨论】:

        • 延迟来自使用 META-redirect,它仅在 DOM 完全加载后“执行”,这就是为什么这里大多数人建议使用 HTTP-header-redirects。
        【解决方案4】:

        试试

        if (!is_user_logged_in()) {
            header("Location: http://www.*****.com/non-member.php");
        }
        

        相反。 ! 是一个布尔值 'not',它与 if() 测试的结果相反,并且 header() 调用是一种比发出嵌入在 HTML 中的元标头更简洁/更可靠的方法。

        【讨论】:

          【解决方案5】:

          使用Location 标头进行重定向:

          <?php
          if ( is_user_logged_in() ) {
               /* snip */
          } else {
              header("Location: http://www.****.com/non-member.php");
          }
          ?>
          

          使用此方法将在收到 HTTP 标头后立即触发重定向,并且 HTML 页面本身将被完全忽略。

          【讨论】:

            【解决方案6】:
            if (!is_user_logged_in())
            {
                    header("Location: http://www.****.com/non-member.php");
                    exit;
            }
            //rest of content.
            

            【讨论】:

              【解决方案7】:

              对于这种类型的行为,你最好使用标题:

              <?php
              // place at the top, before anything (including whitespace).
              if( !is_user_logged_in() ) {
                  header("Location: http://www.****.com/non-member.php");
                  die(); // you don't want the rest of the page showing.
              }
              ?>
              

              这将完成您正在尝试的操作,而不会让未登录的人先看到该页面。

              【讨论】:

              • 没有尝试。在这种情况下 die() 或 exit 比将文件的其余部分包装在 else 语句中要明显得多
              • @yi_H - 在这里使用die 有什么问题? (或者exit,我更喜欢die,没有特别的原因)。
              • @yi_H 也许你应该阅读这个问题:stackoverflow.com/questions/1795025/…
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-10-04
              • 2012-09-10
              • 2010-10-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多