Welcome Guest, Not a member yet? Register   Sign In
How to set Session in Codeigniter?
#1

[eluser]tahakirmani[/eluser]
Hi

I am trying to create session in my login form. When a user opens my website Login Page appers, after login user can access to the dashboard. I want the user to land on Dashboard page if he is already login. Right now its not storing session information. I have autoload session and have written the following code. Kindly check it and guide me how to resolve this problem.



//Controller

Code:
public function index()
    {            
       $data['products_data']= $this->products_model->all_products();
      
       if (!$this->session->userdata('username'))
            
       {
           $this->load->view('login');
       }
       else
       {
            $this->form_validation->set_rules('username','Username','required');
            $this->form_validation->set_rules('password','Password','required');
            
            if ($this->form_validation->run()== false)
            {
                $this->load->view('login');
            }
            else{
                
                        $username=   $this->input->post('username');
                        $password=   $this->input->post('password');  
                
                if(!$this->products_model->login_model($username,$password))
                {
                    $this->load->view('login');
                    echo "Invalid Username or Password";
                }
                
                else
                {
                    $sess_data= array(
                                    'username'=> $username,
                                    'login_status'=>'1'
                );
                    
                   $session_data   =   $this->session->set_userdata($sess_data);
                
                    
                    $this->load->view('header');
                    $this->load->view('dashboard');
                    $this->load->view('footer');
                    }
            }          

       }

    }
#2

[eluser]tahakirmani[/eluser]
Anyone for the help?
#3

[eluser]Kenan[/eluser]
try to add this code after set_userdata($sess_data):

Code:
$this->session->all_userdata()
#4

[eluser]tahakirmani[/eluser]
[quote author="Kenan" date="1390375805"]try to add this code after set_userdata($sess_data):

Code:
$this->session->all_userdata()
[/quote]

Thanks for the reply.
I have added these lines in my code, but still there is no change in the output.
#5

[eluser]Kenan[/eluser]
never mind, then u have to wait for the others' help, i am new in ci, i can give u workfull code in classic php (i think u also know it) but in ci my knowledge is not enough
#6

[eluser]tahakirmani[/eluser]
[quote author="Kenan" date="1390384993"]never mind, then u have to wait for the others' help, i am new in ci, i can give u workfull code in classic php (i think u also know it) but in ci my knowledge is not enough[/quote]

Yes i know how to set and test session in classic php, but in codeigniter i am unable to understand that how to test if session is already set or not.
#7

[eluser]Kenan[/eluser]
[quote author="tahakirmani" date="1390385580"][quote author="Kenan" date="1390384993"]never mind, then u have to wait for the others' help, i am new in ci, i can give u workfull code in classic php (i think u also know it) but in ci my knowledge is not enough[/quote]

Yes i know how to set and test session in classic php, but in codeigniter i am unable to understand that how to test if session is already set or not. [/quote]

well, so, i am rewriting my apps from the classic php into the ci, when i'll reach sessions (i seem it'll be today or tomorrow) and it'll work, i'll give my example
#8

[eluser]tahakirmani[/eluser]
[quote author="Kenan" date="1390386808"][quote author="tahakirmani" date="1390385580"][quote author="Kenan" date="1390384993"]never mind, then u have to wait for the others' help, i am new in ci, i can give u workfull code in classic php (i think u also know it) but in ci my knowledge is not enough[/quote]

Yes i know how to set and test session in classic php, but in codeigniter i am unable to understand that how to test if session is already set or not. [/quote]

well, so, i am rewriting my apps from the classic php into the ci, when i'll reach sessions (i seem it'll be today or tomorrow) and it'll work, i'll give my example[/quote]

Ok, Thanks Smile
#9

[eluser]Kenan[/eluser]
[quote author="tahakirmani" date="1390387106"][quote author="Kenan" date="1390386808"][quote author="tahakirmani" date="1390385580"][quote author="Kenan" date="1390384993"]never mind, then u have to wait for the others' help, i am new in ci, i can give u workfull code in classic php (i think u also know it) but in ci my knowledge is not enough[/quote]

Yes i know how to set and test session in classic php, but in codeigniter i am unable to understand that how to test if session is already set or not. [/quote]

well, so, i am rewriting my apps from the classic php into the ci, when i'll reach sessions (i seem it'll be today or tomorrow) and it'll work, i'll give my example[/quote]

Ok, Thanks Smile[/quote]

never mind Wink
#10

[eluser]Kenan[/eluser]
dear friend, i have realized session storing in ci, and it works

Code:
$data = array(
  'id' => $data['id'],
  'email' => $data['email'],
  'login' => $auth
);
$this->session->set_userdata($data);

i check it using it:

Code:
$this->session->userdata('id');
$this->session->userdata('email');
$this->session->userdata('login');




Theme © iAndrew 2016 - Forum software by © MyBB