0

This is my Users model

public function user_login($email, $password)
{
    $this->load->library('session');
    $this->db->where('email', $email);
    $this->db->where('password', $password);
    $query = $this->db->get('users');
    if($query->num_rows() > 0) {
        foreach($query->result() as $row) {
            $data = array(
                'id' => $row->id,
                'username' => $row->username,
                'email' => $row->email
            );
            $this->session->set_userdata($data);
        }
        return 1;
    } else {
        return false;
    }
}

On my view i have a div to display the session username of the user

<div>
    <?php echo $this->session->userdata('username'); ?>
</div>

It's does not appear What could be wrong?


When I print the contents of userdata() like this

<?php print_r($this->session->userdata()); ?>

I get the response

Array ( [__ci_last_regenerate] => 1502894606 )
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
Lestah
  • 63
  • 1
  • 2
  • 9
  • 1
    What is your output if you do `session->userdata()); ?>`? – GrumpyCrouton Aug 16 '17 at 14:41
  • check the session data $this->session->has_userdata('username'); Also initialize the Session class manually in your controller. – Ravinder Reddy Aug 16 '17 at 14:43
  • @GrumpyCrouton Array ( [__ci_last_regenerate] => 1502894606 ) 1 thats what i get when i print_r – Lestah Aug 16 '17 at 14:46
  • @raving Reddy i loaded the library session il try has_userdata – Lestah Aug 16 '17 at 14:46
  • **Never store plain text passwords!** Please use **PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)** (`password_hash()` and `password_verify()`)to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). **It is not necessary** to [escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so _changes_ the password and causes unnecessary additional coding. – GrumpyCrouton Aug 16 '17 at 14:48
  • It looks like your session data is not being set for some reason. – GrumpyCrouton Aug 16 '17 at 14:50
  • That's what i thought is there any other way to set a session other than the way i used to write it – Lestah Aug 16 '17 at 14:54
  • @Lestah I'm not familiar enough with codeigniter to help you unfortunately. From the docs it looks ike you are doing it correctly. – GrumpyCrouton Aug 16 '17 at 14:59
  • Why you do not try to send instead of `$data` just one atributte **username** for example? – Mary Aug 16 '17 at 17:03

3 Answers3

0

simplify your query some

$data = $this->db->where(['email'=>$email, 'password'=>$password])->get('users')->row_array();
if($data){
    $_SESSION['id'] = $data['id'];
    $_SESSION['username'] = $data['username'];
    $_SESSION['email'] = $data['email'];
    return TRUE;
}
else{
   return FALSE;
}

in view:

<div>
     <?php echo $_SESSION['username']; ?>
</div>
Parker Dell
  • 472
  • 4
  • 11
-1

try to print it out like this echo $data['username'] where $data is whatever variable you are sending to the view from the controller

brettywhite
  • 521
  • 4
  • 14
-1

If you actually have a username at line with

$row->username

Then right after that array, add

$this->load->vars( array('username' => $row->username) );

And in your view you would be able to get username

echo $username;

This is assuming you are not redirecting after setting your session.

Brian Gottier
  • 4,522
  • 3
  • 21
  • 37
  • I'm sure OP wants to use the variable again in other views, hence using sessions. – GrumpyCrouton Aug 16 '17 at 14:49
  • I never said not to use sessions. I just said this is a way to get the username to the view immediately. No part of what I said recommended removal of existing code. – Brian Gottier Aug 16 '17 at 14:51
  • Then this does not answer the question. – GrumpyCrouton Aug 16 '17 at 14:57
  • Here you are babbling about sessions not being set for some reason, and you think I'm wrong about this? You admit that you're not familiar with CodeIgniter, but I'm wrong? ... and you downvote me? – Brian Gottier Aug 16 '17 at 15:01
  • I also didn't post an answer that doesn't attempt to solve the issue. I never said you were wrong, I said your answer doesn't answer the question, therefore it's "not useful" as the downvote tooltip says. – GrumpyCrouton Aug 16 '17 at 15:02
  • And there's no way you could, because you don't know what you're doing. – Brian Gottier Aug 16 '17 at 15:03
  • Exactly why I didn't :) However, if you "know what you are doing" more than me, you can attempt to actually answer the question and I will retract my vote. Just because I don't know about codeigniter (As I have never personally used it), does not make my downvote less valid. – GrumpyCrouton Aug 16 '17 at 15:03