【发布时间】: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