Welcome Guest, Not a member yet? Register   Sign In
cookie login help
#11

[eluser]UnknownPlayer[/eluser]
There is my controller:
Code:
function login() {
        if ($this->check_login()) redirect('user/profil');
        if ($this->input->post('remember')) $this->rememberme->setCookie($this->input->post('remember'));
        $data['title'] = "Login Page";

        $this->form_validation->set_error_delimiters('<p>','</p>');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[3]|xss_clean|strtolower');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[3]|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            $data['main_content'] = 'pages/login';
            $this->load->view('template', $data);
        } else {
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $user = $this->User_model->check_user($username, $password);

            if ($user->user_id) {
                $remember = $this->input->post('remember'); // correct ?
                if ($remember) $this->rememberme->setCookie($remember); // correct ?

                $login_data = array(
                                'user_id'   =>  $user->user_id,
                                'logged_in' =>  TRUE
                                );

                $this->session->set_userdata($login_data);
                if ($this->ref_url('get')) redirect($this->ref_url('get'));
                redirect('user');

            } else {
                $this->session->set_flashdata('login_error', TRUE);
                redirect('user/login');
            }

        }
    }


My login page view form:
Code:
&lt;?php
            echo form_open(base_url().'user/login');

            $username = array(
                'name'  =>  'username',
                'id'    =>  'username',
                'value' =>  set_value('username')
            );
            $password = array(
                'name'  =>  'password',
                'id'    =>  'password',
                'value' =>  ''
            );
            $remember = array(
                'name'  =>  'remember',
                'id'    =>  'remember',
                'value' =>  'Remember me'
            );
            $submit = array(
                'name'  =>  'login',
                'id'    =>  'login',
                'value' =>  'Prijavi se'
            );
        ?&gt;

        <h1>Prijavi se</h1>
        <fieldset>
            <label for="username">Username:</label>
            &lt;?php echo form_input($username); ?&gt;


            <label for="password">Password:</label>
            &lt;?php echo form_password($password); ?&gt;

            <label for="remember">Remember me:</label>
            &lt;?php echo form_checkbox($remember); ?&gt;

            &lt;?php echo form_submit($submit); ?&gt;
            &lt;?php
                if ($this->session->flashdata('login_error')) echo '<p>Error.</p>';
                echo validation_errors();
            ?&gt;

        </fieldset>
        &lt;?php echo form_close(); ?&gt;

And my check_login() function:
Code:
function check_login() {
        if ($this->rememberme->verifyCookie()) {
            // for users that have logged in with the "remember" checkbox checked
            return TRUE;
        } else if ($this->session->userdata('logged_in')) {
            // for users that did not login with the "remember" checkbox checked
            return TRUE;
        } else return FALSE;
    }

And 2 tables.
ci_session:
[Image: 2b7e6p.png]
and ci_cookies:
[Image: 2ppm3is.png]

And still error Sad

Code:
An Error Was Encountered

Unable to locate the model you have specified: user

When i remove:
Code:
if ($this->rememberme->verifyCookie()) {
            // for users that have logged in with the "remember" checkbox checked
            return TRUE;
        }
from check_login function, i don't get error. Sad
#12

[eluser]besson3c[/eluser]
Ahhh.. check out your RememberMe Spark config:

https://github.com/joeauty/RememberMe-Co...mberme.php

Set this to your liking. Remove the requiremodel and authfunc variable settings if you don't wish to use the supported callbacks provided by the Spark and instead wish to write your own authorization checks. These are simply intended to verify that the user exists and should have access, but it looks like you have already done that with your User_model.
#13

[eluser]UnknownPlayer[/eluser]
One more error fixed, but another is there Sad
Code:
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1

SELECT * FROM users WHERE user_id = ?

Filename: /home/up/www/oglasi/models/user_model.php

Line Number: 21

There is error $user_id = $this->session->userdata('user_id');
Can't retrive that session value Sad

What is next move ?


I don't know if this is error, but in db on field netId is "Remember me" text, is that right or no ?
#14

[eluser]besson3c[/eluser]
Getting the user id by calling on $this->session->userdata('user_id') is not going to be 100% reliable, because this is only set when you login, and only for that session. You need a:

Code:
if ($this->rememberme->verifyCookie() && !$this->session->userdata('user_id')) {
     $this->session->set_userdata('user_id', $this->rememberme->verifyCookie());
}

somewhere near the top of your code (e.g. your core controller) to handle accessing the site with an expired CodeIgniter session if you wish to use your user_id session variable this way. If this is not the cause of your problem, I'd suggest testing to see if there is a problem with retrieving session variables in general, outside of the context of the RememberMe spark.

Are you saying that the value of the netId field in ci_cookies is literally "Remember me"? This would be causing the problem if so, it should store your user id and be the exact same as what you'd expect $this->session->userdata('user_id') to be.

#15

[eluser]UnknownPlayer[/eluser]
I think that error is on that Remember me field in db, becouse i setCookie like this:
Code:
if ($user->user_id) {

                $remember = $this->input->post('remember'); // check if this is good
                if ($remember) $this->rememberme->setCookie($remember); // and this (i dont know what to put in setCookire func)

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

                $login_data = array(
                                'user_id'   =>  $user->user_id,
                                'logged_in' =>  TRUE
                                );

                
                if ($this->ref_url('get')) redirect($this->ref_url('get'));
                redirect('user');

            }

Look at this comments, see if there is error?

Can i send you project(there are only user controller, model and view), and sql, so you can fix it, and tell me errors, to know for later, or maybe to join using teamviewer to check ?
#16

[eluser]InsiteFX[/eluser]
One thing to keep in mind is that radio buttons and checkboxes will not return any value if
they are not checked!

If you try to read a checkbox that is not checked you will get an error because the value is undefined.

For remember me I create a hash key using sha512 and save it in the users database record and write it out
to the cookie thats all!

If the user has a remember me cookie I read in the hash value from the cookie and get the users record
based on the hash code value, then I automatically log the user in.
#17

[eluser]UnknownPlayer[/eluser]
I made it works, thanks for help guys.
Mistake was on start of login controller, i set to secCookie, but not only when validation is run..

Thanks guys.

But do i need to use authfunc, or i can just use:
Code:
function check_login() {
        if ($this->rememberme->verifyCookie()) {
            // for users that have logged in with the "remember" checkbox checked
            return $this->rememberme->verifyCookie();
        } else if ($this->session->userdata('user_id')) {
            // for users that did not login with the "remember" checkbox checked
            return $this->session->userdata('user_id');
        } else return FALSE;
    }
on every page that need to be logged in, and i will get user_id with this function ?
#18

[eluser]besson3c[/eluser]
authfunc just allows you to plug in your own authorization checks to not only check to see if the user has their login cookie set, but whether they should have access to the application or page being viewed. This is actually important because if there is a valid cookie set and the user hasn't paid for subscription to your app or is no longer an employee or something, they shouldn't have perpetual access to the application.

If you don't need to do any particular authorization checks, what you have will work fine!
#19

[eluser]UnknownPlayer[/eluser]
Thanks, it works fine.
#20

[eluser]besson3c[/eluser]
Glad you got it working! If you are willing/able, I'd love to see your code in action if there is a URL you could point me at...




Theme © iAndrew 2016 - Forum software by © MyBB