Welcome Guest, Not a member yet? Register   Sign In
Userdata and session data into database
#1

[eluser]someone Smile[/eluser]
Hello,

How can I get some data from userdata, to show it on site?
I'm trying to store it in userdata with set_userdata() from database, but I always get error "Object of class stdClass could not be converted to string".

Controller:
Code:
$checklogin = $this->Userdat->check_login($this->input->post('username', TRUE), crypt($this->input->post('password', TRUE), 'somehash'));
if ($checklogin)
{
$this->session->set_userdata(array(
  'logged_in' => TRUE,
  'id_user' => $checklogin->row('id_user'),
  'username' => $checklogin->row('username')
));
redirect('user');
}

Model:
Code:
public function checklogin($username, $password)
{
$checklogin = $this->db->select('id_user', 'username')->where(array('username' => $username, 'password' => $password))->get('users');
if ($checklogin->num_rows() == 1)
{
  return $checklogin;
}
else
{
  return FALSE;
}
}

And then if I want to show some data (just for testing) in controller I use: $this->userdata('username'); but there is problem with error which I mentioned before.

Second question is about storing session data into database. Is this good and can I change name of table and fields into my language? How to use this data then and when I can delete it or how to delete it if it's old?

Thanks in advance! :-)
#2

[eluser]CroNiX[/eluser]
$this->userdata(‘username’);

You're not accessing the session class here...
#3

[eluser]someone Smile[/eluser]
[quote author="CroNiX" date="1342459942"]$this->userdata(‘username’);

You're not accessing the session class here...[/quote]

Then I get this error: "Fatal error: Call to undefined method User::userdata()". But if I leave $this->session->userdata(‘username’); in other function in this controller, then it works normally. Error is only when I want to display data from userdata() or compare it to some string or int.

Where am I doing mistake?
#4

[eluser]InsiteFX[/eluser]
Code:
$this->session->set_userdata(array(
  'logged_in' => TRUE,
  'id_user' => $checklogin->id_user,
  'username' => $checklogin->username
));
#5

[eluser]someone Smile[/eluser]
It also doesn't work, but I changed code a little bit, because I didn't get errors.

This is new (only for testing) code:
Code:
$checklogin = $this->Userdat->check_login($this->input->post('username', TRUE), crypt($this->input->post('password', TRUE), 'somehash'));
if ($checklogin)
{
$this->session->set_userdata(array(
  'logged_in' => TRUE,
  'id_user' => $checklogin->row('id_user'),
  'username' => $checklogin->row('username')
));
//redirect('user');
echo $this->session->userdata('username');
echo $this->session->userdata('id_user');
}

After I log in, I get this error "Object of class stdClass could not be converted to string", same as before. In the case if I change to 'username' => $checklogin->username, I get this error: "Undefined property: CI_DB_mysql_result::$username".

Where is the problem?
#6

[eluser]someone Smile[/eluser]
I found a problem...
Instead of
Code:
$checklogin = $this->db->select('id_user', 'username')->where(array('username' => $username, 'password' => $password))->get('users');
It should be
Code:
$checklogin = $this->db->select('id_user, username')->where(array('username' => $username, 'password' => $password))->get('users');

Thanks for help anyway! :-)
#7

[eluser]InsiteFX[/eluser]
It's because of the type your returning from your database query.
Code:
$checklogin = $this->Userdat->check_login($this->input->post('username', TRUE), crypt($this->input->post('password', TRUE), 'somehash'))->result_array();
if ($checklogin)
{
$this->session->set_userdata(array(
  'logged_in' => TRUE,
  'id_user' => $checklogin['id_user'],
  'username' => $checklogin['username']
));
//redirect('user');
echo $this->session->userdata('username');
echo $this->session->userdata('id_user');
}


CodeIgniter Users Guide - Generating Query Results




Theme © iAndrew 2016 - Forum software by © MyBB