I creating a library called 'Basic' and in the library i call 'ion auth' library to checking user login. It's working on php 5, but when application running on php 7 users cannot login to app. I was checking the session data is empty on php 7. what's wrong ?
My 'Basic.php' library :
public function check_login(){
//ajax request
if ($this->_ci->input->is_ajax_request()) {
if (!$this->_ci->ion_auth->logged_in())
{
if($this->is_pjax()){
die('<script>alert("Anda belum login atau sesi login anda sudah habis, silahkan melakukan login ulang.");window.location.replace("'.base_url().'");</script>');
}else{
die(json_encode(array('status'=>false,'noty'=>'Anda belum login atau sesi login anda sudah habis, silahkan melakukan login ulang.')));
}
}else{
$user_data=$this->_ci->ion_auth->user()->row();
$user_data->grup=$this->_ci->ion_auth->get_users_groups()->row()->name;
return $user_data;
}
}else{
if (!$this->_ci->ion_auth->logged_in())
{
// redirect them to the login page
redirect('login', 'refresh');
}
else
{
$user_data=$this->_ci->ion_auth->user()->row();
$user_data->grup=$this->_ci->ion_auth->get_users_groups()->row()->name;
return $user_data;
}
}
}
My 'Auth' Controller to handle login :
public function login()
{
if ($this->ion_auth->logged_in())
{
$this->index();
}
$this->form_validation->set_rules('email', str_replace(':', '', $this->lang->line('login_identity_email')), 'required|valid_email');
$this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required');
//ajax request
if ($this->input->is_ajax_request()) {
if(!$this->basic->is_pjax()){
header('Access-Control-Allow-Origin: *');
$res=array('status'=>false,'noty'=>'Login Gagal !');
//sleep(10);
$post=$this->input->post(null,true);
if ($this->form_validation->run() == true)
{
if ($this->ion_auth->login($post['email'], $post['password'], false))
{
$this->session->set_flashdata('message', $this->ion_auth->messages());
$res=array('status'=>true,'noty'=>'Login Berhasil !');
}
}else{
$res['noty']=(validation_errors()) ? validation_errors() : $this->session->flashdata('message');
}
die(json_encode($res));
}else{
$this->data['title'] = $this->lang->line('login_heading');
if ($this->form_validation->run() == true)
{
// check to see if the user is logging in
// check for "remember me"
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember))
{
$user_data=$this->ion_auth->user()->row();
$grupname=$this->ion_auth->get_users_groups($user_data->id)->row()->name;
//if the login is successful
//redirect them back to the home page
$this->session->set_flashdata('message', $this->ion_auth->messages());
if (!$this->ion_auth->is_admin()) // remove this elseif if you want to enable this for non-admins
{
// redirect them to the home page because they must be an administrator to view this
//redirect('/', 'refresh');
die('<script>window.location.replace("'.base_url().'dashboard");</script>');
}
else
{
die('<script>window.location.replace("'.base_url().'admin/dashboard");</script>');
//redirect('admin/dashboard', 'refresh');
}
}
else
{
// if the login was un-successful
// redirect them back to the login page
$this->session->set_flashdata('message', $this->ion_auth->errors());
$this->login_view();
///redirect('login', 'refresh'); // use redirects instead of loading views for compatibility with MY_Controller libraries
}
}
else
{
// the user is not logging in so display the login page
// set the flash data error message if there is one
$this->login_view();
}
}
}else{
$this->login_view();
}
}
And on every controllers i added this code to checking user login:
$this->data['user_data']=$this->basic->check_login();
I have same issues with this library: * Name: Ion Auth * Version: 2.5.2 * Author: Ben Edmunds with PHP 7
Basicaly i found problem in last parameter of function: login.
if remember parameter is set to 0 don't work if set to 1 it workS!
so in your code try this:
change
if ($this->ion_auth->login($post['email'], $post['password'], false))
with
if ($this->ion_auth->login($post['email'], $post['password'], true))