douxi8119 2015-02-26 11:01
浏览 32
已采纳

如何根据PHP中的会话数据将用户重定向到不同的目录?

I have a PHP/MySQL based website with folders for member pages and folders for admin pages. I want to direct the users to the different pages according to how they login - as a member or as a admin (from the main user pages) - this is the function I've tried and it doesn't work.

How can I write a function that will work for this?

function connectadmin($level) {
    if ($level === "Administrator"){
        include('admin/home.php');
    }elseif ($level === "Member"){
        include('member/home.php');
    }
}

connectadmin($level);
  • 写回答

1条回答 默认 最新

  • dongzai3139 2015-02-26 11:02
    关注

    Well, you should redirect your users, not include files:

     // At beggining of this file insert this line
     // Start session
     session_start();
    
    function connectadmin($level) {
        if ($level === "Administrator"){
    
            // Set user role
            $_SESSION['role'] = 'Administrator';
    
            // Redirect user
            header('Location: admin/home.php');
            exit();
        }elseif ($level === "Member"){
    
            // Set user role
            $_SESSION['role'] = 'Member';
    
            // Redirect user
            header('Location: member/home.php');
            exit();
        }
    }
    
    // $level should be something you retrieve from your Database for example
    // And perhaps, should be 'Administrator' or 'Member' following your example
    connectadmin($level);
    

    And after redirect user don't forget to validate if the logged in user have access to the redirected page.

    Edit: For example, if you want to validate if user is Administrator and have access to the page admin/home.php, do something like this:

    // You should get from your database, some file or use sessions, 
    // in your function I have used sessions, so lets use them here too
    
    // At beggining of your file use this
    session_start();
    
    // If user is not Administrator
    if($_SESSION['role'] !== 'Administrator'){
    
        // It's not admin, let redirect him to somewhere else or show him a Access not allowed page
        header('Location: accessNotAllowed.php');
        exit();
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现