我已登錄用戶名和密碼到它將登錄成功的頁面,該頁面有其他頁面的鏈接,但我設置用戶名到會話變量,但它會說會話不是availabe。codeigniter會話只適用於一個頁面不適用於鏈接到頁面的其他頁面
verifylogin.php
<?php
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login/user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('index');
}
else
{
//$this->load->view('home_view');
//Go to private area
$password = $this->input->post('password');
if($this->check_data($password))
{
redirect('login_f/home', 'refresh');
}
else
{
$data['username'] = $this->input->post('username');
$data['rannum'] = $this->send_confirm_email();
$this->load->view('/login/verify_code2',$data);
}
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
$this->load->model('login/user');
//query the database
$result = $this->user->login($username, $password);
$value = $this->user->user_type($username);
if($result)
{
if($value == 0)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
//'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
function check()
{
$this->load->view('index');
}
}
?>
home.php
<?php
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('login/home_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login_f/login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('login_f/home', 'refresh');
}
}
?>
你能發佈一些相關的代碼?謝謝。 – Tom
我認爲這是相關的 – user2422387