【问题标题】:session is not working in codeiginiter framework会话在 codeigniter 框架中不起作用
【发布时间】:2020-11-22 12:40:21
【问题描述】:

我是 Codeigniter 的新手。我正在关注https://www.webslesson.info/2018/10/user-registration-and-login-system-in-codeigniter-3.html 作为我的项目使用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();
        }
    }
}

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';
        }
    }
}

private_area.php 控制器

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 查看文件

echo '<br /><br /><br /><h1 align="center">Welcome User</h1>';
echo '<p align="center"><a href="' . base_url() . 'private_area/logout">Logout</a></p>';

【问题讨论】:

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

标签: php mysql codeigniter session


【解决方案1】:

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

像这样改变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>';
?>

【讨论】:

    【解决方案2】:

    还有一个办法,

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

    将此代码放在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>';
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2017-02-13
      • 2012-10-06
      相关资源
      最近更新 更多