Can I pass a function to the view in CodeIgniter? The function basically checks if the session value is set. For example:
public function is_logged(){
$logged = $this->session->userdata('user_id');
if ($logged){
return true;
} else {
redirect('index');
}
}
Now i want to place this function on some of my view. so how can i pass this function to the view?
Thanks.
I would take a different approach, much like #atno said: you're using MVC pattern, so doing this kind of checks in your view is 'logically' wrong as well as going against a DRY approach.
I would do the check in controller, using the function I have in the model, and load the appropriate view according to the results:
class Mycontroller extends CI_Controller {
function index() //just an example
{
$this->load->model('mymodel');
if($this->mymodel->is_logged())
{
$this->load->view('ok_page');
}
else
{
$this->load->view('not_logged_view');
//OR redirect('another_page','refresh')
}
}
}
In your model:
function is_logged()
{
$logged = $this->session->userdata('user_id');
if ($logged)
{
return TRUE;
} else {
return FALSE;
}
}
If it's someting you need to do programmatically, for every method of a controller (like checking for being logged in), you can check inside the constructor:
function __construct()
{
parent::__construct();
// check code here
}
In this way you'll have the check before any method of the controller is called, i.e. upon controllers' initialization.
UPDATE:
using a model can be overkill here, you can just check what $this->session returns:
function index() { // or mypage() or whatever
if($this->session->user_data('user_id'))
{
$this->load->view('ok_page');
}
else
{
$this->load->view('not_ok_page');
}
}
You shouldn't be doing that. Just have this code directly in your layout, or just have it in your view.
You could also create a helper : http://codeigniter.com/user_guide/general/helpers.html
Related
I am checking whether the user is logged in or not in constructor method, if not logged in then redirect to login method but it is looping infinite time.login method is defined in same controller.
issue:
1)For testing purpose just now I wrote same(login) method in 'Welcome' controller in that case it is working fine and redirect perfectly,after that I commented method in 'Welcome' controller and now login method is uncommented in user controller now it is not working
class User extends CI_Controller {
function __construct() {
parent::__construct();
is_logged_in(); // defined in custom helper
if (!is_logged_in()) {
redirect(base_url().'User/Login');
}
}
public function login(){
$this->load->view('loginpage');
}
}
//code in custom helper
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('id');
if (!isset($user)) {
return false;
}
else {
return true;
}
}
if your helper returns false your code never reaches the login function of the controller, it always passes by the constructor, which redirects and so on...
change this:
if (!is_logged_in()) {
$this->login();
}
now it calls the login function, if not logged_in(), otherwise it continues with the controller's index() function
Please Use Helper In Controller.
class User extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('name');
$checkLogin = $this->helperName->is_logged_in(); // defined in custom helper
if (!$checkLogin) {
redirect(base_url().'User/Login');
}
}
I guess you autoload your helper.
Maybe your helper function need to changed
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('id');
if (!isset($user) || is_null($user)|| empty($user)) {
return false;
}else {
return true;
}
}
Try this way. This is working fine.
Controller
function __construct(){
parent::__construct();
if(!is_login()){
redirect(site_url().'/User/login');
}
}
Helper
function is_login(){
//Your Logic
return false;
}
autoload.php
$autoload['helper'] = array('your_helper_class_name');
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 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.
I have a login helper in my CodeIgniter project.
In every constructor of a controller I call a function to check if the user is logged in.
However when calling a function(getSetup) when logged out the login_view appears but also the other view db_manage for example. But I don't want to show content when not logged in.. How to fix this ?
Thanks
function __construct() {
parent::__construct();
if (!is_logged_in()) {
$this->load->view('login_view');
}
}
public function getSetup() {
$this->load->view("db_manage");
}
this way:
function __construct() {
parent::__construct();
if (!is_logged_in()) {
echo $this->load->view('login_view', null, TRUE);
exit();
}
}
yes, you can't exit in the c'tor that will stop Codeigniter from doing its render process from the output buffer. You can't really do this from the c'tor, its not really what the c'tor is for, its meant for setting up the class variables..
you should check the logged in from the method, and return the login view from there.
class My_Controller {
function ensureLoggedIn() {
if(!is_logged_in()) {
$this->load->view('login_view');
return False;
}
return True;
}
function getSetup() {
if(!$this->ensureLoggedIn())
return;
.... rest of method ...
}
}
#egig - whats the point in using a framework if your going to bypass the stack?!
I have a controller called member within this a construct function
function __construct()
{
parent::Controller();
$this->is_logged_in();
}
I want to check in my other controller that user is logged in how I can use this function in my other controller called profile and others
This is my First project with CodeIgniter
Your authentication checks should be in a library:
The is an excerpt from a basic codigniter authentcation script:
class Site_sentry
{
function Site_sentry()
{
$this->obj =& get_instance();
}
function is_logged_in()
{
if ($this->obj->session)
{
if ($this->obj->session->userdata('session_logged_in'))
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
function login_routine()
{
//do login here (enter into session)
}
}
This library is stored in application/libraries under a filename named after its class with the .php suffix.
Then you can either add this to your autoload config file application/conig/config.php:
$autoload['libraries'] = array('database', 'site_sentry', 'session');
or load it manually in each controller:
$this->load->library('Site_sentry);
Then you can check your session from within controllers, like so:
class Class extends Controller{
function Class()
{
parent::Controller();
if( $this->site_sentry->is_logged_in() == FALSE){
redirect('managerlogin/');
}
}
}
Also check this documentation page http://codeigniter.com/user_guide/libraries/sessions.html; of particular interest is the storing the session into the database section.
I don't think that doing it with the class is the best idea. If the user is logged in, you should check for a flag (value or whatever) inside the session, so you don't need to work with the other controller.
The advantage would be that the session can be accessed more easily and it is the more common approach.
Example with session:
class SomeClass extends Controller {
function __construct()
{
parent::Controller();
$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)
{
redirect('login');
}
}