Welcome Guest, Not a member yet? Register   Sign In
Sessions....... :(
#1

[eluser]Unknown[/eluser]
I am taking a stab at CI, and I have hit a bit of a wall. I created a user login function in my user controller, and it works dandy right up until I navigate away from the landing after sucessfully authenticating. My sessions disapear.


I will be using sessions to secure pages, so if your session user id value is not populated then you will be redirected to log in. Anyhow here is what I have.
Code:
function login()
    {


    $this->load->library('validation');
        $this->load->helper('url');
    $this->load->library('session');

    if($_POST['username'])
    {
        $this->load->database();
            $query = $this->db->query("SELECT * FROM users WHERE username = '".$_POST['username']."' and password = '".md5($_POST['password'])."'");
        
        if($query->num_rows() > 0){
            //return true;
            $status = "0";
            
             foreach ($query->result() as $row)
                 {
                 $username = $row->username;
                 $id = $row->id;
            
                }
            
            $sid = array('id' => $id);
            
            
            $this->session->set_userdata($sid);
            $data['goodz'] = $this->session->userdata('id');
            $data['status'] = $status;
            }
                
                else {
                    $status = "1";
                    $data['status'] = $status;
                    $data['goodz'] = $this->session->userdata('id');
                    }    
        }
$this->load->view('login_vals',$data);
    }
#2

[eluser]Flayra[/eluser]
As far as I can see there's nothing wrong with your code. However, I suggest you set the Session library to autoload (in config/autoload.php) since you need it on every page to keep the sessions running - that might be why it disappears after leaving this page.
#3

[eluser]Doosje[/eluser]
Same here,
Whenever i use the redirect / header function .. my session is initialized all over again.
It looses the 'userdata' section somehow.
I;ve loaded session in the autoload btw.

using: PHP VERSION 5.2.1 & Apache 2.0 CI->1.5.3



update: i've made a error .... my fault ..forget my post ..
#4

[eluser]Developer13[/eluser]
I came across what I believe to be is a similar issue to yours with one of my applications... but it would only happen on a specific server and sometimes the problem would exist and other times it wouldn't (don't ask -- I don't know!).

Anyhow, I would recommend setting the following in your system/application/config/config.php:

$config['cookie_prefix'] = "http://www";
$config['cookie_domain'] = ".domainnamehere.com";
$config['cookie_path'] = "/";

That might solve your problem.




Theme © iAndrew 2016 - Forum software by © MyBB