Php 会话在CodeIgniter框架中不工作

Php 会话在CodeIgniter框架中不工作,php,mysql,codeigniter,session,Php,Mysql,Codeigniter,Session,我是Codeigniter的新手。下面是我使用codeiginiter进行注册和登录页面的项目教程 一切都很好,但会议不符合我的需要 我想要的:- 非登录用户也可以看到只有登录用户才能看到的私人区域。 这是我的密码: login.php控制器 defined('BASEPATH') or exit('No direct script access allowed'); class Login extends CI_Controller { public function __const

我是Codeigniter的新手。下面是我使用
codeiginiter
进行注册和登录页面的项目教程

一切都很好,但会议不符合我的需要

我想要的:-

非登录用户也可以看到只有登录用户才能看到的私人区域。

这是我的密码:

login.php控制器

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

class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if ($this->session->userdata('hospital_email')) {
            redirect('private_area');
        }
        $this->load->library('form_validation');
        $this->load->library('encryption');
        $this->load->model('login_model');
    }

    function index()
    {
        $this->load->view('view/login');
    }

    function validation()
    {
        $this->form_validation->set_rules('hospital_email', 'Email Address', 'required|trim|valid_email');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        if ($this->form_validation->run()) {
            $result = $this->login_model->can_login($this->input->post('hospital_email'), $this->input->post('pass'));
            if ($result == '') {
                redirect('private_area');
            } else {
                $this->session->set_flashdata('message', $result);
                redirect('view/login');
            }
        } else {
            $this->index();
        }
    }
}
defined('BASEPATH') or exit('No direct script access allowed');

class Private_area extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!$this->session->userdata('hospital_email')) {
            redirect('view/login');
        }
    }

    function index()
    {
        redirect('view/private_area');
    }

    function logout()
    {
        $data = $this->session->all_userdata();
        foreach ($data as $row => $rows_value) {
            $this->session->unset_userdata($row);
        }
        redirect('login');
    }
}
login_model.php模型文件

class Login_model extends CI_Model
{
    function can_login($email, $password)
    {
        $this->db->where('hospital_email', $email);
        $query = $this->db->get('hospital');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                if ($row->is_email_verified == 'yes') {
                    $store_password = $this->encryption->decrypt($row->pass);
                    if ($password == $store_password) {
                        $this->session->set_userdata('hospital_email', $row->hospital_email);
                    } else {
                        return 'Wrong Password';
                    }
                } else {
                    return 'First verified your email address';
                }
            }
        } else {
            return 'Wrong Email Address';
        }
    }
}
echo '<br /><br /><br /><h1 align="center">Welcome User</h1>';
echo '<p align="center"><a href="' . base_url() . 'private_area/logout">Logout</a></p>';
private_area.php控制器

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

class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if ($this->session->userdata('hospital_email')) {
            redirect('private_area');
        }
        $this->load->library('form_validation');
        $this->load->library('encryption');
        $this->load->model('login_model');
    }

    function index()
    {
        $this->load->view('view/login');
    }

    function validation()
    {
        $this->form_validation->set_rules('hospital_email', 'Email Address', 'required|trim|valid_email');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        if ($this->form_validation->run()) {
            $result = $this->login_model->can_login($this->input->post('hospital_email'), $this->input->post('pass'));
            if ($result == '') {
                redirect('private_area');
            } else {
                $this->session->set_flashdata('message', $result);
                redirect('view/login');
            }
        } else {
            $this->index();
        }
    }
}
defined('BASEPATH') or exit('No direct script access allowed');

class Private_area extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!$this->session->userdata('hospital_email')) {
            redirect('view/login');
        }
    }

    function index()
    {
        redirect('view/private_area');
    }

    function logout()
    {
        $data = $this->session->all_userdata();
        foreach ($data as $row => $rows_value) {
            $this->session->unset_userdata($row);
        }
        redirect('login');
    }
}
private_area.php查看文件

class Login_model extends CI_Model
{
    function can_login($email, $password)
    {
        $this->db->where('hospital_email', $email);
        $query = $this->db->get('hospital');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                if ($row->is_email_verified == 'yes') {
                    $store_password = $this->encryption->decrypt($row->pass);
                    if ($password == $store_password) {
                        $this->session->set_userdata('hospital_email', $row->hospital_email);
                    } else {
                        return 'Wrong Password';
                    }
                } else {
                    return 'First verified your email address';
                }
            }
        } else {
            return 'Wrong Email Address';
        }
    }
}
echo '<br /><br /><br /><h1 align="center">Welcome User</h1>';
echo '<p align="center"><a href="' . base_url() . 'private_area/logout">Logout</a></p>';
echo'



欢迎用户'; 回显“

”;
我正在尝试这些教程,但没有出现任何错误。我不知道你的意思,因为你没有提供更多的细节。但我有一个关于如何检查会话值的简单解决方案

Private_area.php的函数index()更改如下

$data['user'] = $this->session->userdata('hospital_email');
$this->load->view('private_area', $data);
在您的视图中
private_area.php

<?php
    echo '<br /><br /><br /><h1 align="center">Welcome '.$user.'</h1>';
    echo '<p align="center"><a href="private_area/logout">Logout</a></p>';
?>

还有另一种方法

如何检查会话值是否存在。

将此代码放在
private_area.php
查看文件的顶部:-

<?php
 $hospitalSession = $this->session->userdata('hospital_email');
if($hospitalSession['hospital_email']==''){
  redirect('Controller/method_name');   
}

 echo '<br /><br /><br /><h1 align="center">Welcome '.$user.'</h1>';
 echo '<p align="center"><a href="private_area/logout">Logout</a></p>';

?>


什么是“不工作”?您需要提供更多详细信息。@TimBrownlaw非登录用户还可以看到只有登录用户才能看到的私有区域。因此,此代码与教程中提供的代码有多少不同?如果未设置“hospital_email”会话,则用户无法访问“Private”控制器,并被重定向到登录页面。。。因此,你需要更详细地解释你的问题。听起来这个会话已经创建并且仍然有效。还有一个大问题。。。您是否完全按照教程设置了代码,并在修改之前对其进行了测试?