简体   繁体   English

会话在Codeigniter中不起作用

[英]Session Doesn't work in codeigniter

In my project, session is work fine before few days.But now it doesn't work. 在我的项目中,会话可以在几天前正常工作,但现在不起作用。 i can't find the error. 我找不到错误。 plsease help me. 请帮助我。 it displays error called Severity: Notice 它显示错误,称为严重性:注意

Message: Undefined 讯息:未定义
index: firstname 索引:名
Filename: user_include/header.php 档名:user_include / header.php
Line Number: 5 行号:5
A PHP Error was encountered 遇到PHP错误
Severity: Notice 严重程度:注意
Message: Undefined 讯息:未定义
index: id 索引:id
Filename: user_include/header.php 档名:user_include / header.php
Line Number: 7 行号:7

controller 控制者

/ Check for user login process
public function user_login_process() {

$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

if ($this->form_validation->run() == FALSE) {
if(isset($this->session->userdata['logged_in'])){
//$this->load->view('admin_page');
    $this->home();
}else{
$this->load->view('user_site/login_form');
}
} else {
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if ($result == TRUE) {

$email = $this->input->post('email');
$result = $this->login_database->read_user_information($email);
if ($result != false) {
$session_data = array(
'firstname' => $result[0]->firstname,
'email' => $result[0]->email,
    'id' => $result[0]->id,
);
// Add user data in session
$this->session->set_userdata('logged_in', $session_data);

                    $this->load->view("user_include/header");
                    $this->load->view('user_site/index');
}
} else {
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('user_site/login_form', $data);
}
}
}





// Logout 
public function logout() {

// Removing session data
$sess_array = array(
'email' => ''


);
$this->session->unset_userdata('logged_in', $sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('user_site/login_form', $data);
}
    }     

?>

model 模型

// Read data using username and password
public function login($data) {

$condition = "email =" . "'" . $data['email'] . "' AND " . "password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('user');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {

return true;
} else {
return false;
}
}

// Read data from database to show data in admin page
public function read_user_information($email) {

$condition = "email =" . "'" . $email . "'";
$this->db->select('*');
$this->db->from('user');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}

}

?>

view 视图

<?php
if (isset($this->session->userdata['logged_in'])) {
$firstname = ($this->session->userdata['logged_in']['firstname']);
$email = ($this->session->userdata['logged_in']['email']);
$id = ($this->session->userdata['logged_in']['id']);
} else {
header("location: login");
}

错误出在您user_include / header.php中,在回显它们之前,请检查ID和名字是否已设置。

In your model replace following code by given code: 在模型中,将以下代码替换为给定代码:

public function read_user_information($email) {

    $condition = "email =" . "'" . $email . "'";
    $this->db->select('*');
    $this->db->from('user');
    $this->db->where($condition);
    $this->db->limit(1);
    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return $query->result();
    } else {
        return false;
    }
}

To

public function read_user_information($email) {
    $this->db->select('firstname, email, id');
    $this->db->from('user');
    $this->db->where('email',$email);
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->row_array();
    } else {
        return false;
    }
}

In your controller replace following code by given 在您的控制器中,将以下代码替换为

$email = $this->input->post('email');
$result = $this->login_database->read_user_information($email);
if ($result != false) {
$session_data = array(
'firstname' => $result[0]->firstname,
'email' => $result[0]->email,
    'id' => $result[0]->id,
);
// Add user data in session
$this->session->set_userdata('logged_in', $session_data);

                    $this->load->view("user_include/header");
                    $this->load->view('user_site/index');
}

To

$email = $this->input->post('email');
$user_details = $this->login_database->read_user_information($email);
if ($user_details != false) {
    // Add user data in session
    $this->session->set_userdata('logged_in', $user_details);
    $this->load->view("user_include/header");
    $this->load->view('user_site/index');   
}

In view, replace your code by following: 在视图中,请按以下步骤替换代码:

<?php
$user_details = $this->session->userdata['logged_in']);

if ($user_details != "") {
    $firstname = $user_details['firstname'];
    $email = $user_details['email'];
    $id = $user_details['id'];
} else {
    header("location: login");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM