Codeigniter - How to avoid user entering logged page without login?












0















I have Controller called login.php that will take login credentials, if true user will be directed to a method profile() in another controller called page.php.
In that profile() method only contain a command to load the view of user's profile.


So the route is like this:

Home->login->profie

But when I try to bypass the login process via url like this

Home->profile

The system still accept that. How can I make a rule that a user can't open profile if they're not logged in?



Here's the Controller:



page.php



class page extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
}

function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}


function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}

function register() {

$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');

}


function profile(){

$this->load->view('page_header');
$this->load->view('content_profile');
$this->load->view('page_footer');

}

function success() {
$this->load->view('page_header');
$this->load->view('content_success');
$this->load->view('page_footer');
}

function logout()
{
//destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();

redirect('page/index');
}


}


?>


login.php



<?php

class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url','html','security'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}

function index()
{
//get form input
$username = $this->input->post('username');
$password = $this->input->post('password');

//form validation
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');

if($this->form_validation->run() == FALSE)
{
//validation fail
$this->load->view('content_login');
}
else
{
//check user credentials
$uresult = $this->user_model->get_user($username, $password);
if(count($uresult)>0)
{
//set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
$this->load->library('../controllers/page');
$this->page->profile();
}
else
{
$this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
$this->load->library('../controllers/page');
$this->page->login();

}
}
}
}

?>


Can anyone please help me how to fix this?
Thank you.










share|improve this question























  • stackoverflow.com/questions/31261007/…

    – shafiq
    Mar 1 '17 at 15:32











  • you can use session concept. Please check the whether that session exist or not. Please check and confirm

    – Muthusamy
    Nov 2 '18 at 14:02
















0















I have Controller called login.php that will take login credentials, if true user will be directed to a method profile() in another controller called page.php.
In that profile() method only contain a command to load the view of user's profile.


So the route is like this:

Home->login->profie

But when I try to bypass the login process via url like this

Home->profile

The system still accept that. How can I make a rule that a user can't open profile if they're not logged in?



Here's the Controller:



page.php



class page extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
}

function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}


function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}

function register() {

$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');

}


function profile(){

$this->load->view('page_header');
$this->load->view('content_profile');
$this->load->view('page_footer');

}

function success() {
$this->load->view('page_header');
$this->load->view('content_success');
$this->load->view('page_footer');
}

function logout()
{
//destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();

redirect('page/index');
}


}


?>


login.php



<?php

class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url','html','security'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}

function index()
{
//get form input
$username = $this->input->post('username');
$password = $this->input->post('password');

//form validation
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');

if($this->form_validation->run() == FALSE)
{
//validation fail
$this->load->view('content_login');
}
else
{
//check user credentials
$uresult = $this->user_model->get_user($username, $password);
if(count($uresult)>0)
{
//set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
$this->load->library('../controllers/page');
$this->page->profile();
}
else
{
$this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
$this->load->library('../controllers/page');
$this->page->login();

}
}
}
}

?>


Can anyone please help me how to fix this?
Thank you.










share|improve this question























  • stackoverflow.com/questions/31261007/…

    – shafiq
    Mar 1 '17 at 15:32











  • you can use session concept. Please check the whether that session exist or not. Please check and confirm

    – Muthusamy
    Nov 2 '18 at 14:02














0












0








0








I have Controller called login.php that will take login credentials, if true user will be directed to a method profile() in another controller called page.php.
In that profile() method only contain a command to load the view of user's profile.


So the route is like this:

Home->login->profie

But when I try to bypass the login process via url like this

Home->profile

The system still accept that. How can I make a rule that a user can't open profile if they're not logged in?



Here's the Controller:



page.php



class page extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
}

function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}


function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}

function register() {

$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');

}


function profile(){

$this->load->view('page_header');
$this->load->view('content_profile');
$this->load->view('page_footer');

}

function success() {
$this->load->view('page_header');
$this->load->view('content_success');
$this->load->view('page_footer');
}

function logout()
{
//destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();

redirect('page/index');
}


}


?>


login.php



<?php

class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url','html','security'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}

function index()
{
//get form input
$username = $this->input->post('username');
$password = $this->input->post('password');

//form validation
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');

if($this->form_validation->run() == FALSE)
{
//validation fail
$this->load->view('content_login');
}
else
{
//check user credentials
$uresult = $this->user_model->get_user($username, $password);
if(count($uresult)>0)
{
//set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
$this->load->library('../controllers/page');
$this->page->profile();
}
else
{
$this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
$this->load->library('../controllers/page');
$this->page->login();

}
}
}
}

?>


Can anyone please help me how to fix this?
Thank you.










share|improve this question














I have Controller called login.php that will take login credentials, if true user will be directed to a method profile() in another controller called page.php.
In that profile() method only contain a command to load the view of user's profile.


So the route is like this:

Home->login->profie

But when I try to bypass the login process via url like this

Home->profile

The system still accept that. How can I make a rule that a user can't open profile if they're not logged in?



Here's the Controller:



page.php



class page extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
}

function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}


function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}

function register() {

$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');

}


function profile(){

$this->load->view('page_header');
$this->load->view('content_profile');
$this->load->view('page_footer');

}

function success() {
$this->load->view('page_header');
$this->load->view('content_success');
$this->load->view('page_footer');
}

function logout()
{
//destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();

redirect('page/index');
}


}


?>


login.php



<?php

class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url','html','security'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}

function index()
{
//get form input
$username = $this->input->post('username');
$password = $this->input->post('password');

//form validation
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');

if($this->form_validation->run() == FALSE)
{
//validation fail
$this->load->view('content_login');
}
else
{
//check user credentials
$uresult = $this->user_model->get_user($username, $password);
if(count($uresult)>0)
{
//set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
$this->load->library('../controllers/page');
$this->page->profile();
}
else
{
$this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
$this->load->library('../controllers/page');
$this->page->login();

}
}
}
}

?>


Can anyone please help me how to fix this?
Thank you.







php codeigniter session






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 1 '17 at 15:24









bnrflybnrfly

7818




7818













  • stackoverflow.com/questions/31261007/…

    – shafiq
    Mar 1 '17 at 15:32











  • you can use session concept. Please check the whether that session exist or not. Please check and confirm

    – Muthusamy
    Nov 2 '18 at 14:02



















  • stackoverflow.com/questions/31261007/…

    – shafiq
    Mar 1 '17 at 15:32











  • you can use session concept. Please check the whether that session exist or not. Please check and confirm

    – Muthusamy
    Nov 2 '18 at 14:02

















stackoverflow.com/questions/31261007/…

– shafiq
Mar 1 '17 at 15:32





stackoverflow.com/questions/31261007/…

– shafiq
Mar 1 '17 at 15:32













you can use session concept. Please check the whether that session exist or not. Please check and confirm

– Muthusamy
Nov 2 '18 at 14:02





you can use session concept. Please check the whether that session exist or not. Please check and confirm

– Muthusamy
Nov 2 '18 at 14:02












7 Answers
7






active

oldest

votes


















1














Add a constructor to every applicable controller.
Let the constructor check if the user is logged in, possibly by checking if a particular session exists. If it doesn't redirect to the login page.



Something like below



   function __construct(){
parent::__construct();
if(!$this->session->userdata('userid')){
redirect('user/login');
}
}





share|improve this answer































    1














    use this at the top of the page you don't want it accessed when not logged in (supposing you've already set session data)



    <?php if (!isset($_SESSION['username'])) {
    redirect(base_url());
    } ?>





    share|improve this answer































      0














      Before showing that page, you should check whether that session exist or not, like if($this->session->userdata('username')). If exists, show that page, if not show any warning.






      share|improve this answer































        0














        You can try



        if ($this->session->userdata('login') == true) {
        redirect('controller');
        }


        Login Controller



        <?php

        class Login extends CI_Controller
        {
        public function __construct()
        {
        parent::__construct();
        $this->load->helper(array('form','url','html','security'));
        $this->load->library(array('session','form_validation'));
        $this->load->database();
        $this->load->model('user_model');
        }

        function index()
        {

        if ($this->session->userdata('login') == true) {
        redirect('controller');
        }
        //get form input
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        //form validation
        $this->form_validation->set_rules('username','username','trim|required|xss_clean');
        $this->form_validation->set_rules('password','Password','trim|required|xss_clean');

        if($this->form_validation->run() == FALSE)
        {
        //validation fail
        $this->load->view('content_login');
        }
        else
        {
        //check user credentials
        $uresult = $this->user_model->get_user($username, $password);
        if(count($uresult)>0)
        {
        //set session
        $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
        $this->session->set_userdata($sess_data);
        $this->load->library('../controllers/page');
        $this->page->profile();
        }
        else
        {
        $this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
        $this->load->library('../controllers/page');
        $this->page->login();

        }
        }
        }
        }


        Couple things I noticed also in Codeigniter 3 + versions the first letter must only be upper case on FILENAME and Controller Class



        Also you don't need to close the controllers and model with ?> https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag






        share|improve this answer































          0














          If for example you don't want the user to enter a function without login, you could check the session in the constructor of the class.



          If it's only for a specific function, you could just also check the session in the function directly.






          share|improve this answer































            0














            Create a helper file login_helper.php



            function check_login( $session ) {
            $CI =& get_instance();
            $CI->load->helper('url');
            if(!$session->userdata('login')){
            redirect(base_url());
            }

            }


            Now use this helper function in controllers where login is mandatory



            class page extends CI_Controller {

            function __construct() {
            parent::__construct();
            $this->load->model('User_model', 'user_model', TRUE);
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            $this->status = $this->config->item('status');
            $this->roles = $this->config->item('roles');
            }

            function index() {
            $this->load->view('page_header');
            $this->load->view('content_front');
            $this->load->view('page_footer');
            }


            function login() {
            $this->load->view('page_header');
            $this->load->view('content_login');
            $this->load->view('page_footer');
            }

            function register() {

            $this->load->view('page_header');
            $this->load->view('content_register');
            $this->load->view('page_footer');

            }


            function profile(){
            check_login();
            $this->load->view('page_header');
            $this->load->view('content_profile');
            $this->load->view('page_footer');

            }

            function success() {
            $this->load->view('page_header');
            $this->load->view('content_success');
            $this->load->view('page_footer');
            }

            function logout()
            {
            //destroy session
            $data = array('login' => '', 'uname' => '', 'uid' => '');
            $this->session->unset_userdata($data);
            $this->session->sess_destroy();

            redirect('page/index');
            }


            }
            ?>


            Note: add login_helper in autoload.php in config folder.



            $autoload['helper'] = array('login','url','cookie');





            share|improve this answer

































              0














              if($this->session->userdata('logged_in') == FALSE) {
              $this->session->set_flashdata('error','<p class="alert alert-danger">Please login to view this page.</p>');
              redirect('login_c');
              exit;

              }





              share|improve this answer


























              • Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                – sepehr
                Nov 13 '18 at 21:06











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42535619%2fcodeigniter-how-to-avoid-user-entering-logged-page-without-login%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Add a constructor to every applicable controller.
              Let the constructor check if the user is logged in, possibly by checking if a particular session exists. If it doesn't redirect to the login page.



              Something like below



                 function __construct(){
              parent::__construct();
              if(!$this->session->userdata('userid')){
              redirect('user/login');
              }
              }





              share|improve this answer




























                1














                Add a constructor to every applicable controller.
                Let the constructor check if the user is logged in, possibly by checking if a particular session exists. If it doesn't redirect to the login page.



                Something like below



                   function __construct(){
                parent::__construct();
                if(!$this->session->userdata('userid')){
                redirect('user/login');
                }
                }





                share|improve this answer


























                  1












                  1








                  1







                  Add a constructor to every applicable controller.
                  Let the constructor check if the user is logged in, possibly by checking if a particular session exists. If it doesn't redirect to the login page.



                  Something like below



                     function __construct(){
                  parent::__construct();
                  if(!$this->session->userdata('userid')){
                  redirect('user/login');
                  }
                  }





                  share|improve this answer













                  Add a constructor to every applicable controller.
                  Let the constructor check if the user is logged in, possibly by checking if a particular session exists. If it doesn't redirect to the login page.



                  Something like below



                     function __construct(){
                  parent::__construct();
                  if(!$this->session->userdata('userid')){
                  redirect('user/login');
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 1 '17 at 18:43









                  Ela BuwaEla Buwa

                  1,08421628




                  1,08421628

























                      1














                      use this at the top of the page you don't want it accessed when not logged in (supposing you've already set session data)



                      <?php if (!isset($_SESSION['username'])) {
                      redirect(base_url());
                      } ?>





                      share|improve this answer




























                        1














                        use this at the top of the page you don't want it accessed when not logged in (supposing you've already set session data)



                        <?php if (!isset($_SESSION['username'])) {
                        redirect(base_url());
                        } ?>





                        share|improve this answer


























                          1












                          1








                          1







                          use this at the top of the page you don't want it accessed when not logged in (supposing you've already set session data)



                          <?php if (!isset($_SESSION['username'])) {
                          redirect(base_url());
                          } ?>





                          share|improve this answer













                          use this at the top of the page you don't want it accessed when not logged in (supposing you've already set session data)



                          <?php if (!isset($_SESSION['username'])) {
                          redirect(base_url());
                          } ?>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 15 '17 at 12:12









                          stevstev

                          111




                          111























                              0














                              Before showing that page, you should check whether that session exist or not, like if($this->session->userdata('username')). If exists, show that page, if not show any warning.






                              share|improve this answer




























                                0














                                Before showing that page, you should check whether that session exist or not, like if($this->session->userdata('username')). If exists, show that page, if not show any warning.






                                share|improve this answer


























                                  0












                                  0








                                  0







                                  Before showing that page, you should check whether that session exist or not, like if($this->session->userdata('username')). If exists, show that page, if not show any warning.






                                  share|improve this answer













                                  Before showing that page, you should check whether that session exist or not, like if($this->session->userdata('username')). If exists, show that page, if not show any warning.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Mar 1 '17 at 17:29









                                  JobayerJobayer

                                  1,02111018




                                  1,02111018























                                      0














                                      You can try



                                      if ($this->session->userdata('login') == true) {
                                      redirect('controller');
                                      }


                                      Login Controller



                                      <?php

                                      class Login extends CI_Controller
                                      {
                                      public function __construct()
                                      {
                                      parent::__construct();
                                      $this->load->helper(array('form','url','html','security'));
                                      $this->load->library(array('session','form_validation'));
                                      $this->load->database();
                                      $this->load->model('user_model');
                                      }

                                      function index()
                                      {

                                      if ($this->session->userdata('login') == true) {
                                      redirect('controller');
                                      }
                                      //get form input
                                      $username = $this->input->post('username');
                                      $password = $this->input->post('password');

                                      //form validation
                                      $this->form_validation->set_rules('username','username','trim|required|xss_clean');
                                      $this->form_validation->set_rules('password','Password','trim|required|xss_clean');

                                      if($this->form_validation->run() == FALSE)
                                      {
                                      //validation fail
                                      $this->load->view('content_login');
                                      }
                                      else
                                      {
                                      //check user credentials
                                      $uresult = $this->user_model->get_user($username, $password);
                                      if(count($uresult)>0)
                                      {
                                      //set session
                                      $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
                                      $this->session->set_userdata($sess_data);
                                      $this->load->library('../controllers/page');
                                      $this->page->profile();
                                      }
                                      else
                                      {
                                      $this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
                                      $this->load->library('../controllers/page');
                                      $this->page->login();

                                      }
                                      }
                                      }
                                      }


                                      Couple things I noticed also in Codeigniter 3 + versions the first letter must only be upper case on FILENAME and Controller Class



                                      Also you don't need to close the controllers and model with ?> https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag






                                      share|improve this answer




























                                        0














                                        You can try



                                        if ($this->session->userdata('login') == true) {
                                        redirect('controller');
                                        }


                                        Login Controller



                                        <?php

                                        class Login extends CI_Controller
                                        {
                                        public function __construct()
                                        {
                                        parent::__construct();
                                        $this->load->helper(array('form','url','html','security'));
                                        $this->load->library(array('session','form_validation'));
                                        $this->load->database();
                                        $this->load->model('user_model');
                                        }

                                        function index()
                                        {

                                        if ($this->session->userdata('login') == true) {
                                        redirect('controller');
                                        }
                                        //get form input
                                        $username = $this->input->post('username');
                                        $password = $this->input->post('password');

                                        //form validation
                                        $this->form_validation->set_rules('username','username','trim|required|xss_clean');
                                        $this->form_validation->set_rules('password','Password','trim|required|xss_clean');

                                        if($this->form_validation->run() == FALSE)
                                        {
                                        //validation fail
                                        $this->load->view('content_login');
                                        }
                                        else
                                        {
                                        //check user credentials
                                        $uresult = $this->user_model->get_user($username, $password);
                                        if(count($uresult)>0)
                                        {
                                        //set session
                                        $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
                                        $this->session->set_userdata($sess_data);
                                        $this->load->library('../controllers/page');
                                        $this->page->profile();
                                        }
                                        else
                                        {
                                        $this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
                                        $this->load->library('../controllers/page');
                                        $this->page->login();

                                        }
                                        }
                                        }
                                        }


                                        Couple things I noticed also in Codeigniter 3 + versions the first letter must only be upper case on FILENAME and Controller Class



                                        Also you don't need to close the controllers and model with ?> https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag






                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          You can try



                                          if ($this->session->userdata('login') == true) {
                                          redirect('controller');
                                          }


                                          Login Controller



                                          <?php

                                          class Login extends CI_Controller
                                          {
                                          public function __construct()
                                          {
                                          parent::__construct();
                                          $this->load->helper(array('form','url','html','security'));
                                          $this->load->library(array('session','form_validation'));
                                          $this->load->database();
                                          $this->load->model('user_model');
                                          }

                                          function index()
                                          {

                                          if ($this->session->userdata('login') == true) {
                                          redirect('controller');
                                          }
                                          //get form input
                                          $username = $this->input->post('username');
                                          $password = $this->input->post('password');

                                          //form validation
                                          $this->form_validation->set_rules('username','username','trim|required|xss_clean');
                                          $this->form_validation->set_rules('password','Password','trim|required|xss_clean');

                                          if($this->form_validation->run() == FALSE)
                                          {
                                          //validation fail
                                          $this->load->view('content_login');
                                          }
                                          else
                                          {
                                          //check user credentials
                                          $uresult = $this->user_model->get_user($username, $password);
                                          if(count($uresult)>0)
                                          {
                                          //set session
                                          $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
                                          $this->session->set_userdata($sess_data);
                                          $this->load->library('../controllers/page');
                                          $this->page->profile();
                                          }
                                          else
                                          {
                                          $this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
                                          $this->load->library('../controllers/page');
                                          $this->page->login();

                                          }
                                          }
                                          }
                                          }


                                          Couple things I noticed also in Codeigniter 3 + versions the first letter must only be upper case on FILENAME and Controller Class



                                          Also you don't need to close the controllers and model with ?> https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag






                                          share|improve this answer













                                          You can try



                                          if ($this->session->userdata('login') == true) {
                                          redirect('controller');
                                          }


                                          Login Controller



                                          <?php

                                          class Login extends CI_Controller
                                          {
                                          public function __construct()
                                          {
                                          parent::__construct();
                                          $this->load->helper(array('form','url','html','security'));
                                          $this->load->library(array('session','form_validation'));
                                          $this->load->database();
                                          $this->load->model('user_model');
                                          }

                                          function index()
                                          {

                                          if ($this->session->userdata('login') == true) {
                                          redirect('controller');
                                          }
                                          //get form input
                                          $username = $this->input->post('username');
                                          $password = $this->input->post('password');

                                          //form validation
                                          $this->form_validation->set_rules('username','username','trim|required|xss_clean');
                                          $this->form_validation->set_rules('password','Password','trim|required|xss_clean');

                                          if($this->form_validation->run() == FALSE)
                                          {
                                          //validation fail
                                          $this->load->view('content_login');
                                          }
                                          else
                                          {
                                          //check user credentials
                                          $uresult = $this->user_model->get_user($username, $password);
                                          if(count($uresult)>0)
                                          {
                                          //set session
                                          $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
                                          $this->session->set_userdata($sess_data);
                                          $this->load->library('../controllers/page');
                                          $this->page->profile();
                                          }
                                          else
                                          {
                                          $this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
                                          $this->load->library('../controllers/page');
                                          $this->page->login();

                                          }
                                          }
                                          }
                                          }


                                          Couple things I noticed also in Codeigniter 3 + versions the first letter must only be upper case on FILENAME and Controller Class



                                          Also you don't need to close the controllers and model with ?> https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Mar 1 '17 at 18:24









                                          Mr. EDMr. ED

                                          8,63763679




                                          8,63763679























                                              0














                                              If for example you don't want the user to enter a function without login, you could check the session in the constructor of the class.



                                              If it's only for a specific function, you could just also check the session in the function directly.






                                              share|improve this answer




























                                                0














                                                If for example you don't want the user to enter a function without login, you could check the session in the constructor of the class.



                                                If it's only for a specific function, you could just also check the session in the function directly.






                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  If for example you don't want the user to enter a function without login, you could check the session in the constructor of the class.



                                                  If it's only for a specific function, you could just also check the session in the function directly.






                                                  share|improve this answer













                                                  If for example you don't want the user to enter a function without login, you could check the session in the constructor of the class.



                                                  If it's only for a specific function, you could just also check the session in the function directly.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Mar 2 '17 at 7:18









                                                  harritbalharritbal

                                                  11




                                                  11























                                                      0














                                                      Create a helper file login_helper.php



                                                      function check_login( $session ) {
                                                      $CI =& get_instance();
                                                      $CI->load->helper('url');
                                                      if(!$session->userdata('login')){
                                                      redirect(base_url());
                                                      }

                                                      }


                                                      Now use this helper function in controllers where login is mandatory



                                                      class page extends CI_Controller {

                                                      function __construct() {
                                                      parent::__construct();
                                                      $this->load->model('User_model', 'user_model', TRUE);
                                                      $this->load->library('form_validation');
                                                      $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
                                                      $this->status = $this->config->item('status');
                                                      $this->roles = $this->config->item('roles');
                                                      }

                                                      function index() {
                                                      $this->load->view('page_header');
                                                      $this->load->view('content_front');
                                                      $this->load->view('page_footer');
                                                      }


                                                      function login() {
                                                      $this->load->view('page_header');
                                                      $this->load->view('content_login');
                                                      $this->load->view('page_footer');
                                                      }

                                                      function register() {

                                                      $this->load->view('page_header');
                                                      $this->load->view('content_register');
                                                      $this->load->view('page_footer');

                                                      }


                                                      function profile(){
                                                      check_login();
                                                      $this->load->view('page_header');
                                                      $this->load->view('content_profile');
                                                      $this->load->view('page_footer');

                                                      }

                                                      function success() {
                                                      $this->load->view('page_header');
                                                      $this->load->view('content_success');
                                                      $this->load->view('page_footer');
                                                      }

                                                      function logout()
                                                      {
                                                      //destroy session
                                                      $data = array('login' => '', 'uname' => '', 'uid' => '');
                                                      $this->session->unset_userdata($data);
                                                      $this->session->sess_destroy();

                                                      redirect('page/index');
                                                      }


                                                      }
                                                      ?>


                                                      Note: add login_helper in autoload.php in config folder.



                                                      $autoload['helper'] = array('login','url','cookie');





                                                      share|improve this answer






























                                                        0














                                                        Create a helper file login_helper.php



                                                        function check_login( $session ) {
                                                        $CI =& get_instance();
                                                        $CI->load->helper('url');
                                                        if(!$session->userdata('login')){
                                                        redirect(base_url());
                                                        }

                                                        }


                                                        Now use this helper function in controllers where login is mandatory



                                                        class page extends CI_Controller {

                                                        function __construct() {
                                                        parent::__construct();
                                                        $this->load->model('User_model', 'user_model', TRUE);
                                                        $this->load->library('form_validation');
                                                        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
                                                        $this->status = $this->config->item('status');
                                                        $this->roles = $this->config->item('roles');
                                                        }

                                                        function index() {
                                                        $this->load->view('page_header');
                                                        $this->load->view('content_front');
                                                        $this->load->view('page_footer');
                                                        }


                                                        function login() {
                                                        $this->load->view('page_header');
                                                        $this->load->view('content_login');
                                                        $this->load->view('page_footer');
                                                        }

                                                        function register() {

                                                        $this->load->view('page_header');
                                                        $this->load->view('content_register');
                                                        $this->load->view('page_footer');

                                                        }


                                                        function profile(){
                                                        check_login();
                                                        $this->load->view('page_header');
                                                        $this->load->view('content_profile');
                                                        $this->load->view('page_footer');

                                                        }

                                                        function success() {
                                                        $this->load->view('page_header');
                                                        $this->load->view('content_success');
                                                        $this->load->view('page_footer');
                                                        }

                                                        function logout()
                                                        {
                                                        //destroy session
                                                        $data = array('login' => '', 'uname' => '', 'uid' => '');
                                                        $this->session->unset_userdata($data);
                                                        $this->session->sess_destroy();

                                                        redirect('page/index');
                                                        }


                                                        }
                                                        ?>


                                                        Note: add login_helper in autoload.php in config folder.



                                                        $autoload['helper'] = array('login','url','cookie');





                                                        share|improve this answer




























                                                          0












                                                          0








                                                          0







                                                          Create a helper file login_helper.php



                                                          function check_login( $session ) {
                                                          $CI =& get_instance();
                                                          $CI->load->helper('url');
                                                          if(!$session->userdata('login')){
                                                          redirect(base_url());
                                                          }

                                                          }


                                                          Now use this helper function in controllers where login is mandatory



                                                          class page extends CI_Controller {

                                                          function __construct() {
                                                          parent::__construct();
                                                          $this->load->model('User_model', 'user_model', TRUE);
                                                          $this->load->library('form_validation');
                                                          $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
                                                          $this->status = $this->config->item('status');
                                                          $this->roles = $this->config->item('roles');
                                                          }

                                                          function index() {
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_front');
                                                          $this->load->view('page_footer');
                                                          }


                                                          function login() {
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_login');
                                                          $this->load->view('page_footer');
                                                          }

                                                          function register() {

                                                          $this->load->view('page_header');
                                                          $this->load->view('content_register');
                                                          $this->load->view('page_footer');

                                                          }


                                                          function profile(){
                                                          check_login();
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_profile');
                                                          $this->load->view('page_footer');

                                                          }

                                                          function success() {
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_success');
                                                          $this->load->view('page_footer');
                                                          }

                                                          function logout()
                                                          {
                                                          //destroy session
                                                          $data = array('login' => '', 'uname' => '', 'uid' => '');
                                                          $this->session->unset_userdata($data);
                                                          $this->session->sess_destroy();

                                                          redirect('page/index');
                                                          }


                                                          }
                                                          ?>


                                                          Note: add login_helper in autoload.php in config folder.



                                                          $autoload['helper'] = array('login','url','cookie');





                                                          share|improve this answer















                                                          Create a helper file login_helper.php



                                                          function check_login( $session ) {
                                                          $CI =& get_instance();
                                                          $CI->load->helper('url');
                                                          if(!$session->userdata('login')){
                                                          redirect(base_url());
                                                          }

                                                          }


                                                          Now use this helper function in controllers where login is mandatory



                                                          class page extends CI_Controller {

                                                          function __construct() {
                                                          parent::__construct();
                                                          $this->load->model('User_model', 'user_model', TRUE);
                                                          $this->load->library('form_validation');
                                                          $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
                                                          $this->status = $this->config->item('status');
                                                          $this->roles = $this->config->item('roles');
                                                          }

                                                          function index() {
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_front');
                                                          $this->load->view('page_footer');
                                                          }


                                                          function login() {
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_login');
                                                          $this->load->view('page_footer');
                                                          }

                                                          function register() {

                                                          $this->load->view('page_header');
                                                          $this->load->view('content_register');
                                                          $this->load->view('page_footer');

                                                          }


                                                          function profile(){
                                                          check_login();
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_profile');
                                                          $this->load->view('page_footer');

                                                          }

                                                          function success() {
                                                          $this->load->view('page_header');
                                                          $this->load->view('content_success');
                                                          $this->load->view('page_footer');
                                                          }

                                                          function logout()
                                                          {
                                                          //destroy session
                                                          $data = array('login' => '', 'uname' => '', 'uid' => '');
                                                          $this->session->unset_userdata($data);
                                                          $this->session->sess_destroy();

                                                          redirect('page/index');
                                                          }


                                                          }
                                                          ?>


                                                          Note: add login_helper in autoload.php in config folder.



                                                          $autoload['helper'] = array('login','url','cookie');






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Nov 2 '18 at 13:43









                                                          Pragnesh Chauhan

                                                          6,65993049




                                                          6,65993049










                                                          answered Mar 2 '17 at 7:45









                                                          Pradeep SInghPradeep SIngh

                                                          14




                                                          14























                                                              0














                                                              if($this->session->userdata('logged_in') == FALSE) {
                                                              $this->session->set_flashdata('error','<p class="alert alert-danger">Please login to view this page.</p>');
                                                              redirect('login_c');
                                                              exit;

                                                              }





                                                              share|improve this answer


























                                                              • Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                                                                – sepehr
                                                                Nov 13 '18 at 21:06
















                                                              0














                                                              if($this->session->userdata('logged_in') == FALSE) {
                                                              $this->session->set_flashdata('error','<p class="alert alert-danger">Please login to view this page.</p>');
                                                              redirect('login_c');
                                                              exit;

                                                              }





                                                              share|improve this answer


























                                                              • Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                                                                – sepehr
                                                                Nov 13 '18 at 21:06














                                                              0












                                                              0








                                                              0







                                                              if($this->session->userdata('logged_in') == FALSE) {
                                                              $this->session->set_flashdata('error','<p class="alert alert-danger">Please login to view this page.</p>');
                                                              redirect('login_c');
                                                              exit;

                                                              }





                                                              share|improve this answer















                                                              if($this->session->userdata('logged_in') == FALSE) {
                                                              $this->session->set_flashdata('error','<p class="alert alert-danger">Please login to view this page.</p>');
                                                              redirect('login_c');
                                                              exit;

                                                              }






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Nov 13 '18 at 18:01









                                                              yesitsme

                                                              1,204824




                                                              1,204824










                                                              answered Nov 13 '18 at 14:33









                                                              mixture333mixture333

                                                              1




                                                              1













                                                              • Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                                                                – sepehr
                                                                Nov 13 '18 at 21:06



















                                                              • Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                                                                – sepehr
                                                                Nov 13 '18 at 21:06

















                                                              Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                                                              – sepehr
                                                              Nov 13 '18 at 21:06





                                                              Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

                                                              – sepehr
                                                              Nov 13 '18 at 21:06


















                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42535619%2fcodeigniter-how-to-avoid-user-entering-logged-page-without-login%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              這個網誌中的熱門文章

                                                              Tangent Lines Diagram Along Smooth Curve

                                                              Yusuf al-Mu'taman ibn Hud

                                                              Zucchini