登录会话不起作用 codeigniter 3.1.11 、 php 7.4

我正在尝试创建一个简单的登录会话,但我被重定向到登录页面,这是我的 Login.php 控制器。



> <?php class Login extends CI_Controller

 {  function __construct()  

{

>       parent::__construct();  

    if($this->session->userdata('admin'))

>           redirect('admin/dashboard'); 

    }   

function index() 

    {

>       $this->load->view('admin/login', $data); 

    }   function verify()   {

>       //username:admin password:123456    

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

>   $check = $this->admin->validate();   

    if($check)      { 

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

>           redirect('admin/dashboard');     

    }       

else        

{

>           

redirect('admin');      }    }

>   

> }


这是我的 Dashboard.php 控制器,如果用户名和密码正确,管理人员会还原该控制器

<?php

    class Dashboard extends CI_Controller

    {

        function __construct()

        {

            parent::__construct();

            if(!$this->session->userdata('admin'))

                redirect('admin');

        }

        function index()

        {

            $this->load->view('admin/dashboard');

        }

        function logout()

        {

            $this->session->sess_destroy();

            redirect('admin');

        }

    }

这是database.php



   $active_group = 'default';

               $query_builder = TRUE;

               

               $db['default'] = array(

                   'dsn'   => '',

                   'hostname' => 'localhost',

                   'username' => 'root',

                   'password' => '',

                   'database' => 'ASGB-test',

                   'dbdriver' => 'mysqli',

                   'dbprefix' => '',

                   'pconnect' => FALSE,

               )

       

       ;

这是我的 autoload.php,我在这里激活数据库和会话库

这是routes.php,欢迎控制器基本上是原始的codeigniter欢迎视图,我还定义了管理路由

我确信我输入了正确的密码和用户名,但它只是将我重定向到登录页面


LEATH
浏览 161回答 3
3回答

慕少森

你必须使用session_start();在需要会话变量的每个页面的 php 脚本的最开始处。

撒科打诨

解决了,我实际上错过了拼写“用户名”和“密码”,通过解决我成功登录的问题。感谢您的回答

繁星coding

首先,我不知道$data在Login 控制器的index()中做什么,以及validate()在模型 admin中做什么,那么我认为最好放置登录和注销功能在登录控制器中如下:<?php&nbsp;class Login extends CI_Controller{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function __construct(){&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; function index() {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('admin/login');&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public function log_in()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('admin');&nbsp; &nbsp; &nbsp; &nbsp; $this->form_validation->set_rules('username', 'Username', 'trim|required', array('required' => 'Username required'));&nbsp; &nbsp; &nbsp; &nbsp; $this->form_validation->set_rules('password', 'Password', 'trim|required', array('required' => 'Password required'));&nbsp; &nbsp; &nbsp; &nbsp; //username:admin password:123456&nbsp; &nbsp; &nbsp; &nbsp; if ($this->form_validation->run() !== false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $username = $this->input->post('username');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $password = $this->input->post('password');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $check = $this->admin->validate($username, $password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($check != false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_userdata('admin','1');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect('Dashboard');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect('Login');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('admin/login');&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public function log_out()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->session->sess_destroy();&nbsp; &nbsp; &nbsp; &nbsp; redirect('Login');&nbsp;&nbsp; &nbsp; }}?>那么你的仪表板控制器如下:<?php&nbsp; &nbsp; class Dashboard extends CI_Controller&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; function __construct()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!$this->session->userdata('admin'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect('Login');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function index()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('admin/dashboard');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>和管理模型如下:<?phpclass Admin extends CI_Model{&nbsp; &nbsp; function validate($username, $password)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->db->select('*')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->from('admins')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where('username', $username)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where('password', md5($password));&nbsp; &nbsp; &nbsp; &nbsp; $query = $this->db->get();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $query->result();&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP