【问题标题】:Session Doesn't work in codeigniter会话在 codeigniter 中不起作用
【发布时间】:2015-11-17 08:50:47
【问题描述】:

在我的项目中,会话在几天前可以正常工作。但现在它不起作用。我找不到错误。请帮助我。它显示名为 Severity: Notice 的错误

消息:未定义
索引:名字
文件名:user_include/header.php
行号:5
遇到 PHP 错误
严重性:通知
消息:未定义
索引:id
文件名:user_include/header.php
行号:7

控制器

/ 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);
}
    }     

?>

型号

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

}

?>

查看

<?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");
}

【问题讨论】:

    标签: php mysql codeigniter session


    【解决方案1】:

    在您的模型中,用给定代码替换以下代码:

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

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

    在您的控制器中,将以下代码替换为给定

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

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

    在视图中,将您的代码替换为:

    <?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");
    }
    

    【讨论】:

    • 替换代码后遇到 PHP 错误严重性:通知消息:未定义索引:名字文件名:user_include/header.php 行号:5
    • 您必须在表用户上有名字字段。你呢?
    • 遇到 PHP 错误严重性:通知消息:未定义索引:名字文件名:user_include/header.php 行号:32
    • 清除会话(ctrl+shift+del)并在此处给出已登录会话的输出。
    【解决方案2】:

    错误在你 user_include/header.php 中,检查 id 和 firstname 在你回显之前设置。

    【讨论】:

    • 当我打印会话数组时,结果是 Array ( [session_id] => 9f9e42213f3dd75e1fe6d50eb4f4cc1e [ip_address] => 127.0.0.1 [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 ( KHTML,如 Gecko)Chrome/44.0.2403.157 Safari/537.36 [last_activity] => 1440383538 [user_data] => [logged_in] => 数组([first_name] => lak [email] => lak@gmail.com [id] => 19 ) )
    • 什么时候出现错误,是在登录之后还是之前?
    • 您的会话有名字,但您的会话用户数据设置为名字?请参阅您发布的打印会话。
    • 从浏览器中删除codeigniter session,登录你的app并重新打印session数据。
    • 现在它工作正常。问题是在我使用用户 ID 和会话 ID 过滤数据之前。现在它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2012-10-06
    • 2020-11-22
    • 1970-01-01
    相关资源
    最近更新 更多