I have a CodeIgniter 2 app that currently returns a 404 page anytime an user requests an invalid URL. Inside my MY_controller.php file, I have a function that checks whether the user is authenticated. If the URL the user requested is valid and the user is not logged in, they are redirected to the login page. If the URL they requested is invalid, they'll see a 404 page whether or not they are logged in.
If the user is not logged in, is there anyway to redirect the user to the log in page even when they request an invalid URL?
Yes possible. steps given below
Create a new controller named Errors
in your controllers directory with following code
defined('BASEPATH') OR exit('No direct script access allowed');
class Errors extends CI_Controller {
public function index() {
redirect('authentication/login');
}
}
Now in your config/routes.php
file $route['404_override']
change as following
$route['404_override'] = 'errors/index';
Now you will be redirected when wrong URL given instead showing 404 error page
If you want logged in user will see 404 error but NOT LOGGED IN user will see login page then change your index
method in Errors
controller as
public function index() {
if($this->session->userdata('user_id')){// check any session item
show_404();
}
redirect('authentication/login');
}