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))
Related
I am using CodeIgniter.
I am working on the login page. There is no issue with login. I am getting the issue on login session.
I set if there is no login session then it will redirect on login page else redirect on the dashboard page.
Now I tried below code but it's not working properly. I am getting the error "Message: Undefined index: login_session"
What I did, I logged in the portal(Now I am on dashboard page ) and I just copy the URL and paste it on next tab. So it shows me my dashboard page correct. Now I log out from the second tab and after that, I refresh the first tab then it shows me Undefined index: login_session. So according to the first tab also redirect on the login page. correct?
Also sometimes I am on the portal and within few second my session logout.
public function index()
{
$this->checkLogin();
}
public function checkLogin(){
if(!($this->session->userdata('login_session')))
{
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('customer_id', 'Username', 'trim|required|min_length[12]');
$this->form_validation->set_rules('member_password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('member/login');
}
else
{
$custid = $this->input->post('customer_id');
$password = $this->input->post('member_password');
$result=$this->Member_model->check_password($custid,$password);//varifying password
if ($result) {
//creating session
$login_emp_session = array('firstname' => $result->first_name,'lastname' => $result->last_name);
$this->session->set_userdata('login_session',$login_emp_session);//set the session
redirect("Member_controller/dashboard");
}
else{
$this->session->set_flashdata('invalid_password', 'Invalid username and password');
$this->load->view('member/login');
//echo "wrong details";
}
}
}
else
{
//echo "Already";
redirect("Member_controller/dashboard");//calling employee register
}
}
Logout code
function logout()
{
$this->session->unset_userdata('login_session');
$this->session->sess_destroy();
redirect('Member_controller/index');
}
When you logout from second tab that means you destroying you login session from browser. That`s why on 1st tab when you refresh it shows that error. to avoid that error use:
if(!$this->session->userdata('login_session'))
{
redirect to login
}
//Dashboard
Updated :
public function index()
{
if(!$this->session->userdata('login_session'))
{
$this->checkLogin();
}
else
{
redirect("Member_controller/dashboard");//calling employee
}
}
public function checkLogin()
{
if($this->input->server('REQUEST_METHOD')=='POST')
{
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('customer_id', 'Username', 'trim|required|min_length[12]');
$this->form_validation->set_rules('member_password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('member/login');
}
else
{
$custid = $this->input->post('customer_id');
$password = $this->input->post('member_password');
$result=$this->Member_model->check_password($custid,$password);//varifying password
if ($result)
{
$login_emp_session = array('firstname' => $result->first_name,'lastname' => $result->last_name);
$this->session->set_userdata('login_session',$login_emp_session);//set the session
redirect("Member_controller/dashboard");
}
else
{
$this->session->set_flashdata('invalid_password', 'Invalid username and password');
$this->load->view('member/login');
}
}
}
else
{
$this->load->view('member/login');
}
}
I'm working on PHP-CodeIgniter project. I'm using ION Auth library for authentication.
My question is when I'm copy paste table data to Excel sheet, and then click on links copied to excel are not properly redirect to particular page. If I hover mouse on the link it shows proper address, but while clicking on it then it redirects to Dashboard not to particular page, their is a Active session data, but it redirects to Dashboard only.
Controller code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class Auth extends CI_Controller
{
function __construct()
{
parent::__construct();
// $this->load->library('');
$this->load->model('Landlord_m', 'l');
}
public function index()
{
redirect('auth/login');
}
public function login()
{
if( $this->ion_auth->logged_in() ) redirect('dashboard');
$this->form_validation->set_rules('email', 'email', 'required|valid_email|trim');
$this->form_validation->set_rules('password', 'password', 'required|trim');
$this->form_validation->set_message('required', 'Please enter your %s');
// Validate form
if( $this->form_validation->run() )
{
$remember = (bool) $this->input->post('remember');
// Check login
if( $this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember) )
{
// Login was successful
redirect('dashboard', 'refresh');
}
else
{
// Login was un-successful
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/login', 'refresh');
}
}
else
{
$data['message'] = $this->session->flashdata('message');
$this->load->view('auth/login', $data);
}
}
public function logout()
{
if( $this->ion_auth->logout() )
redirect('auth/login');
else
die("There was an error logging you out");
}
Any kind of help is welcome, Thanks in advance.
I have two controllers
1.Login
2.Dashboard
In Login controller I have two methods
1.logged_in()
2.logged_out()
This is my Login Controller
public function logged_in()
{
$user_email =$this->input->post('user_email');
$user_password =$this->input->post('user_password');
$result=$this->Login_model->login_data($user_email,$user_password);
if(!$result)
{
$this->session->set_flashdata('failure', 'Login failed');
redirect(BASE_URL.'admin/Login');
}
else
{
$data=array(
'user_email'=>$result[0]['user_email'],
'user_password'=>$result[0]['user_password'],
);
$this->session->set_userdata('session_data',$data);
$this->session->set_flashdata('success', 'Login sucessfully');
redirect(BASE_URL.'admin/Dashboard');
}
}
public function logged_out()
{
$this->session->unset_userdata('session_data');
$this->session->sess_destroy();
$this->session->set_flashdata('success', 'Logout sucessfully');
redirect(BASE_URL.'admin/Login');
}
And this is Dashboard controller :
class Dashboard extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
if($this->session->userdata('session_data')!='')
{
$this->load->view('admin/dashboard');
}
else
{
$this->session->set_flashdata('admin_flash', 'Try again');
redirect(BASE_URL."admin/Login");
}
}
}
I have a problem while after login it enters in my view which I have to load but the problem is that if I go back it goes to login page which i don't want.
so suggest me solution?
My idea is to make something like a boolean called logged by default false and set it true when the user log into the page and false if you log out. Then check if the user is login in the login page and if he's login redirect the user to the homepage
simply check whether user logged in or not in your login page. If user is already logged in, redirect him/her to dashboard.
In your code:
public function logged_in()
{
// check whether user is logged in or not,
// if yes redirect them to dashboard
if($this->session->userdata('session_data')!='')
{
redirect(BASE_URL."admin/dashboard");
}
$user_email =$this->input->post('user_email');
$user_password =$this->input->post('user_password');
$result=$this->Login_model->login_data($user_email,$user_password);
if(!$result)
{
$this->session->set_flashdata('failure', 'Login failed');
redirect(BASE_URL.'admin/Login');
}
else
{
$data=array(
'user_email'=>$result[0]['user_email'],
'user_password'=>$result[0]['user_password'],
);
$this->session->set_userdata('session_data',$data);
$this->session->set_flashdata('success', 'Login sucessfully');
redirect(BASE_URL.'admin/Dashboard');
}
}
I have write a custom login functionality in an old fashioned way.
1.If the email or password is incorrect it will shows the correct error message (ie invalid email,invalid password,account blocked)
2.If login is ok set a session user with corresponding row from the user table.
3.redirect to different url's according to usertype
Here is the implementation
public function login(Request $request)
{
$matches=['email'=>$request->email];
$users =User::where($matches)->first();
if($users == FALSE)
{
$request->session()->flash(
'errors',
'Invalid Email');
return redirect('adminlogin');
}
else if($users->account_status==0)
{
$request->session()->flash(
'errors',
'Account is blocked please contact admin');
return redirect('adminlogin');
}
else if (!Hash::check($request->password,$users->user_password))
{
$request->session()->flash('errors', 'Invalid Password');
return redirect('adminlogin');
}
else
{
$request->session()->put('user',$users);
if($users->user_type == 1)
{
$url = 'index';
}
else if($users->user_type == 3)
{
$url = 'index/package-home';
}
else
{
return view('errors.404');
}
return redirect($url);
}
}
Also in every page i've checked the user authentication with session value,if user session is not set it will redirect to login screen.
public function index(Request $request,$page='admin-home',$id=null)
{
if(!$request->session()->has('user'))
{
$request->session()->flash('errors', 'Session is expired');
return redirect('adminlogin');
}
//load dashboard
}
So my question is my method is correct for a custom authentication or do i need to anything else??
Note:
I don't like to use laravel default auth system,because it dosen't provide a way for custom error message or redirect to differnt url's based on usertype (ie admin,super admin etc)
I am enable enable_query_strings and want to redirect url when user login successful through redirect() method.
Url look like this:
http://localhost/DemoTest/index.php?c=admin_controller&m=login&d=backend
config file setting:
$config['uri_protocol'] = 'QUERY_STRING';
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
admin_controller Code:
function login() {
$data['page_title'] = "Admin Login";
if (isset($_POST['username'])) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', "Username", "trim|required");
$this->form_validation->set_rules('password', "Password", "trim|required");
if ($this->form_validation->run() == FALSE) {
$this->index();
} else {
$result = $this->admin_model->check_login();
if ($result) {
$user_data = array(
'username' => $_POST['username'],
'login' => TRUE,
);
$this->session->set_userdata($user_data);
site_url('m=admin_controller&t=dashboard&s=backend');
//Here i can call redirect() function to redirect this url
http://localhost/DemoSite/index.php?c=admin_controller&m=dashboard&d=backend
} else {
$this->session->set_flashdata('login_failed', 'The login info you entered is invalid');
}
}
}
$this->load->view('backend/templates/header_assets.php', $data);
$this->load->view('backend/login');
}
After a login successfully done page redirected using redirect(), see below example:
redirect('Login', 'refresh');
you just paste this line and replace login to your controller name and method