我知道如何在核心PHP中创建会话,我已经了解如何在codeigniter中执行此操作,但我无法理解如何检查,如果会话已设置?我试图通过View检查这个,但它总是给我meesage Please Login
。
请告诉我如何检查会话是否设置
控制器
if ($user_type=='Student')
{
if ($LoginData= $this->loginmodel->studentLogin($username,$password))
{
foreach($LoginData as $UserId)
{
$currentId= $UserId->StudentId;
}
//[[session]]
$data['students_data']= $this->loginmodel->student_profile($currentId);
$this->session->userdata('$data');
$this->load->view('students',$data);
}
else
{
//$data['message']= array('Invalid Username or Password');
$this->load->view('Login');
echo "Invalid Username or Password";
}
}
elseif ($user_type=="Faculty")
{
if($data['faculty_data']=$this->loginmodel->faculty_admin($username, $password))
{
$this->session->userdata('$data');
$this->load->view('faculty');
}
else
{
$this->load->view('Login');
echo "Invalid Username or Password";
}
}
查看
<?php
if (!$this->session->userdata('$data'))
{
echo "Please Login";
}
else
{
}
?>
<!DOCTYPE
答案 0 :(得分:12)
创建会话:
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe@some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
或
$this->session->set_userdata('some_name', 'some_value');
但在此之前,请确保包含会话库。
$this->load->library('session');
获取会话数据:
if($this->session->userdata('whatever_session_name_is')){
// do something when exist
}else{
// do something when doesn't exist
}
答案 1 :(得分:5)
if ($this->session->userdata('variable') !== FALSE) {
echo 'Variable is set';
} else {
echo 'Variable is not set';
}
答案 2 :(得分:0)
使用has_userdata
if ($this->session->has_userdata('username')) {
return TRUE;
} else {
return FALSE;
}