我已经使用用户名和密码登录到它将成功登录的页面,并且该页面具有其他页面的链接,但我将用户名设置为会话变量,但它会说会话不可用。
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');
}
}
?>