猿问

Codeigniter 中的登录会话问题

我创建具有多级别功能的登录表单,多级别的代码正在工作,但是当我以管理员身份登录我的帐户并直接进入管理仪表板时,我在会话上遇到问题,但我仍然可以使用直接登录页面的后页再说一遍,我错过了什么?

我的登录控制器:

<?php

defined('BASEPATH') or exit('No direct script access allowed');


class Login extends CI_Controller

{

    function __construct()

    {

        parent::__construct();

        $this->load->model('login_model');

        $this->load->library('session');

        $this->load->helper('url');

        // $this->load->library('form_validation');

    }


    function index()

    {


            $datatitle['title'] = 'Login LPPM UTY';

            $this->load->view('auth/tamplate/auth_header', $datatitle);

            $this->load->view('auth/login_v');

            $this->load->view('auth/tamplate/auth_footer');


    }


    function auth()

    {

        $username = htmlspecialchars($this->input->post('username', TRUE), ENT_QUOTES);

        $password = htmlspecialchars($this->input->post('password', TRUE), ENT_QUOTES);

        $cek_admin = $this->login_model->auth_admin($username,$password);


        if ($cek_admin->num_rows() > 0) { //jika login sebagai admin

            $data = $cek_admin->row_array();

            $this->session->set_userdata('masuk', TRUE);

            if ($data['level'] == '1') 

            { //Akses admin


                $this->session->set_userdata('akses', '1');

                $this->session->set_userdata('user', $data['username']);

                $this->session->set_userdata('name', $data['name']);

                redirect('admin');

            } else { // jika username dan password tidak ditemukan atau salah

                $url = base_url();

                echo $this->session->set_flashdata('notif', '<div class="alert 

                    alert-danger" role="alert">Username Atau Password Salah

                    </div>');

                redirect($url);

            }



青春有我
浏览 99回答 2
2回答

函数式编程

检查构造中的会话function __construct ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; parent ::__construct ();&nbsp; &nbsp; &nbsp; &nbsp; if ($this -> session -> userdata ( "akses" ) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect ( "admin/dashboard" , "refresh" );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ($this -> session -> userdata ( "akses" ) == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect ( "user/dashboard" , "refresh" );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

SMILET

生成一个会话以了解管理员是否登录--$this->session->set_userdata('is_admin_login', 1)检查会话是否在您的登录网址(控制器)上生成--function index(){&nbsp; &nbsp; if ($this->session->userdata('is_admin_login')) {&nbsp; &nbsp; &nbsp; &nbsp; redirect('admin/home');&nbsp; // redirect to desired location&nbsp; &nbsp; }&nbsp; &nbsp; $datatitle['title'] = 'Login LPPM UTY';&nbsp; &nbsp; $this->load->view('auth/tamplate/auth_header', $datatitle);&nbsp; &nbsp; $this->load->view('auth/login_v');&nbsp; &nbsp; $this->load->view('auth/tamplate/auth_footer');}
随时随地看视频慕课网APP

相关分类

Html5
我要回答