sharing session data across multiple applications

[eluser]Frank Liu[/eluser]
Does anyone know of a way to do this?

Let's say I have 3 apps:


and I want to share a single session table for the backend, what are the appropriate settings?

The problem i found was somehow the session data is "refreshing" so i would lose session
data when I go across apps.

Yes you can share sessions over multiple applications, as long as they have the same domain.
Just check your config file. In the comments it tells you how.


I looked all through the config file but there was no mention of how to share session data across multiple applications. The domain is same, and they're both connecting to the same database that stores session data, and yet, as soon as I move from one application to the other, the session data is destroyed. What do I need to do to keep the session going across multiple applications?


In CI, sessions are bind to cookies.
You probably have seen
$config['cookie_domain']    = "";
this in your config file. For site wide cookies, set it to Set to .your-domain.tld.

hope it helps.

Thanks for the suggestion. I went into both config files and set the cookie domain to, which is the address my site is located at. And after I log into the control panel app, here's what the cookie looks like in my browser:

And here's the code in the second application, which is the default application:

<code>$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != TRUE) {
} else {
$data['pageTitle'] = "Your one-stop shop for properties with potential.";
$data['main_content'] = "home";
$this->load->view('includes/template', $data);


You might want to remove any underscore in your cookie name.
An other option is to use a session table. It is easier to debug (because you see what data is stored in the session) and if you use multiple webservers, you don't have to migrate the session.

good luck..

Removed the underscore and it still doesn't work. And I am using a database for sessions, it's called 'rr_sessions'. Any other ideas? Anything I could do to debug and figure out what the problem is?


what happens when you are logged in to the admin panel and you open a new browser window (same browser)and open the default app. Do you get a new row in you session table? What does the session data look like.
Create a document called MY_Profiler.php in your application/libraries directory
and paste the following code in it.

&lt;?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Profiler extends CI_Profiler {

     * Adds session data to the profiler
     * Adds a table row for each item of session data with the key and value
     * Shows both CI session data and custom session data

    function _compile_session() {    

        $output  = "\n\n";
        $output .= '<fieldset style="border:1px solid #009999;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
        $output .= "\n";
        $output .= '<legend style="color:#009999;">&nbsp;&nbsp;'.'SESSION DATA'.'&nbsp;&nbsp;</legend>';
        $output .= "\n";

        if (!is_object($this->CI->session))
            $output .= "<div style='color:#009999;font-weight:normal;padding:4px 0 4px 0'>".'No SESSION data exists'."</div>";
            $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";

            $sess = get_object_vars($this->CI->session);

            foreach ($sess['userdata'] as $key => $val)
                if ( ! is_numeric($key))
                    $key = "'".$key."'";

                $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>$_SESSION[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#009999;font-weight:normal;background-color:#ddd;'>";

                if (is_array($val))
                    $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
                    $output .= htmlspecialchars(stripslashes($val));

                $output .= "</td></tr>\n";

            $output .= "</table>\n";

        $output .= "</fieldset>";

        return $output;    

    function run()
        $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
        $output .= $this->_compile_uri_string();
        $output .= $this->_compile_controller_info();
        $output .= $this->_compile_memory_usage();
        $output .= $this->_compile_benchmarks();
        $output .= $this->_compile_get();
        $output .= $this->_compile_post();
        $output .= $this->_compile_queries();
        $output .= $this->_compile_session();
        $output .= '</div>';

        return $output;

in your constructor of your controller.

Ps. did you change both config files?

Thanks. I'm still logged in in a new browser window, and no new session data is entered into the db.

Here's what I get after I login to the control panel:
And on the default application:

Looks like it's trying to select session data, and there is indeed a record with that exact information. Is the data not being split correctly or something? The user data begins with something like this - a:6:{s:5:"email";s:19:"[email protected]";s:12:" - not sure what the technical term is for that, but is CI supposed to automatically set up the variables and stuff from that data?


did you try to romove the cookies before refreshing?
I don't see you're session veriables in your profiler, Did you add the MY_Profiler to your libraries. Does the session_id change with every request/reload of the page?

The data in the session table is json encoded. but i did not see the 'is_logged_in' veriable in your post. (please use the code highlighting function in this editor). what are the all config veriables related to the session / cookie for both apps?

I Hope you find the problem, because it shouldn't be one....

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.