dongzhuzhou4504 2017-10-15 10:49
浏览 351
已采纳

如果未登录,则将用户重定向到另一个页面?

This may seem pretty confusing at first but I have a log in system on my website. I also have a forum on my website. What I want to do is make it so if people click on the forum button and they aren't logged in, it takes them to the login page, but if they are, it will take them to the forums. I have that in place but I am trying to do one more thing. I also want to make it so if people go in the url and type www.example.com/forums.php, it will check if they are logged in and if they aren't, take them back to the login page and if they are, proceed to take them there. I tried with this but it only works for the first part like I stated, not the rest.

<!-- Main Content -->
        <p class="japanese">プレーンズ</p>
        <p class="dev" contenteditable>currently under development</p>
        <p class="clock"></p>
        <p class="login"><a href="login.php">login</a></p>
        <p class="register"><a href="register.php">register</a></p>
        <?php
            if (isset($_SESSION['u_username'])) {
                echo '<p class="forums"><a href="forums.php">forums</a></p>';
            } else {
                echo '<p class="forums"><a href="login.php">forums</a></p>';
            }
        ?>
  • 写回答

1条回答 默认 最新

  • doumi1944 2017-10-15 11:01
    关注

    On top of your forums.php, check if the session is active, and if not, issue an HTTP redirect.

    <?php
    if (!isset($_SESSION['u_username'])) {
        header('Location: login.php');
        exit();
    }
    

    The exit is important to avoid running any more code on the page if the user is being redirected.

    And the redirect should be placed before any output takes place, which means that it should go at the top of the file being executed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么