After the user logs in, I set the variable is_logged_in=true, but in some other controller how can I check is_logged_in is true in codeigniter?
Here is my login code:
public login_con extends CI_Controller
{
public function login()
{
is_logged_in=true;
}
}
I want to check this is_logged_in in another controller so how can i write code for that?
Session is best solution for this. You can read codeignitor session
// set value in session
$this->session->set_userdata('is_logged_in', true);
To get in other controller
$is_logged_in = $this->session->userdata('is_logged_in');
Please also make sure you have loaded session library.
$this->load->library('session');
First thing autoload applications/config/autoload.php, to add session library
$autoload['libraries'] = array('session');
This will include session on every page.
Now your controller file
controller1
public login_con extends CI_Controller
{
public function login()
{
//here you set session like that
$data['is_logged_in'] = TRUE;
$this->session->set_userdata($data);
}
}
Here you get your is_logged_in session on other controller
controller2
public your_con extends CI_Controller
{
public function your_function()
{
//here you get session like that
if($this->session->userdata("is_logged_in"))
{
// your code here
}
}
}
Related
I am trying to redirect to controller index if not authorized the access to other functions within same controller. According to my coding it is looking like infinite loop. Please help me to do that.
class Customer_Dashboard extends CI_Controller {
public function __construct() {
$method= $this->router->fetch_method();
if ($this->session->userdata("cus_sel_comp")) {
}else{
if($method !="index"){
redirect(base_url()."customer_dashboard");exit;
}
}
}
public function index() {
// Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
}
public function other_function1() {
}
public function other_function2() {
}
}
My coding is as above. I need to do this using same controller. Problem is if that session not set there is a infinite loop.
Instead of redirecting return index function. See the code below
if($method !="index"){
return $this->index();
}
You are calling the same function and redirecting it to same method.
class Customer_Dashboard extends CI_Controller {
public function __construct() {
$method= $this->router->fetch_method();
if ($this->session->userdata("cus_sel_comp")) {
}else{
if($method !="index"){
redirect(base_url()."Customer_Dashboard/index"); // Redirect it to index if other method is invoked.
}
}
}
public function index() {
// Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
}
public function other_function1() {
}
public function other_function2() {
}
}
Also dont use base_url() instead of that define an path in config
base_url() has many other entries present which are un-necessarily called.
Is it allowed to load multiple models in single Controller ? And my each model is loading same database in their controller. Thanks
function __construct()
{
parent::__construct();
$this->load->database() or die("Cannot open database");
$this->load->model("Admin/Product_model");
$this->load->model("Admin/Category_model");
$this->load->model("Admin/Attribute_model");
$this->load->model("Admin/Attribute_value_model");
}
This is my controler constuctor. And in constuctors of all models I am loading same database. Like these,...
Class Attribute_value_model extends CI_Model
{
Name : function __construct
Returns : NULL
Use : This is the constructor of project, loads the database on every times page is called
function __construct()
{
parent::__construct();
$this->load->database("default") or die("Cannot open");
}
Issue resolved
Insted of loading database from each model, I directly loadaed
default database from autoload.php.
So it prevents loading same
database again
//In autoload.php
$autoload['libraries'] = array('mailchimp','session','form_validation','database');
Yes, you can load multiple models in a single controller.You can tell the model loading method to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file.
for example:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
//session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('user', 'guru', TRUE);
$this->load->model('student', 'santosh', TRUE);
}
function index() {
//echo $this->session->userdata('validuser');
// die('here');
if ($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$pankaj = $this->guru->userinfo(1);
var_dump($pankaj);
$santosh = $this->santosh->studentinfo(1);
var_dump($santosh);
$this->load->view('home_view', $data);
} else {
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout() {
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
$this->is_logged_in();
}
function is_logged_in()
{
$is_logged_in=$this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
//$this->load->view('notlogin');
$this->load->view('admin_login');
//echo "you dont have permission to access this area";
}
}
function index()
{
$this->load->view('admin_login');
//die();
}
function adminlogin()
{
$this->load->model('loginmodel');
$query=$this->loginmodel->verify();
if($query==true){
$data=array(
'username'=>$this->input->post('username'),
'is_logged_in'=>true
);
$this->session->set_userdata($data);
redirect('Login/loginarea');
}
else
{
//$this->is_logged_in();
$this->index();
}
}
public function loginarea()
{
$this->load->view('mainadmin');
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
Here two function __construct and index is loading the same view twice. I don't how to logically correct this. I am checking is_logged_in variable is true or not: if true then load admin area or else admin login page. But index function is also getting executed along with it.
When you call your controller, it will always execute the constructor and the function you have specified in the second segment or by default, index().
In your case, if we assume your URL is http://example.com/index.php/Login
The first thing to be executed is the constructor which calls $this->is_logged_in();
If we assume that the login failed, you load the view with this call $this->load->view('admin_login');
That's it for the constructor.
Then codeigniter call the default function index() which also loads a view $this->load->view('admin_login');
So, that's why your view appears two times.
Imo, the easiest way to fix this is by removing the code in index(). You are not doing anything special in that function and the view is loaded inside is_logged_in() when the constructor is executed.
However, I don't understand why do you check the user status in the controller supposed to authenticate people.
It shouldn't be done that way imho :
Actually, you check if the user is not logged. Instead, I would have have check if the user is logged then I redirect to the admin area or whatever it is.
This way, index() will be the function that displays admin_login and the constructor via is_logged_in() displays the other view.
How to set user previleges in array using codeigniter. for example i have a controller name prospect and its method. I have to check if the logged in user have permission to access these controller methods. how to check anyone please give me advise on this.
You can set permission to access these controller/methods in constructor of the controller.
See the example:
class Admin extends CI_Controller {
function __construct()
{
parent::__construct();
$session_user = $this->session->userdata('session_array');
$method = $this->router->fetch_method();
if(empty($session_user) && $method != 'login'){
redirect('admin/login');
}
}
public function login(){
// Login method
}
public function index(){
// home page method
}
}
Here you can only access the function login from the controller admin. If you access the other functions it redirects to login method of admin controller if the session is not present.
I'm using a codeigniter and i have 3 controller, i use the session to store username from the 1st controller and i use this to determine from the other controller if someone is logged in.
Here is a the constructor of the first controller, in this class, i also use the statement where i give a value to $_SESSION['username'] so the other controller use it:
class Login extends CI_Controller {
public function __construct()
{
session_start();
parent::__construct();
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
if ( $this->form_validation->run() !== false ) {
// then validation passed. Get from db
$this->load->model('Login_Model');
$res = $this->Login_Model->verify_user(
$this->input->post('userName'),
$this->input->post('password')
);
if ( $res !== false ) {
$tempuser = $this->input->post('username');
$_SESSION['username'] = $tempuser;
redirect(base_url());
}
}
$this->load->view('login_view');
}
This is the 2nd controller, on he constructor, i created a conditional statement to determine if someone is logged in, i tested this and it's still ok:
class Pagination extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
session_start();
if(!isset($_SESSION['username'])){
redirect(base_url().'index.php/login');
}
}
In the 3rd controller, this is where i encounter the problem, i cannot access the variable where i stored the username, here is the code:
class Blood_Controller extends CI_Controller {
public function __construct()
{
session_start();
parent::__construct();
if(!isset($_SESSION['username'])){
redirect(base_url().'index.php/login');
}
}
It always go inside the if statement even if i enter a rightusername on the 1st controller.
can anyone help?
load this "$this->load->library('session'); " in the constructor ie in here
public function __construct()
{
session_start();
parent::__construct();
}
and remove the session_start() and use
`$this->session->set_userdata('username', $tempuser);`
It's always good idea to check if session is already exists.
Try:
if (!session_id())
session_start();
And try to var_dump($_SESSION); in 3rd controller after session_start() to check what is inside.
Need to set session data using $_SESSION array in this case instead of CI session library ie. replace $this->session->set_userdata('username', $tempuser); with $_SESSION['username'] = $tempuser
remove all the session_start(); and set it in the config -> autoload.php
change '!=='to "!=' and check the session variable value
You can initialize the Session class manually in your controller constructor, use the $this->load->library function:
$this->load->library('session');
Don't need to use session_start() in codeigniter.
Once loaded, the Sessions library object will be available using: $this->session
On the other hand if you don't want to initialize the Session class manually in your every controller constructor, then you can autoload it from your application/config/autoload.php file,just add session class name in $autoload['libraries'] array like this:
$autoload['libraries'] = array('database', 'form_validation', 'session');
Now you can set your session data like this way:
$this->session->set_userdata('user_name', $user_name);
And you can retrieve your session data like this way:
$user_name = $this->session->userdata('user_name');
Go here to learn more about CI session : http://ellislab.com/codeigniter/user-guide/libraries/sessions.html