php - 如何在 Codeigniter 中创建多个 session

标签 php mysql codeigniter

我有一个问题,我需要插入功能,admin_list_students 设置为只有管理员才能登录和访问这些功能或网页。然后我需要它,以便用户只能访问 user_list_students。目前,当用户登录时,他们还可以访问管理区域。我尝试设置 2 个不同的登录页面,但当用户登录时,他们可以访问管理页面。

student.php Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Student extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('login/student_model');

        }   

    //Shows the dashboard
    public function index()
    {
         if($this->session->userdata('is_logged_in'))
        {

        $this->load->view('header');
        $this->load->view('student');
        $this->load->view('login/footer');
        }else{
            $this->load->view('login/header');
            $this->load->view('login/content'); 
            $this->load->view('login/footer');
        }
    }
    //Insert the Student 
    public function  insert_student()
    {
        $interest=implode(',',$this->input->post('interest'));
        $data=array('name'=>$this->input->post('name'),
            'address'=>$this->input->post('address'),
            'year'=>$this->input->post('year'),
            'gender'=>$this->input->post('gender'),
            'interest'=>$interest,
            'status'=>1);
        //print_r($data);

        $result=$this->student_model->insert_student($data);
        if($result==true)
        {
            $this->session->set_flashdata('msg',"Student Records Added Successfully");
            redirect('student');

        }
        else
        {

            $this->seesion->set_flashdata('msg1',"Student Records Added Failed");
            redirect('student');


        }
    }
    //List of students 
        public function admin_list_students()
    {
         if($this->session->userdata('is_logged_in'))
        {

            $data['student']=$this->student_model->get_student();
            $this->load->view('header');
            $this->load->view('admin_list_of_students',$data);
             $this->load->view('login/footer');
        }
        else{
            $this->load->view('login/header');
            $this->load->view('login/content'); 
             $this->load->view('login/footer');
        }
    }
    //List of students 
        public function user_list_students()
    {
         if($this->session->userdata('is_logged_in'))
        {

            $data['student']=$this->student_model->get_student();
            $this->load->view('header');
            $this->load->view('user_list_of_students',$data);
             $this->load->view('login/footer');
        }
        else{
            $this->load->view('login/header');
            $this->load->view('login/content'); 
             $this->load->view('login/footer');
        }
    }


    public function delete_student()
    {
        $id=$this->input->post('id');
        $data=array('status'=>0);
        $result=$this->student_model->delete_student($id,$data);
        if($result==true)
        {
            $this->session->set_flashdata('msg1',"Deleted Successfully");
            redirect('student/list_students');

        }
        else
        {

            $this->session->set_flashdata('msg1',"Student Records Deletion Failed");
            redirect('student/list_students');


        }

    }
    public function edit_student()
    {
        $id=$this->uri->segment(3);
        $data['student']=$this->student_model->edit_student($id);
        $this->load->view('header',$data);
        $this->load->view('edit_student');
    }
    public function  update_student()
    {
        $id=$this->input->post('id');
        $interest=implode(',',$this->input->post('interest'));
        $data=array('name'=>$this->input->post('name'),
            'address'=>$this->input->post('address'),
            'year'=>$this->input->post('year'),
            'gender'=>$this->input->post('gender'),
            'interest'=>$interest,
            'status'=>1);

        $result=$this->student_model->update_student($data,$id);
        if($result==true)
        {
            $this->session->set_flashdata('msg',"Student Records Updated Successfully");
            redirect('student/list_students');

        }
        else
        {

            $this->session->set_flashdata('msg1',"No changes Made in Student Records");
            redirect('student/list_students');


        }
    }

}
?>

login.php Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    /**
    * Check if the user is logged in, if he's not, 
    * send him to the login page
    * @return void
    */  
    function index()
    {

        if($this->session->userdata('is_logged_in'))
        {
            redirect('student');
        }else{
            $this->load->view('login/header');
            $this->load->view('login/content'); 
        }
    }

    /**
    * encript the password 
    * @return mixed
    */  
    function __encrip_password($password) {
        return md5($password);

    }   

    /**
    * check the username and the password with the database
    * @return void
    */

    function validate()
    {   
        $this->load->model('login/login_model');
        $username = $this->input->post('username');
        $password = $this->__encrip_password($this->input->post('password'));
        $is_valid = $this->login_model->validate($username, $password);

        if($is_valid)/*If valid username and password set */
        {
            $get_id = $this->login_model->get_id($username, $password);

            foreach($get_id as $val)
                { 
                     $mobileno = $val->mobileno;
                     $fname = $val->firstname;
                     $lname = $val->lastname;
                     $state = $val->state;
                     $email=$val->email;
                     $city = $val->city;
                     $username=$val->username;
                     $adminid=$val->admin_id;

            }
           $data = array(
                'mobileno'=>$mobileno,
                'firstname'=>$fname,
                'lastname'=>$lname,
                'email'=>$email,
                'state'=>$state,
                'city'=>$city,
                'admin_id' => $adminid,
                'username' => $username,
                'is_logged_in' => true
            );
        //  print_r($data);
            $this->session->set_userdata($data); /*Here you can set the values in session */
            redirect('student');
        }
        else // incorrect username or password
        {
            $this->session->set_flashdata('msg', 'Username or Password Incorrect');
            redirect('login');
        }

    }

    /**
        * Destroy the session, and logout the user.
        * @return void
    */      
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('login');
    }

}  

login.php模型

<?php

class Login_model extends CI_Model {

    /**
    * Validate the login's data with the database
    * @param string $user_name
    * @param string $password
    * @return void
    */

    /*Check Login*/
    function validate($username, $password)
    {
        $this->db->where('password', $password);
        $this->db->where('username', $username);
        $query = $this->db->get('membership');
        if($query->num_rows == 1)
        {
            return true;
        }       
    }

    /*Get Session values */

    function get_id($username, $password)
    {
        $this->db->select('*');
        $this->db->from('membership');
        $this->db->where('password', $password);
        $this->db->where('username', $username);
        $query = $this->db->get();
        return $query->result();

    }

}

最佳答案

您需要在 validate()->$data 数组中设置一个附加值,例如 is_admin。根据此参数,您可以访问网站的某些管理区域,如下所示: 在 student.php Controller 中,

public function admin_list_students()
    {
         if($this->session->userdata('is_logged_in') and $this->session->userdata('is_admin'))
        { ...etc

关于php - 如何在 Codeigniter 中创建多个 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872916/

相关文章:

php - 在 Laravel 中验证用户时防止暴力攻击

php - 从 MySQL 中的 4 个表中选择数据

mysql varchar字节长度255问题

css - codeigniter Controller 和 controller/之间有什么区别?

php - MySQL Select 查询选取错误记录

php - PHP 的 HTTP 身份验证究竟是如何工作的?

java - 如何让 schemaspy 看到 mysql 连接器?

java - 在具有 csv 文件的变量上加载数据 infile

php - 与 PHP 对话从 Android 应用程序获取命令

mysql - 如果该行具有相同的值,则将光标移动到下一个