我的会话有问题。这不是毁灭。当我回到上一页时,会话仍在那里。
请查看我的代码:
user_model.php - 型号
<?php
class User_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
function login($username, $password)
{
$this->db->where("username", $username);
$this->db->where("password", $password);
$query=$this->db->get("users");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
//'user_id' => $rows->id,
'username' => $rows->username,
'logged_in' => TRUE
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
public function add_user()
{
$data=array(
'username'=> $this->input->post('username'),
'password'=> $this->input->post('password')// md5($this->input->post('password'))
);
$this->db->insert('users',$data);
}
}
?>
user.php - controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
if(($this->session->userdata('username') != "" ))
{
$this->welcome();
}
else {
$data['title']= 'Home';
$this->load->view('include/header',$data);
$this->load->view("frontpage", $data);
$this->load->view('include/footer',$data);
}
}
public function welcome()
{
$data['title']= 'Welcome';
$this->load->view('include/header', $data);
$this->load->view('include/navbar', $data);
$this->load->view('welcome_view', $data);
$this->load->view('include/sidebar', $data);
$this->load->view('include/ftr', $data);
$this->load->view('include/footer', $data);
}
public function login()
{
$username = $this->input->post('username');
$password = $this->input->post('password'); // md5($this->input->post('password'));
$result = $this->user_model->login($username, $password);
if($result)
$this->welcome();
else
$this->index();
}
public function thank()
{
$data['title']= 'Thank';
$this->load->view('include/header',$data);
$this->load->view('thank_view.php', $data);
$this->load->view('include/footer',$data);
}
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->user_model->add_user();
$this->thank();
}
}
public function logout()
{
$newdata = array(
//'user_id' =>'',
'username' => '',
'logged_in' => FALSE
);
$this->session->unset_userdata($newdata);
$this->session->sess_destroy();
$this->index();
}
}
?>
我不知道这些代码有什么问题。任何人都能指出我有什么不对吗?我真的需要你的帮助。感谢。
答案 0 :(得分:0)
将会话存储到数据库中。阅读CodeIgniter文档以了解如何执行此操作。我有同样的问题,会话表是一个解决方案。
答案 1 :(得分:0)
将此代码添加到 config.php 文件的末尾以防止页面缓存问题,
function __autoload($class)
{
header("cache-Control: no-store, no-cache, must-revalidate");
header("cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
}