user7978382
user7978382

Reputation:

Issue with CodeIgniter redirect

Here i'm creating a login system. Once the user is verified the controller redirects the page to home.php.

There are 2 pages here login_registration.php and home.php

login_registration.php consists login and registration form from here the user is redirected to home.php. both the files are in path C:\wamp\www\CI-Project_1\application\views\login

The issue is it show 404 Page Not Found when redirected.

 class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
                $this->load->helper('url');
        //LOAD MODELS HERE
        $this->load->model('Login_model');
    }

    public function index(){
        $this->load->view('login/login_registration.php');
    }

    function login_user(){
        $login_btn = $this->input->post('login_btn');

        if($login_btn == TRUE){
            $arr['login'] = array(
                'username'=>$this->input->post('username'),
                'password'=>$this->input->post('password')
            );

            $query = $this->Login_model->users_login($arr);

            if($query->num_rows() > 0 && $query->num_rows() == 1){
                $this->load->library('session');
                $this->session->set_userdata($arr['login']);
                redirect('login/home.php', 'location');
            }
            else
            {
                $this->index();
            }
        }
        else
        {
            $this->index();
        }
    }
}

THANKS IN ADVANCE

Upvotes: 1

Views: 278

Answers (6)

Rachit
Rachit

Reputation: 248

For redirect and $this->load->view, don't use .php.

Use something like this: redirect('login/home', 'location');

Upvotes: 0

Muhammad Usman
Muhammad Usman

Reputation: 1423

You have to mention the name of your controller and method name where you want to redirect something like this

redirect('controllername/methodname');

Upvotes: 1

user7596840
user7596840

Reputation: 137

To redirect on any function in CI can use redirect('controller n_name/function_name','refresh');

Upvotes: 0

rowmoin
rowmoin

Reputation: 708

Why you going to redirect if you want to call any method from a controller then you can redirect like that:

public function home(){
    $this->load->view('login/home.php');
}

redirect('login/home', 'location');

But I think you don't need to redirect you can simply load view file just like you already load your registration view. So you just load view Instead of redirect..

$this->load->view('login/home.php');

Upvotes: 0

Muhammad Furqan Ul Haq
Muhammad Furqan Ul Haq

Reputation: 170

According to codeigniter views documentation

The .php file extension does not need to be specified unless you use something other than .php.


So, the correct code is as follow:

$this->load->view('login/login_registration');


Also you are not using the redirect function correctly, as i don't see any home function inside login controller. If you somehow have home function inside login controller then why are you using .php after function name?


Check the codeigniter redirect documentation for more info.

Upvotes: 2

Naga
Naga

Reputation: 2168

Try this,

class Login extends CI_Controller {
public function __construct(){
    parent::__construct();
            $this->load->helper('url');
    //LOAD MODELS HERE
    $this->load->model('Login_model');
}

public function index(){
    $this->load->view('login/login_registration.php');
}

public function home(){
    $this->load->view('login/home.php');
}

function login_user(){
    $login_btn = $this->input->post('login_btn');

    if($login_btn == TRUE){
        $arr['login'] = array(
            'username'=>$this->input->post('username'),
            'password'=>$this->input->post('password')
        );

        $query = $this->Login_model->users_login($arr);

        if($query->num_rows() > 0 && $query->num_rows() == 1){
            $this->load->library('session');
            $this->session->set_userdata($arr['login']);
            redirect('login/home', 'location');
        }
        else
        {
            $this->index();
        }
    }
    else
    {
        $this->index();
    }
}
}

Upvotes: 0

Related Questions