Hello Reader's, if you are making the website in which you want to redirect the user on the same page after the user login. Then you can learn it from this blog.
For example if a user hit 'my profile' without making the login, then the user must be redirect to myprofile once he make the login.
Lets learn the logic below this function behind it. Once user hit any page we will take that link and save it into session. And check condition if user is login or not if not then redirect it to login just keep it mind we have already saved the myprofile in the session data. Now as user do login check is any data is present in session or not. If found, redirect it to that data.
$nextURL = substr($_SERVER['PATH_INFO'], 1);
$userSiteActivity = array('urlTarget' => $nextURL);
$this->session->set_userdata('userSiteActivity', $userSiteActivity);
$this->checklogin();
Session routing is very usefull for giving user more user friendly experience. Also it will save server process for opening the same page "home " again and again. Now in the checklogin() function code will go like this:-
public function SignIn($data)
{
$this->db->select('*');
$this->db->from('user_registrations');
$this->db->where('email', $data['userEmail'] );
// $this->db->where('status', 1 );
$this->db->where('password', $data['userPassword']);
$query = $this -> db -> get();
$result = $query->row_array();
if($query->num_rows()>0){
if($result['verified'] == "0")
{
$this ->session->set_flashdata('msg','Please enter the activation code send to you');
$userTempData = array('id'=>$result['id'],'code' => $result['token'], 'email' => $result['email'], 'password' => $result['password']);
$this->session->set_userdata('userAccountActivation', $userTempData);
redirect(base_url().'user/activateAccount','refresh');
}else if($result['status'] == "0"){
$this ->session->set_flashdata('msg','Your Account has beed suspended by admin');
redirect(base_url().'user/Login','refresh');
}else{
$user_data = array('id'=>$result['id'], 'name'=>$result['first_name']." ".$result['last_name'], 'email' => $result['email'], 'username' => $result['username'], 'landline' => $result['landline'] , 'mobile' => $result['mobile']);
$this->session->set_userdata('userdata', $user_data);
$this->session->unset_userdata('userAccountActivation');
}
if($this->session->userdata('userSiteActivity')['urlTarget'])
{
redirect(base_url().$this->session->userdata('userSiteActivity')['urlTarget']);
} else{
redirect(base_url(),'refresh');
}
}else{
$this ->session->set_flashdata('msg','Invalid Login Details');
redirect(base_url().'user/Login','refresh');
}
}
In the code function login above you can see if($this->session->userdata('userSiteActivity')['urlTarget']) is set (My profile link) then function will redirect user to this page else function will redirect user to 'home' page. Similarly you can put this session link to each and every link so that user can easily redirected to it just after login.
Last don't forget clear this session otherwise you will be get redirected to same page again and again.
0 Comment(s)