title: Shared Session Across Domains
body: <p>Okay.. I have two domains example.com and example.net, they share the same code space and sessions. The problem is that the domain is supposed to change when the user changes the language, when this happens all the session data is lost. I want to do an ajax call right before that so I can change the session.
  Here is the code which is used to initialize the session.</p>
  
  <pre><code>&lt;?php 
      /* This method is invoked */
      public function start($session_id = '', $key = 'default') {
          if (!session_id()) {
              ini_set('session.use_only_cookies', 'Off');
              ini_set('session.use_cookies', 'On');
              ini_set('session.use_trans_sid', 'Off');
              ini_set('session.cookie_httponly', 'On');
  
              if ($session_id) {
                  session_id($session_id);
              }   
  
              if (isset($_COOKIE[session_name()]) &amp;&amp; !preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $_COOKIE[session_name()])) {
                  exit('Error: Invalid session ID!');
              }
  
              session_set_cookie_params(0, '/');
              session_start();
          }
  
          if (!isset($_SESSION[$key])) {
              $_SESSION[$key] = array();
          }
  
          $this-&gt;data =&amp; $_SESSION[$key]; 
  
          return true;            
      }
  </code></pre>
  
  <p>I tried creating a standalone script to handle the ajax call, but It failed to work.</p>
  
  <pre><code>&lt;?php 
      ini_set('session.use_only_cookies', 'Off');
      ini_set('session.use_cookies', 'On');
      ini_set('session.use_trans_sid', 'Off');
      ini_set('session.cookie_httponly', 'On');
  
  
      session_set_cookie_params(0, '/');
      session_start();
      session_id($_POST['session_id']);
  
  
      header('Content-Type: application/json');
      $data = array('session_id' =&gt; session_id());
      echo json_encode($data);
  </code></pre>
  
  <p>The session is not preserved when switching between the two domains.
  Any clues?</p>