codeigniter check for user session in every controller
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
Codeigniter - set/check session in every controller function
You can use a hook : http://ellislab.com/codeigniter/user-guide/general/hooks.html
Or you can code your small script into the construct of your controllers.
function __construct()
{
parent::__construct();
//Put your code here, you can also load your models and stuff
//$this->load->model("user_model","user");
//Code code code
}
And here is an example of a hook for i18n (language module) -- Create a file under applications/hooks/my_hook.php
function setUserLang()
{
//Getting the language of the user
//If nothing was found, stick with English
$ci =& get_instance();
if(!$ci->session->userdata('lang')){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
$ci->session->set_userdata('lang','french');
break;
case "en":
$ci->session->set_userdata('lang','english');
break;
default:
$ci->session->set_userdata('lang','english');
break;
}
}
}
And go to applications/config/hooks.php and add something like :
$hook['post_controller_constructor'][] = array(
'class' => '',
'function' => 'setUserLang',
'filename' => 'my_hook.php',
'filepath' => 'hooks'
);
CodeIgniter check sessions in every controller?
Create a MY_Controller in the application/core folder which extends CI_Controller. Place your session functions in the MY_Controller file and then have the rest of your controllers extend MY_Controller instead of CI_Controller.
CodeIgniter - How to check session to be used at every methods
Create a file called MY_controller.php
(the prefix can be edited in config file) in /application/core
:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects all controllers
}
}
class Public_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects Public controllers. Probably not much needed because everyone can access public.
}
}
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects Admin controllers I.E. redirect and die if not logged in or not an admin
}
}
class Member_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects Member controllers. I.E. redirect and die if not logged in
}
}
Then anytime you create a new controller, you decide what access it requires
class Book extends Member_Controller {
//Code that will be executed, no need to check anywhere if the user is logged in.
//The user is guaranteed to be logged in if we are executing code here.
//If you define a __construct() here, remember to call parent::__construct();
}
This cuts code duplication a lot, since if you need another member controller other than Book
you can just extend the Member_Controller
. Instead of having to do the checks in all of them.
CodeIgniter: checking if user logged in for multiple pages
You can run code in every method of a Controller by running it in the __construct()
method:
function __construct()
{
parent::__construct();
if ( ! $this->session->userdata('logged_in'))
{
// Allow some methods?
$allowed = array(
'some_method_in_this_controller',
'other_method_in_this_controller',
);
if ( ! in_array($this->router->fetch_method(), $allowed)
{
redirect('login');
}
}
}
You can remove the "allowed" bits if you want to restrict access to the whole thing, but there are better ways to do this, like creating a base controller:
// Create file application/core/MY_Controller.php
class Auth_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
if ( ! $this->session->userdata('logged_in'))
{
redirect('login');
}
}
}
Then have your restricted controllers extend Auth_Controller
instead of CI_Controller
. Now your code will be run every time the controller is loaded.
More info on extending core classes: http://www.codeigniter.com/user_guide/general/core_classes.html#extending-core-class
Also of interest: http://php.net/manual/en/language.oop5.decon.php
Codeigniter show controller if session exists
You put the session $this->session->userdata('is_logged_in')
condition in __construct()
Like this :
<?php
class Lists extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('is_logged_in') != 1) {
//redirect code here
}
}
public function index() {
$this->load->view('lists');}
}
}
Related Topics
Forget Password Page, Creating a Generated Password to Email to the User
Symfony 2 - How to Pass Data to Formbuilder
Bind Param with Array of Parameters
How to Upload Files in Laravel Directly into Public Folder
Filling Gaps in Dates Returned from Database - Pure SQL Solution Possible
Double Results in My Array ( MySQL_Fetch_Array )
Using PHP as a Template Engine
Mysqli_Stmt::Execute() Expects Exactly 0 Parameters, 1 Given
Check If Url Has Certain String with PHP
Is "Set Character Set Utf8" Necessary
What's the Difference Between Array_Merge and Array + Array
Using MySQL Concat() in Where Clause
Issue in Installing PHP7.2-Mcrypt
How to Export HTML Table to Excel or PDF in PHP
What Is the Syntax for Sorting an Eloquent Collection by Multiple Columns