Welcome Guest, Not a member yet? Register   Sign In
CodeIgniters session library - how to?
#1

[eluser]v33s[/eluser]
Hello all,
I am new in CodeIgniter and I have a question about CodeIgniters Sessions. Can someone explain me how to start write simple admin login page area? I tried most of complete Authorization Systems but I don`t understand how to implement them in my code.

Ok. I have my `admins` MySQL table with -id-name-md5password- columns.

On first place in my Contructor, i have that code:
Code:
$this->load->library(array('session', 'validation'));

I would like to check the form values with the database values, if it is correct then make a session.

I can extract user from database, but how to make a session, check it if admin is now logged in???

I found in User Guide how to make a session:

Code:
$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => '[email protected]',
                   'logged_in' => TRUE
               );

$this->session->set_userdata($newdata);

And now, what I must change in this array? Replace username with this from database?
How to check this session if it is correct?

Sorry for my English Wink
#2

[eluser]Seppo[/eluser]
Well... to login you have to store something on the session... you don't have an email, so, with your db columns you can do
Code:
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('admin');
if ($query->num_rows() > 0)
{
  $row = $query->row();
  $this->session->set_userdata(array(
    'id' => $row->id,
    'username' => $this->input->post('username'),
    'is_admin' => true
  ));
} else {
// Login failed
}

Then, on an admin-page you have to check any of those values...
Code:
$this->load->library('session');
if (!$this->session->userdata('is_admin'))
{
  // Not enough access!
  $this->load->helper('url');
  redirect('login');
  return;
}

// Is admin...
#3

[eluser]v33s[/eluser]
Hello! Thanks a lot Seppo. That`s I need!

Code:
if (!$this->session->userdata('is_admin'))
Code:
array(
    'id' => $row->id,
    'username' => $this->input->post('username'),
    'is_admin' => true

So I understand it now! Big Grin I must create a session with uniqe phrase like 'is_admin' and type it for TRUE. Next is to check if this session is TRUE and is set. But why I can not use something like that:

Code:
if(!isset($this->session->userdata('is_admin'))
#4

[eluser]Seppo[/eluser]
No... Isset only works with variables (empty is the same). Also the userdata method returns FALSE when it doesn't exists, not NULL.




Theme © iAndrew 2016 - Forum software by © MyBB