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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM