Zoe
Zoe

Reputation: 99

Why does the is_logged_in() method run in my CodeIgniter app?

I don't know why after I put exit; in the is_logged_in() function, the page ~/home/members became blank. If I comment exit; it can load the view page. p.s. $is_logged_in is not set yet.

<?php
    class Home extends CI_Controller {
        function __construct() {
            parent::__construct();
            $this->is_logged_in();
        }

        function members() {}

        function is_logged_in() {
            $is_logged_in = $this->session->userdata('is_logged_in');

            if(!isset($is_logged_in) || $is_logged_in != true) {
                $data['main_content'] = 'login_failure';
                $this->load->view('inc/template', $data);
                //exit();
            }
        }
    }
?>

Upvotes: 0

Views: 3493

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

Without using function in __construct use in each and every method. Load if only user valid, else it will load logging page

In controller

<?php
    class Home extends CI_Controller {
        function __construct() {
            parent::__construct();

        }

        function index()
        {
           $result =  $this->Model_name->is_logged_in();
            if($result==1)
            {
                //valid user
                //$this->load->view("admin_panel");
            }
            else
            {
                //invalid user
                //$this->load->view("loging");
            }            
        }


    }
?>

in Model

<?php

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');

        if(!empty($is_logged_in))
        {
            $log = 1;
            return $log;

        }
        else
        {
            $log = 0;
            return $log;
        }
    }

Upvotes: 1

Linus
Linus

Reputation: 909

Page is blank because there is nothing to show after exit try this

public function is_login() {


      $is_login=$this->session->userdata('is_login');
      if(!isset($is_login) || $is_login !=true)
      {
        //don;t echo the message from controller 
        echo "you don\'t have permission to access this page <a href="reference to login page">Login</a>";
        die();
      }
}

The actual problem was your use of exit. When you load a view, its output is added to the Output class (system/core/Output.php). The final view data is then sent (echoed) to the browser by the line $OUT->_display(); found in system/core/CodeIgniter.php.

Since you tossed the exit in there, the script stops, and that display method is never called.

Upvotes: 1

Related Questions