CodeIgniter Forums
working with CI sessions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: working with CI sessions (/showthread.php?tid=26892)



working with CI sessions - El Forum - 01-26-2010

[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"


working with CI sessions - El Forum - 01-26-2010

[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');



working with CI sessions - El Forum - 01-26-2010

[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)


working with CI sessions - El Forum - 01-26-2010

[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/screencasts/codeigniter-from-scratch-day-6-login/


working with CI sessions - El Forum - 01-26-2010

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