Azure1015 Azure1015 - 4 months ago 92
PHP Question

CodeIgniter 3.0.6 Redirect function does not work correctly

I am an absolute beginner of web development. I am using CodeIgniter framework right now.

https://www.youtube.com/watch?v=5Xjme7Mw3UM&list=PLillGF-RfqbZQw-pSO62ha_imR_848j_X&index=6

I have been following the tutorial above so far. When watching part 6, I have an issue with redirect function in the login function below.
The redirect function should load to index/home, but it actually does not. It goes to user/login and the application says that the requested page does not exist.

Has anyone ever encountered a similar problem below? If you have, please leave your comments!
English is not my first language, so if this post does not make sense or you need more information, please let me know as well.
Any advice would be appreciated! Thanks in advance!

User.php

public function login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[50]');

if($this->form_validation->run() == FALSE){
// nothing
} else{
// get from post
$username = $this->input->post('username');
$password = $this->input->post('password');

// get user id from the model
$user_id = $this->User_model->login_user($username, $password);

// validate user
if($user_id){
// create array of user data
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);

// set session data
$this->session->set_userdata($user_data);

$this->session->set_flashdata('login_success', 'You are now logged in');

redirect('home/index');
}else{
// set error
$this->session->set_flashdata('login_failed', 'Sorry the login info that you entered is invalid');
redirect('home/index');
}
}

}

Answer

try adding "/" before the url

redirect('/home/index');