CodeIgniter Forums
CI' session question - 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: CI' session question (/thread-13609.html)



CI' session question - El Forum - 11-29-2008

[eluser]chmod[/eluser]
If i enable the item sess_use_database,set
Code:
$config['sess_use_database']        = TRUE;

then CI will auot save user's session data in ci_session table.
If I set the customer's session data , the data will be store in this table.
Like this:
Code:
$this->session->set_userdata('islogin' => TRUE)

I can get the customer's data:
Code:
$this->session->userdata('islogin');

but in another controller:
I can't get the data,why?


CI' session question - El Forum - 11-29-2008

[eluser]iainco[/eluser]
Have you loaded the session library in the other controller?

You should probably have the session library autoload. You can change that in your autoload.php

Also, you have a syntax error with this code

Code:
$this->session->set_userdata('islogin' => TRUE)

It should be:

Code:
$this->session->set_userdata(array('islogin' => TRUE))

or

Code:
$this->session->set_userdata('islogin', TRUE)



CI' session question - El Forum - 11-29-2008

[eluser]chmod[/eluser]
[quote author="iainco" date="1227999289"]Have you loaded the session library in the other controller?

You should probably have the session library autoload. You can change that in your autoload.php

Also, you have a syntax error with this code

Code:
$this->session->set_userdata('islogin' => TRUE)

It should be:

Code:
$this->session->set_userdata(array('islogin' => TRUE))

or

Code:
$this->session->set_userdata('islogin', TRUE)
[/quote]



I am sure loaded the session lib in autoload.php:
Code:
$autoload['libraries'] = array('database','session','form_validation');


and I set session like this:
Code:
$custom_session_data = array('username' => $this->input->post('username'),
                                            'isloginto' => true,
                                            'session_id_user' => $this->session->userdata('session_id'));
                $this->session->set_userdata($custom_session_data);



CI' session question - El Forum - 11-29-2008

[eluser]iainco[/eluser]
Ok, that is all fine, can you show us the controller code where you can't seem to access the session data?


CI' session question - El Forum - 11-29-2008

[eluser]chmod[/eluser]
[quote author="iainco" date="1228002456"]Ok, that is all fine, can you show us the controller code where you can't seem to access the session data?[/quote]

Yes,my code is bellow:

login.php:
Code:
<?php
    class Loginto extends Controller{
        function Loginto(){
            parent::Controller();
            $this->load->model('center/loginto_model');
        }

        function index(){
            $data = array('title' => $this->lang->line('title_center_index'));
            $this->load->view('center/loginto_tpl',$data);
        }

        function checkUsernamePassword($username,$password){
            $resultCheckUsernamePassword = $this->loginto_model->checkUserLoginTo($this->input->post('username'),md5($this->input->post('password')),$this->lang->line('table_name_member'));
            if($resultCheckUsernamePassword){
                return true;
            }else{
                $this->form_validation->set_message('checkUsernamePassword',$this->lang->line('error_user_password'));
                return false;
            }
        }

        function iloginto(){
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $this->form_validation->set_rules('username','lang:username','trim|required|min_length[3]|max_length[15]|xss_clean');
            $this->form_validation->set_rules('password','lang:password','trim|required|min_length[8]|max_length[15]|xss_clean|callback_checkUsernamePassword');

            if($this->form_validation->run()){
                //set session
                $custom_session_data = array('username' => $this->input->post('username'),
                                            'isloginto' => true,
                                            'session_id_user' => $this->session->userdata('session_id'));
                $this->session->set_userdata($custom_session_data);


                if($this->session->userdata('forward')){
                    redirect($this->session->userdata('forward'),'refresh');
                }else{
                    $data = array('title' => $this->lang->line('title_mycenter_index'));
                    $this->load->view('center/mycenter_tpl',$data);
                }
            }else{
                $data = array('title' => $this->lang->line('title_center_index'));
                $this->load->view('center/loginto_tpl',$data);
            }
        }
    }
?>


mycenter.php:

Code:
<?php
class Mycenter extends Controller{
    function Mycenter(){
        parent::Controller();
        $this->load->model('common/security/security_model');
    }

    function index(){
        if ($this->security_model->isLoginTo() === true){
            $data = array('title' => $this->lang->line('title_mycenter_index'));
            $this->load->view('center/mycenter_tpl',$data);
        }else{
            $this->session->set_userdata('forward',$_SERVER['PHP_SELF']);
            redirect('center/loginto','refresh');
        }
    }
}
?>


Model:
Code:
<?php
class Security_model extends Model{
    function Security_model(){
        parent::Model();
    }

    function isLoginTo(){
        $session_id_user = $this->session->userdata('session_id_user');

        if (isset($session_id_user)){
            $this->db->where('session_id',$session_id_user);
            $this->db->from('ci_sessions');
            $query = $this->db->get();
            if ($query->num_rows() == 1){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }


    }

}
?>



my view:

mycenter_tpl.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
        &lt;title&gt;&lt;?=isset($title)?$title:'';?&gt;&lt;/title&gt;

        &lt;!--css link--&gt;
        &lt;link type="text/css" rel="stylesheet" href="&lt;?=CSS_URL;?&gt;css/index.css" /&gt;
        &lt;!--javascript--&gt;
        [removed][removed]
    &lt;/head&gt;
&lt;body&gt;

    &lt;?=$this->session->userdata('isloginto').'<---------&gt;'.$this->session->userdata('username') ;?&gt;
    <div><a >mybussiness</a></div>

&lt;/body&gt;
&lt;/html&gt;


in mycenter_tpl.php, the session data is displayed.but when I click the link: mybussiness

I will redirect to the controller mybussiness Controller.
But the session data can't access session data.
Why?

my mybussiness Controller:

Code:
&lt;?php
class Mybussiness extends Controller{
    function Mybussiness(){
        parent::Controller();
        $this->load->model('center/bussioness_model');
        $this->load->model('common/security/security_model');
    }

    function index(){
        if ($this->security_model->isLoginTo() === true){
            $data = array('title' => $this->lang->line('title_mybussiness_index'));
            $this->load->view('center/mybussiness_tpl',$data);
        }else{
            $this->session->set_userdata('forward',$_SERVER['PHP_SELF']);
            redirect('center/loginto','refresh');
        }
    }
}
?&gt;



CI' session question - El Forum - 12-01-2008

[eluser]iainco[/eluser]
Hi, sorry for taking so long to get back to you.

In your login.php you have this code (function iloginto() for setting session data):

Code:
'session_id_user' => $this->session->userdata('session_id')

The session_id in CI sessions is automatically regenerated, I think the default is every 5 minutes.

So when you try to access the other page where you are having problems, if the session has been regenerated, then you will be denied access. All the other session information will still be present and saved in the database but the session_id won't match.

Maybe someone else will have a look and try and help!


CI' session question - El Forum - 12-01-2008

[eluser]chmod[/eluser]
Thanks to iainco. you are right.although I saved the session_id before,but he session updated, and CI's method gc deleted it.so I can't acess it.so, I resolved this problem:
Edit the config.php:
Step 1:
Code:
$config['sess_cookie_name']        = 'ci_session';
$config['sess_expiration']        = 300;
$config['sess_encrypt_cookie']    = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']        = 'ci_sessions';
$config['sess_match_ip']        = TRUE;
$config['sess_match_useragent']    = TRUE;
$config['sess_time_to_update']     = 300;

Step 2:
decide gc:
$config['sess_probability'] = 25;


Step 3:
edit lib's Session.php
change:
Code:
if ((rand() % 100) < $this->gc_probability)

to

Code:
if ((rand() % $this->CI->config->item('sess_probability')) < $this->gc_probability)


Step 4:
change:
Code:
setcookie(
                    $this->sess_cookie_name,
                    $cookie_data,
                                        $this->sess_expiration + time(),
                    $this->cookie_path,
                    $this->cookie_domain,
                    0
                );
to
Code:
setcookie(
                    $this->sess_cookie_name,
                    $cookie_data,
                    0,
                    //$this->sess_expiration + time(),
                    $this->cookie_path,
                    $this->cookie_domain,
                    0
                );

all these code resolved three things:
1:when I close browser's windows,the cookie will be delete.
2:CI's gc will work by myself;
3Confusedession will not regen in short time.