Welcome Guest, Not a member yet? Register   Sign In
CI creates new session every page
#1

[eluser]Jhourlad Estrella[/eluser]
I am using a table to handle my sessions. I currently creating the login module suing the following codes:

Code:
class Login extends Controller {

    function Login()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
    }
    
    function index()
    {
    
        $data['attributes']  = array('class' => 'formbig', 'id' => 'frmLogin');
        
        $data['f_username']  = null;
        $data['f_password']  = null;
        
        $data['errmsg'] = '';        
        
        $this->form_validation->set_rules('f_username', 'Username', 'required|alpha_numeric|xss_clean|min_length[3]|max_length[12]');
        $this->form_validation->set_rules('f_password', 'Password', 'required|min_length[8]|max_length[30]');
                
        if ($this->form_validation->run()) {
            $uname  = $this->input->post('f_username');
            $passw  = md5($this->input->post('f_password'));
            
            if($user = dbGather('hib_users','*',"user_uname='$uname' AND user_passw='$passw'")) {
                $this->session->set_userdata('username',$user[0]['user_uname']);
                redirect('');
                
            } else {
                $data['errmsg'] = 'Invalid username/password.';
                
            }
        }
        
        $this->load->view('header',$data);
        $this->load->view('login/login_index',$data);
        $this->load->view('footer',$data);

    }
}

The view is somewhat like this:

Code:
<div class="hctr" style="width:220px; margin-top:10px; margin-bottom:10px; ">
    
    &lt;? if(validation_errors() OR $errmsg) { ?&gt;
    <div class="redbox">
        <div>
        &lt;?=validation_errors()?&gt;
        &lt;?=$errmsg?&gt;
      </div>
    </div>
    <br />
    &lt;? } ?&gt;
    
    <div class="graybox">
        &lt;?php
        echo form_open('kblh-blogs-login',$attributes);

        echo form_label('Your username','f_username');
        echo form_input('f_username',$f_username);
        
        echo form_label('Your password','f_password');
        echo form_password('f_password',$f_password);
        
        echo form_submit('btnSubmit','Login');
        
        echo form_close();
        ?&gt;
        
      <div class="clr"></div>
    </div>
</div>

Now, my problem is every time I load a page CI tries to create a new session, thereby erasing the USERDATA from the previous page.

For example:

1. Login.php loads.
5b56e2c6a787256a57a0c929b0af83bd 127.0.0.1 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv 1250365988

2. Login.php saves the USERDATA after successful authentication. Error - New sessiojn was created:

5b56e2c6a787256a57a0c929b0af83bd 127.0.0.1 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv 1250365988
29a25bed37c182bc70dbf975fef5f389 127.0.0.1 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv 1250365995 a:1:{s:8:"username";s:5:"admin";}


3. Redirects to home.php. Another error - new session created:
5b56e2c6a787256a57a0c929b0af83bd 127.0.0.1 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv 1250365988
29a25bed37c182bc70dbf975fef5f389 127.0.0.1 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv 1250365995 a:1:{s:8:"username";s:5:"admin";}
81e4e6bc2ad7dd63271edc71d13aa3d4 127.0.0.1 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv 1250365995

I have been using PHP for years and this is the first time I encountered this scenario. Can somebody please help me point out where am I doing it wrong?

Thanks guys!
#2

[eluser]tomcode[/eluser]
Autoload the session, don't do it in a controller. Check Your session configs in config/config.php. Do You use the defaults ?
#3

[eluser]Jhourlad Estrella[/eluser]
Actually, I changed the cookie setting on the config.php. It stopped working. After I reset the cookie setting the session started working again. I've given up on this by the way. I'd rather go back to this issue after I finished on the more serious stuffs.
#4

[eluser]Unknown[/eluser]
I have been looking around for a solution to the same problem, and after many dead ends, I re-investigated my config.php.
Therein, I noticed that $config['cookie_domain'] wasn't set to my actual domain.
Upon setting this [and setting my cookie_prefix to ''], sessions now work correctly.
Since the cookie needs to be set to your domain in order to be pulled correctly, it was simply not finding a cookie reference for my session.
[Setting up database sessions helped immensely with this.]

Hope that helps!

///rEI
#5

[eluser]WanWizard[/eluser]
And when you're testing, please now that 'localhost' is an INVALID value for cookie_domain, according to the RFC.
Several new browsers are starting to reject cookies with this domain. Use 127.0.0.1 instead (dotted IP's are allowed).




Theme © iAndrew 2016 - Forum software by © MyBB