dongweng9474 2013-05-12 22:17
浏览 16
已采纳

如果已登录,则重定向到某个页面

I have a main page that has a log in link on it that takes the user to a login page. However, if a user is already logged in I want to take the user to another page, for example a info page.

I have this function:

function logged_in_redirect() {
if (logged_in() === true) {
    header('Location: client.php');
    exit();
}
}

I'm wondering where to put this? I've tried pretty much everything. If i put this on the login page, it does not redirect me when i am logged on. I've tried adding it as an onclick function for the link on the home page but it didn't work.

This is my logged_in() function:

function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}

Any suggestions?

Edit:

I have currently fixed the problem by making the button on the home page link to a test.php file which has this code:

<?php
include 'core/init.php';

if (isset($_SESSION["user_id"])) {
    header('Location: client.php');
}
else {
    header('Location: info.php');
}

?>

Is there any way around this?

  • 写回答

1条回答 默认 最新

  • dongyiluan1718 2013-05-12 22:38
    关注

    If your session is set and the user is properly authenticated this will work.

    You don't need extra function to check whether login is set unless you have a common file which is handling authentication related stuff and all the other files calling its function to check if the user is logged in..

    Login Page:

    <?php 
    
    //check if the user is already loggedin
    if(isset($_SESSION["user_id"]){
      //assuming client.php is in the same directoy
      header("Location: client.php"); //you don't need exit since it will be redirected
    }
    
    //your login stuff. if your user_id was not set this part will be executed
    
    ?>
    

    Also don't forget to destroy session once you log out..

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

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据