always-a-learner
always-a-learner

Reputation: 3794

calling function inside function with same class and redirect in codeigniter

I am working on login and search functionality using CodeIgniter.

What Exactly I am doing?

When the user searches something in the search form and on submitting that form I put a check if the user is logged in then it will directly show the result & if the user is not logged in then it redirects the user to the login page and I saved the search data in session as search_session_data.

I have a controller which redirects user regarding the session. If the user came directly to the login page then the user is redirected to the dashboard after login. but if the user is coming from search page then the user is redirected to the search result page after login.

I have mentioned the problem inside the code with a comment.

This is the controller:

public function get_the_search_result() {

        $search_session_data = $this->session->userdata('search_session_data');
        $this->load->model ('my_model');
        $result_data = $this->my_model->find_data_regarding_search_criteria($search_session_data);

        $this->load->view ('app/app_statch_result',['result_data' => $result_data,]);
}


public function login_function() {
    //necessary  login code

    if ($this->session->userdata('search_session_data')) {

        //Here I want the if `search_session_data` available in session then 
        // user goest to `get_the_search_result` and view the
         //'app/app_statch_result` but it is not working.

        $this->get_the_search_result();
    } else {
        return redirect('dashboard');
    }

}

So How do I redirect the user to app_statch_result from login_function function?

Any suggestion regarding improvement is applicable. Thanks

Upvotes: 0

Views: 1150

Answers (1)

If I were you, I would do the following:

  1. Create login controller, or better, library and put all login logic inside that class. Then you can load that library from any controller... For example:

Sample library

    class User_controll{
        private $CI;

        public function __construct() {
            $this->CI = &get_instance();
        }

        public function check_user_data($username, $password) {
            $tmpId;
            // To increase security you can encrypt password
            $user = $this->CI->db->from('users')->where(array('username' => $username, 'password' => $password))->get()->result();

            if(count($user) == 1){
                $this->CI->session->set_userdata('loggedIn', 1);
                redirect('user_page');
            }
        }
        protected function logout() {
            $this->CI->session->set_userdata('loggedIn', 0);
        }

        public function isLoggedIn() {
            return $this->CI->session->userdata('loggedIn') == 1;
        }

    }

Sample Controller

    class User_page extends CI_Controller{

        public function __construct() {
            parent::__construct();
            $this->load->library('User_controll');
        }
        public function index() {
            if(!$this->user_controll->isLoggedIn()){
                redirect('user_page/loginForm');
            }
            else {
                // do whatever you want
            }
        }
    }

Upvotes: 0

Related Questions