working with CI sessions

#1
[eluser]sasori[/eluser]
hi this is my first time to try sessions of CI and im in trouble LOL
here's my controller code

Code:
public function login()
  {
    $username = $_POST['username'];
    $password = $_POST['passwd'];
    $data['username'] = $this->session->set_userdata('username',$username);
    $this->site_model->login($username,$password);
    $this->load->view('member_view',$data);
  }

model
Code:
public function login($username,$password)
    {
        $this->db->where('username',$username);
        $this->db->where('passwd',$password);
        $result = $this->db->get('user');
        return $result;
    }

view

Code:
<?php

    if(isset($_SESSION['username'])){
      echo "you are logged in as .". $_SESSION['username'];
    }else{
      echo "you aren't logged in";
    }
?>

when i try to log-in with a correct data, I'm just getting the
"you aren't logged in"

#2
[eluser]guidorossi[/eluser]
2 things...

1- are you loading the session library?

2- I think
Code:
isset($_SESSION['username']
should be
Code:
isset($this->session->userdata('username'))
and then

Code:
echo "you are logged in as .". $_SESSION['username'];
should be
Code:
echo "you are logged in as .". $this->session->userdata('username');

#3
[eluser]sasori[/eluser]
ok i tried what you said, now am getting this error
Code:
Fatal error: Can't use method return value in write context in C:\xampp\htdocs\myapp\application\views\member_view.php on line 4

where line 4 is the
Code:
if(isset($this->session->userdata('username'))){

(btw the session class is autoloaded from the config)

#4
[eluser]guidorossi[/eluser]
Sorry about that, it should be
Code:
<?php
   $username = $this->session->userdata('username');
    if(isset($username)){
      echo "you are logged in as .". $username;
    }else{
      echo "you aren't logged in";
    }
?>

but I suggest you to take a look at this video tutorial from nettuts to get some good practices because I think your code have some security issues like setting the cookie data before calling the login model....

http://net.tutsplus.com/videos/screencas...y-6-login/

#5
[eluser]sasori[/eluser]
lol , too late, I was already downloading it since my last reply from this thread.
but anyway. thanks for the help.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.