CodeIgniter Forums

Full Version: Sessions question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jinit13[/eluser]
I'm trying to implement a simple login system, however, I followed CI user guide on sessions library and i can't get it work
The login method on users controller
Code:
function login(){
            $rules['name']    = "trim|required";
            $rules['password']    = "trim|required|md5";

            $this->validation->set_rules($rules);
            if ($this->validation->run()==FALSE){
                $this->load->view("users_login");

            }else{
                $name=$this->input->post('name');
                $password=$this->input->post('password');
                if($this->User_model->user_exists($name, $password)){
                    //Login code..
                        $sess_data = array(
                            "logged_in"        =>    TRUE,
                            "name"    =>    $name);
                        $this->session->set_userdata($sess_data);

                        $this->load->view('users_index');
                }


            }

users_login view:
Code:
<html>
    <head>
        <title>
            Login
        </title>
    </head>
    <body>
        &lt;?=form_open('users/login')?&gt;<br/ >
        &lt;?=form_fieldset('Login')?&gt;
        &lt;?php
        if($this->validation->error_string) {
            echo $this->validation->error_string . "<br />";
        }?&gt;

              <table>
                <tr>
                  <td>Name:</td><td>&lt;?=form_input('name', '')?&gt;</td>
                </tr>
                <tr>
                  <td>Password:</td><td>&lt;?=form_password('password', '')?&gt;</td>
                </tr>
                  <td>&lt;?=form_submit('btnsubmit', 'Login!')?&gt;</td><td></td>
                </tr>
              </table>

        &lt;?=form_fieldset_close()?&gt;
        &lt;?=form_close()?&gt;
    &lt;/body&gt;

&lt;/html&gt;

users_index view:
Code:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Users&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    <table>
        <th>Users</th>


        &lt;?php foreach($q->result() as $row):?&gt;
            <tr>
                <td>&lt;?=$row->name?&gt;</td>
            </tr>
        &lt;?php endforeach;?&gt;


    </table>


      <hr width="100!"! size="2">


    &lt;?php
        if ($this->session->userdata("logged_in"==TRUE)){
            echo("Logged in as ". $this->session->userdata("name"));
        }else{
            echo "Unknown!";
        }
    ?&gt;

    &lt;/body&gt;


&lt;/html&gt;
This part exactly is what I'm looking for
Code:
if ($this->session->userdata("logged_in")==TRUE){
            echo("Logged in as ". $this->session->userdata("name"));
        }else{
            echo "Unknown!";
        }
Thanks in advance

El Forum

[eluser]Colin Williams[/eluser]
Try something besides a boolean value to test authentication, like '1' or 'yes'

El Forum

[eluser]Michael Wales[/eluser]
Even better, why worry about the logged_in value at all? You have a name value right there that will serve perfectly.

Code:
if ($this->session->userdata('name')) {
  echo 'Logged in as: ' . $this->session->userdata('name');
} else {
  echo 'Not logged in.';
}

Code:
function logout() {
  $this->session->set_userdata(array('name' => FALSE));
}

El Forum

[eluser]Colin Williams[/eluser]
There's a chance you want to keep a name in the session data to do something like, "Welcome back, Michael Wales! Please login here."

El Forum

[eluser]jinit13[/eluser]
Thanks everyone, I used 'logged_in' from the InkType system source code.
Anyway, I think the problem might be here
Code:
if($this->User_model->user_exists($name, $password))

User_model->user_exists($name, $password)
Code:
function user_exists($name, $password){
        $this->db->where('name',$name);
        $this->db->where('password', $password);
        $q=$this->db->get('users');
        if($q->num_rows() > 0 ){
            return TRUE;
        }else{
            return FALSE;
        }
    }
As it's evaluated to FALSE in the login method.
Code:
function login(){
            $rules['name']    = "trim|required";
            $rules['password']    = "trim|required|md5";

            $this->validation->set_rules($rules);
            if ($this->validation->run()==FALSE){
                $this->load->view("users_login");

            }else{
                $name=$this->input->post('name');
                $password=$this->input->post('password');
                if($this->User_model->user_exists($name, $password)){

                        //Login code..
                        $sess_data = array(
                            "name"    =>    $name);
                        $this->session->set_userdata($sess_data);
                        redirect('users/index');
                }else{
                    redirect('users/login');
                }
                //$this->load->view('users_index');



            }

El Forum

[eluser]Michael Wales[/eluser]
Enable the profiler and make sure your query is correct (CI is generating what you are expecting). If so, time to hit the database and figure out where your query logic is incorrect.

El Forum

[eluser]jinit13[/eluser]
I've enabled the profiler.. here's the output
Quote: BENCHMARKS
Loading Time Base Classes 0.0161
Controller Execution Time ( Users / Login ) 0.0321
Total Execution Time 0.0483
URI STRING
/users/login
GET DATA
No GET data exists
POST DATA
No POST data exists
QUERIES (0)
No queries were run

El Forum

[eluser]Michael Wales[/eluser]
Are you loading your model in autoload.php? I don't see it loaded up anywhere else...

El Forum

[eluser]jinit13[/eluser]
Yup

El Forum

[eluser]jinit13[/eluser]
Hi, I solved it out, the problem was in the database (password column VARCHAR(10)) so no way to match the MD5 generated hash.
Thanks again ^_^