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');
}
}
Related
In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
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.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}
I am trying to load a controller (Dashboard) if session is ok.
if ($this->form_validation->run() == FALSE) {
if(isset($this->session->userdata['logged_in'])){
echo 'dashboard-01'; //test load
$this->load->controllers('Dashboard');//Not sure if syntax is ok.
Is this possible? are there better approaches on how to do this?
What I usually do is to load the controller and check on its constructor if the user has enough credentials:
class Sociedades extends CI_Controller {
var $globales = array();
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('ion_auth','form_validation'));
// Elliot, if you see this, don't delete it!
$this->load->model('fSociety_model');
if (!$this->ion_auth->logged_in())
{
//redirect them to the login page if not authorized
redirect('auth/login', 'refresh');
}
}
// then the index and other methods...
}
By the way, I'm using Ben Edmund's IonAuth.
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
}
}
}
Firstly I am getting no errors, I am trying to create an is_logged_in() method in my header model in Code Igniter, but nothing in the index method of the controller will load. I added die(); into it and even that wont execute, Here is my code:
header.php - controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header extends CI_Controller {
public function index() {
print_r($this->session->all_userdata());
$data = array();
$data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
header_model.php - Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header_model extends CI_Model {
public function is_logged_in($username){
$q = $this
->db
->where('email_address', $username)
->limit(1)
->get('users');
die($q->last_query());
if($q->row('username') != $username){
return FALSE;
} else {
return TRUE;
}
}
}
Note: none of the die() functions in my code work.. Anything I add into the index function of the controller (which to my understanding is loaded by default) does not get executed...
Thanks in advance
public function __construct() { parent::__construct(); }
Add this method at your model else you wont have $this->db loaded
As AdrienXL pointed out the controller is only loaded whern the url /controller_name is called.. This wasn;t the case in my user case scenario.
Also something worth pointing out as Sevtilo mentioned above if you create a construct method in CodeIgniter you ovewrite the dafult calls for things such as $this->db class, using:
public function __construct() {
parent::__contsruct();
}
Will get the parent classes contsructor.
Regards
Ric
If you want to call this code transparently (ie without having to put any extra mess in the uri) then move the code into the constructor of an extension called MY_Controller.php in application/core that looks a bit like this...
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
print_r($this->session->all_userdata());
$data = array(); $data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
And then in your application/controllers files extend this class like
class Some_controller extends MY_Controller{
function __construct (){
parent::__construct();
}
public function index(){
//your header code will be run before this or any other method in this class
}
}
And the code from MY_Controller.php will run before any of your methods.
Hey! I'm very new to Codeigniter, I'm trying to protect the entire admin controller. I figured I'd start here:
function Admin()
{
parent::Controller();
if(!isset($_SESSION['loggedin'])){
$this->login();
}
}
but this is obviously incomplete. How do I also stop the method that is trying to run ( ie index() ), and am I on the right track here??
Thanks for your help!!
there is
Extend the base controllers:
MY_Controller.php
<?php
class MY_Controller extends Controller {
function __construct()
{
parent::Controller();
$user_id = $this->session->userdata('user_id');
$this->data['user'] = $this->user_lib->get($user_id);
}
}
?>
you can store all kinds of info in this construct. This just gets the currently logged in users ID and assigns it the $data['user'] . This will be adjusted depending on which sort of auth library you use but you get the gist. You now have access to the current users ID, and all their details, from within any controller that extends "MY_Controller"
now you can create an "admin" controller, or any number of other ones to restrict access. like so:
Admin_Controller.php
<?php
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::Controller();
if($this->data['user']['group'] !== 'admin')
{
show_error('Error - you need to be an admin.');
}
}
}
?>
Public_controller.php
<?php
class Public_Controller extends MY_Controller {
function __construct()
{
parent::Controller();
if($this->data['user']['group'] !== 'member')
{
show_error('You need to login to see this page...');
}
}
}
?>
as you can see..possibilities are endless
So, for admin only pages - use the admin controller
for member only pages - public
for "normal" pages - use the default controller.
I'll link to Phil Sturgeon's article as it's where I read about it first
put the checking session code in every function in Admin Controller that you want to protect.
that is the easiest way to do it..