[英]Cannot get it correct when is use Session in codeigniter
I have set a session in one method and I want to check in another method, if the user session then user can redirect. 我已经在一种方法中设置了会话,但是我想签入另一种方法,如果是用户会话,则用户可以重定向。
public function validate_credentials(){ //function for validation of users_credentials
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) //if credential validated
{
$data1 = array(
'username' => $this->input->post('username'),
'is_logged_in' => true );
$this->session->set_userdata($data1);
redirect('site/members_area');
}
else{
$this->home();
}
}}
and i want to be able to use session in a different method below 我希望能够以下面的不同方法使用会话
function get_student(){
$is_logged_in = $this->session->set_userdata('is_logged_in');
if(isset($is_logged_in)){
$studentID = $this->input->get('var1');
$this->load_view($studentID);
$this->load->model('fetch_student'); //model for fetching student profile data
$data['student_prof'] = $this->fetch_student->get_profile($studentID);
$data['main_content']='student/student_profile';
$this->load->view('includes/template',$data);
}
else{
$this->_404();
}
}
Have your load session library in application/config/autoload.php ?? 在application / config / autoload.php中有加载会话库?
$autoload['libraries'] = array('session');
Actually, I didn't understood what want to write in get_student function but i think this is the way you want. 实际上,我不理解要在get_student函数中编写什么,但是我认为这是您想要的方法。
function get_student(){
$is_logged_in = $this->session->userdata('is_logged_in');
if($is_logged_in != ''){
//your code goes here
}
}
You should set and get session like this In method 1 您应该在方法1中设置并获取这样的会话
$this->session->set_userdata('is_logged_in','TRUE');
In method 2 在方法2中
$user = $this->session->userdata('is_logged_in');
if($user != "")
{
//your code goes here
}
public function validate_credentials(){ //function for validation of users_credentials public function validate_credentials(){//用于验证users_credentials的函数
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) //if credential validated
{
$data1 = array(
'username' => $this->input->post('username'),
'is_logged_in' => TRUE );
$this->session->set_userdata($data1);
redirect('site/members_area');
}
else{
$this->home();
}
}}
New Function will be: 新功能将是:
public function members_area(){
if($this->session->userdata('is_logged_in')){
///Your coding will be her
}
else{
redirect('controller/login');
}
}
function logout(){
$this->session->unset_userdata('is_logged_in');
redirect('controller/login');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.