Natty Guurl
Natty Guurl

Reputation: 125

codeigniter: base_url part won't redirect to my controller

I am trying to make a log in work. I am all good with the verification but is stuck when it redirects to an another controller to show the view of the logged in page. I am still new to codeigniter and is still not sure about how controllers work.

This is my controller for verifying logged in users:

 function index() {
        $this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('v_login');
            } else {
                //Go to private area
                redirect(base_url('c_home'), 'refresh');
            }       
     }

Its function is to validates the user if its in the database, however when the user is successfully logged in, it won't redirect to this redirect(base_url('c_home'), 'refresh'); It tells me that

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

This is the c_home.php where it is supposed to be redirected:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_home extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('m_login','',TRUE);
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
    }

    function index() {
        if($this->session->userdata('logged_in'))
        {
            $session_data = $this->session->userdata('logged_in');
            $data['studentid'] = $session_data['studentid'];
            $this->load->view('v_home', $data);
        } else {
        //If no session, redirect to login page
            redirect('c_login', 'refresh');
        }
    }

    function logout() {
         //remove all session data
         $this->session->unset_userdata('logged_in');
         $this->session->sess_destroy();
         redirect(base_url('c_login'), 'refresh');
     }

}

is it okay to redirect a controller from another controller? after it redicts to c_home.php

it will show the v_home.php

<!DOCTYPE html>
 <head>
   <title>Simple Login with CodeIgniter - Private Area</title>
 </head>
 <body>
   <h1>Home</h1>
   <h2>Welcome <?php echo $studentid; ?>!</h2>
   <a href="c_home/logout">Logout</a>
 </body>
</html>

Upvotes: 1

Views: 20031

Answers (2)

user3940738
user3940738

Reputation:

You don't need the base_url() in the redirect. just use

redirect('c_home',refresh);

Although there are 2 things i should make you aware of, you need ('controller/function') not just ('controller'). And also make sure you're loading $this->load->helper('url') in your __construct function on both controllers.

Although just for future reference, i think this is what you meant. redirect(base_url().'c_home',refresh)

Upvotes: 3

Craig
Craig

Reputation: 1823

Codeigniter's redirect function already has the site_url() embedded.

So, instead of this;

redirect(base_url('c_login'), 'refresh');

Use this, as you have earlier in your code

redirect('c_login', 'refresh');

Hope this helps

Upvotes: 1

Related Questions