title: Codeigniter - How to avoid user entering logged page without login?
body: <p>I have Controller called <code>login.php</code> that will take login credentials, if true user will be directed to a method <code>profile()</code> in another controller called <code>page.php</code>.
  In that <code>profile()</code> method only contain a command to load the view of user's profile.<br><br>
  So the route is like this: <br>
  Home->login->profie <br>
  But when I try to bypass the login process via url like this <br>
  Home->profile <br>
  The system still accept that. How can I make a rule that a user can't open profile if they're not logged in?</p>
  
  <p>Here's the <strong>Controller:</strong></p>
  
  <p>page.php
      
  
  <pre><code>class page extends CI_Controller {
  
      function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;model('User_model', 'user_model', TRUE);
          $this-&gt;load-&gt;library('form_validation');    
          $this-&gt;form_validation-&gt;set_error_delimiters('&lt;div class="error"&gt;', '&lt;/div&gt;');
          $this-&gt;status = $this-&gt;config-&gt;item('status'); 
          $this-&gt;roles = $this-&gt;config-&gt;item('roles');
      }
  
      function index() {
          $this-&gt;load-&gt;view('page_header');
          $this-&gt;load-&gt;view('content_front');
          $this-&gt;load-&gt;view('page_footer');
      }
  
  
      function login() {
          $this-&gt;load-&gt;view('page_header');
          $this-&gt;load-&gt;view('content_login');
          $this-&gt;load-&gt;view('page_footer');
      }
  
      function register() {
  
         $this-&gt;load-&gt;view('page_header');
         $this-&gt;load-&gt;view('content_register');
         $this-&gt;load-&gt;view('page_footer');
  
     }  
  
  
      function profile(){
  
          $this-&gt;load-&gt;view('page_header');
          $this-&gt;load-&gt;view('content_profile');
          $this-&gt;load-&gt;view('page_footer');
  
      }
  
      function success() {
          $this-&gt;load-&gt;view('page_header');
          $this-&gt;load-&gt;view('content_success');
          $this-&gt;load-&gt;view('page_footer');
      }
  
      function logout()
      {
              //destroy session
          $data = array('login' =&gt; '', 'uname' =&gt; '', 'uid' =&gt; '');
          $this-&gt;session-&gt;unset_userdata($data);
          $this-&gt;session-&gt;sess_destroy();
  
          redirect('page/index');
      }
  
  
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>login.php</p>
  
  <pre><code>&lt;?php
  
  class Login extends CI_Controller
  {
      public function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;helper(array('form','url','html','security'));
          $this-&gt;load-&gt;library(array('session','form_validation'));
          $this-&gt;load-&gt;database();
          $this-&gt;load-&gt;model('user_model');
      }
  
      function index()
      {
          //get form input
          $username = $this-&gt;input-&gt;post('username');
          $password = $this-&gt;input-&gt;post('password');
  
          //form validation
          $this-&gt;form_validation-&gt;set_rules('username','username','trim|required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('password','Password','trim|required|xss_clean');
  
          if($this-&gt;form_validation-&gt;run() == FALSE)
          {
              //validation fail
              $this-&gt;load-&gt;view('content_login');
          }
          else
          {
              //check user credentials
              $uresult = $this-&gt;user_model-&gt;get_user($username, $password);
              if(count($uresult)&gt;0)
              {
                  //set session
                  $sess_data = array('login' =&gt; TRUE, 'uname' =&gt; $uresult[0]-&gt;username,'uid' =&gt; $uresult[0]-&gt;id);
                  $this-&gt;session-&gt;set_userdata($sess_data);
                  $this-&gt;load-&gt;library('../controllers/page');
                  $this-&gt;page-&gt;profile();
              }
              else
              {
                  $this-&gt;session-&gt;set_flashdata('msg','&lt;div class = "alert alert-danger text-center"&gt;Wrong Email/Password&lt;/div&gt;');
                  $this-&gt;load-&gt;library('../controllers/page');
                  $this-&gt;page-&gt;login();
  
              }
          }
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Can anyone please help me how to fix this?
  Thank you.</p>