Welcome Guest, Not a member yet? Register   Sign In
Can't get user id from session
#1

Hello, so i'm trying to insert the user id to a table named "services", but it seems that session('id') isn't working, but when i'm tring to insert it in "images" table, it's working fine, can any one help me with that ?

Link to my controller : https://pastebin.com/5dLDUQMc
Reply
#2

When I gave a look at your codes I could'nt see that you started a session first.

When the user log in, a session should start first. Then you can call any information related to it.

or, if your url contains an id, you can get it via :

PHP Code:
$id $this->request->uri->getSegment(2); 

*2 can be 1 or 3. Depends on your url
Reply
#3

(05-13-2021, 09:29 AM)demyr Wrote: When I gave a look at your codes I could'nt see that you started a session first.

When the user log in, a session should start first. Then you can call any information related to it.

or, if your url contains an id, you can get it via :

PHP Code:
$id $this->request->uri->getSegment(2); 

*2 can be 1 or 3. Depends on your url
Hello, thank you for your reply, si i started a session when the user logs in, in the "login" controller
here the link to that : https://pastebin.com/1SXynPBF
Reply
#4

(This post was last modified: 01-20-2022, 12:08 PM by demyr.)

Hi again,

Your codes don't look healthy. For example, you are trying to start your session with an email, not with a password match. And your password area doesn't have any encryption. I am leaving my codes below:

PHP Code:
public function login_process (){

 if(
$this->request->getMethod() == 'post'){
 
 
$user_email strip_tags(strtolower($this->request->getVar('user_email')));
$user_password strip_tags($this->request->getVar('user_password'));

 
  $account $this->UserModel->user_control($user_email);

 
  if(!$account){
 
  $session = \Config\Services::session();
 
$session->setFlashdata('error''No such user in our database');
 return 
redirect()->to(site_url('/login-page')); // redirects back to user login page
    }else{
 
$hash $account->user_password;
 
$user_name $account->user_name;
 
$user_id $account->user_id;

 if(
password_verify($this->request->getVar('user_password'),$hash)){

            $start_session = [
                 'user_email' => $user_email,
                 'user_password' => $user_password,
                 'user_name' => $user_name,
                 'user_id' => $user_id
               
];

      $session = \Config\Services::session();
      $session->set($start_session);

 
  return redirect()->to(site_url('user-area')); // if everything is OK, redirects to this page

        }else{
          $session = \Config\Services::session();
 
  $session->setFlashdata('error''Wrong Password');
 
  return redirect()->to(site_url('login-page')); //if the password is wrong, redirects to login page again
 
        }

     }

   }



Within your other methods in controller, just follow this :

PHP Code:
   public function another_method(){

        $session = \Config\Services::session();
 
$info $session->user_email;
 
$user_id $session->user_id;

 if(
$info !=''){
          // if session matches your email as logged in, do your stuf here
          
          $data
['user'] = $this->UserModel->user_control($info);

       //now you can call your user_id anywhere in your view : $user->user_id will give your result

       }else{
          // if no user email in session

         $session->setFlashdata('error''Please sign in first');
         return redirect()->to(site_url('user-login'));
        }
}
//ends here 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB