I am learning codeigniter. I am trying to code login system and after perfect login I want to redirect the page to another controller. After redirect, I can’t get user session data. However when i manually go that page i can get the session data. Is there a solution?
what i have done for session:
$this->session->set_userdata('userid', $user->user_id); //in login controller
echo "userid: ".$this->session->userdata('userid'); // in profile controller
codigniter version : 3.1.11
php version : 7.3.7
Note: Ive tried all the solution in the internet about "session.php"
EDIT:
class Profilim extends CI_Controller
{
public function index()
{
echo "profilim userid: ".$this->session->userdata('userid');
}
}
class Giris extends CI_Controller
{
public function index()
{
$this->load->model("GirisModel");
if($this->input->post('giris')){
$data["loginmessage"] = "";
$data["login_status"] = false;
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->GirisModel->isUserExist($email,$password);
if($result->num_rows()){
$data["loginmessage"]="Giriş Başarılı!";
$data["login_status"]=true;
$user = $result->row();
$this->session->set_userdata('userid', $user->user_id);
/*Redirect the user to some internal controller’s method*/
redirect('Profilim','refresh');
}
else{
$data["loginmessage"]="Yanlış şifre veya kullanıcı adı!";
$data["login_status"]=false;
}
$this->load->view("giris",$data);
}else{
$this->load->view("giris");
}
}
}
2
Answers
I found a solution:
If you did not set base_url in the config.php file it redirects page to http://[::1]/something so it deletes session if you change your base url to localhost it works
You can try this (after backing up the named file): https://ganganga.in/2019/09/17/codeigniter-3-x-x-and-php-7-x-x-session-lost-while-redirect/
Codeigniter 3 based on PHP 5 but works to a limited extent with PHP 7.