CodeIgniter Forums
Sessions....... :( - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Sessions....... :( (/thread-1730.html)



Sessions....... :( - El Forum - 06-22-2007

[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);
    }



Sessions....... :( - El Forum - 06-23-2007

[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.


Sessions....... :( - El Forum - 07-07-2007

[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 ..


Sessions....... :( - El Forum - 07-07-2007

[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.