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.
Related
Every time I login, in the model the session receives all the data correctly, but when the page is redirected to the home, the session data disappears.
I'm using codeigniter, and this is the model code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
if (isset($_SESSION['id_usuario']))
redirect(base_url());
$this->load->model('blogueiro_model');
$this->load->model('pagina_model');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('login.php');
}
public function checa()
{
if ( ! $this->input->post())
show_404();
$this->form_validation->set_rules('login', 'login', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
$login = html_escape($this->input->post('login'));
$senha = html_escape($this->input->post('password'));
if ($this->form_validation->run() === TRUE)
{
$usuario = $this->blogueiro_model->autentica($login, $senha);
if ($usuario !== NULL)
{
$this->session->set_userdata('id_usuario', $usuario->id);
$this->session->set_userdata('nome_usuario', $usuario->nome);
$this->session->set_userdata('login_usuario', $usuario->login);
$this->session->set_userdata('e_admin', $usuario->e_admin);
$this->session->set_userdata('foto', $usuario->foto);
$permissoes = ($usuario->e_admin == 0) ? $this->blogueiro_model->busca_permissoes($usuario->id) : $this->blogueiro_model->lista_todas_permissoes();
$this->session->set_userdata('permissoes', $permissoes);
$this->blogueiro_model->limpa_trials($login);
}
else
{
$this->blogueiro_model->incrementa_trials($login);
$this->index();
}
}
else
{
$this->index();
}
redirect(base_url());
}
}
Another thing I noticed was that the status code is always between 302 and 303, I researched a lot but found no solution to this problem.
Follow the status, preview and request images.
Status Code: 302 found
Preview
Request
Are you using Ci3 ?
Are you loading the session library in the controller that has the method that shows the "home".
You can load the library in the controller or methods like this: $this->load->library('session'); or you just can auto load the session library everywhere
adding the session library in application/config/autoload.php.
For more please check Auto loading in CodeIgniter 3
Try like this
$_SESSION['id_usuario'] = (int) $usuario->id;
$_SESSION['nome_usuario'] = (string) $usuario->nome;
The problem I'm facing is that manual new user subscription works locally but does not work on remote web server. The only difference between the non-working one and the one that works is that I upgraded Ion Auth to version 2 and added Facebook-Ion Auth library for Facebook Login.
Again, it works perfect locally, but not on the web server. PHP versions have been tested and work fine.
Here's the controller it gets stuck on (shows a blank page or goes back to homepage).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends CI_Controller {
function __construct()
{
parent::__construct();
$this->CI =& get_instance();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->library('forms/signupform');
$this->form_validation->set_error_delimiters(
$this->config->item('error_start_delimiter', 'ion_auth'),
$this->config->item('error_end_delimiter', 'ion_auth')
);
}
/**
* #signup page of freelancer
*/
public function index()
{
$data['title'] = lang('title_registration');
$data['bodyclass'] = 'hold-transition register-page';
$data['js_bottom_files'] = array('plugins/iCheck/icheck.min', 'js/custom');
$data['cssfiles'] = array('plugins/iCheck/square/blue');
// POST SIGNUP FORM
if ('POST' == $this->input->server('REQUEST_METHOD') && $this->input->post('registersubmit') ) {
// Signup Action
$data['message_success'] = $this->signup();
}
// $user['checked'] = '';
// if ($this->input->post('agree') == 'yes') {
// $user['checked'] = 'checked';
// }
$data['form'] = $this->signupform->view($user);
//Render Or redirect according to User AccessLevel
if (!$this->ion_auth->logged_in()) {
$this->template->load('layout', 'home', $data);
} elseif ($this->ion_auth->is_admin()) {
redirect(site_url('admin/dashboard'), 'refresh');
} elseif ($this->ion_auth->is_members()) {
redirect(site_url('note/create'), 'refresh');
}
}
/**
* #Signup action for user
*/
public function signup()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
// $this->form_validation->set_rules('full_name', lang('label_full_name'), 'required');
$this->form_validation->set_rules('loginPassword', lang('label_signup_createpassword'), 'required|min_length['.$this->config->item('min_password_length', 'ion_auth').']|max_length['.$this->config->item('max_password_length', 'ion_auth').']');
// $this->form_validation->set_rules('confirmpassword', lang('label_signup_confirmpassword'), 'required');
// $this->form_validation->set_rules('agree', 'Agree', 'required');
if ($this->form_validation->run() == true) {
$email = $this->input->post('email');
$password = $this->input->post('loginPassword');
// $additional_data = array('first_name' => $this->input->post('full_name'), 'school' => $this->input->post('school_name'));
$group_ids = array( 'user_groups' => 2);
if ($this->ion_auth->register($email, $password, $email, $additional_data, $group_ids)) {
//check to see if we are creating the user
//$this->session->set_flashdata('message_success', $this->ion_auth->messages());
//redirect(site_url('/'), 'refresh');
if($this->ion_auth->login($email, $password)){
redirect(site_url('note/create'), 'refresh');
}
} else {
$this->session->set_flashdata('message_error', $this->ion_auth->errors());
redirect(site_url('/'), 'refresh');
}
}
}
}
/* End of file signup.php */
Potentially not the issue without more information but:
$group_ids = array( 'user_groups' => 2); should just be $group_ids = array('2')
See usage of the register function here.
and in the Ion auth config file $config['identity'] should be set as email (if you haven't done so already).
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))
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');
}
}
What I want to implement is a simple login page, if user login successfully, then redirect to main page, else remain login page.
I have 1 controller named login, and 1 model named main.
When user click the login button, the login/login_send will be called.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->model('model_login');
}
function index()
{
if ($this->model_login->is_logged_in())
{
redirect('main');
}
else
{
// load login page
$data['main'] = 'view_login';
$data['style'] = 'style_login';
$this->load->view('template', $data);
}
}
function login_send()
{
$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
{
if ( $this->model_login->validate_user() )
{
$user_session_data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata($user_session_data);
redirect('main');
}
else
{
redirect('login');
}
}
}// end function login_send
function logout()
{
if ($this->model_login->is_logged_in())
{
$this->session->sess_destroy();
$this->session->set_userdata(array('username' => '', 'is_logged_in' => 0));
log_message('debug', 'Some variable was correctly set');
}
redirect('login','refresh');
}
}// end class Login
?>
Model_login here is just to help to verify if user is logged in, by check the session data.
<?php
class Model_login extends CI_MOdel{
function _isUserExist($username, $password)
{
$options = array(
'UserName' => $username,
'Password' => $password
);
$query = $this->db->get_where('userinfo', $options);
return $query->num_rows() > 0;
}
function validate_user()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
return ($this->_isUserExist($username, $password));
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if ( !isset($is_logged_in) || $is_logged_in != 1 ) return FALSE;
else return TRUE;
}
}// end class model_login
?>
When first time login, and then logout, there is no problem. However, if I login second time, I can not log out. Even the login/logout was called correctly, I also refreshed the page, but the session['is_logged_in'] == 1. Something wrong with my code?
In your application/config/config.php try changing
$config['sess_time_to_update'] = 300; //This is the default setting in ver 2.1.1
to
$config['sess_time_to_update'] = 0;
This gave me some problems a few years back