我的会话有问题。这不是破坏。当我返回上一页时,会话仍然存在。
请看一下我的代码:
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 - 控制器
<?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();
}
}
?>
我不知道这些代码有什么问题。谁能指出我出了什么问题?我真的需要你的帮助。谢谢。