title: CodeIgniter 3, AJAX issue
body: <p>Here we are talking about PHP CodeIgniter application with AJAX Calls and Sessions.</p>
  
  <p>Hello to all!</p>
  
  <p>I made some searchs on the web before posting this.</p>
  
  <p>Configuration : </p>
  
  <ul>
  <li>Database stored sessions with encrypted cookie</li>
  <li>CodeIgniter 3.1.10</li>
  <li>jQuery 3.1.1</li>
  <li><p>PHP 5.6 or 7.x (same issue)</p></li>
  <li><p>Localhost : no problems with sessions or anything</p></li>
  <li>OVH mutualise server (shared) : SOMETIMES it looses session, it's not the same time, I think it's server-side configuration but I would like to know if some of you have also faced this problem??</li>
  </ul>
  
  <p>The issue is that the AJAX call does not complete in case of session lost with an error like :</p>
  
  <p><a href="https://i.stack.imgur.com/DoKj6.png" rel="nofollow noreferrer">JS error</a></p>
  
  <p>In fact, invalid JSON.</p>
  
  <p>On phpinfo() for server-side le lifetime of cookie is 0, I think OVH Shared Server SOMETIMES renew sessions but it's not a fixed time.</p>
  
  <p>For "normal" navigation (controller / method that load view), the check is OK, if session is lost, the application redirect user to login page.</p>
  
  <p><em>I'm working on a dedicated server, with a good configuration, this problem will be solved when the server will be OK (Migration and rework with CodeIgniter 4 with NodeJS services) but I'm curious about this issue...</em></p>
  
  <pre><code>// Working for navigation
  private function isLogged() {
      if (!$this-&gt;session-&gt;userdata('connected')) { 
          redirect('user/login');
      }
  }
  </code></pre>
  
  <p>For AJAX calls, it's.... hum... I tried some tricks to "handle" this issue, like this for AJAX calls :</p>
  
  <pre><code>// Not working on AJAX calls
  private function ajaxIsLogged() {
      if (!$this-&gt;session-&gt;userdata('connected')) { 
          echo json_encode(['error' =&gt; true, 'message' =&gt; 'Session lost']);
          return false;
      }
  }
  </code></pre>
  
  <p>Here is an example of AJAX calling</p>
  
  <pre><code>    $.ajax({
          type: 'POST',
          url: url,
          data: data,
          beforeSend: function () {
              $("div#divLoading").addClass('show');
          },
          complete: function () {
              $("div#divLoading").removeClass('show');
          },
          success: function (res) {
              // In my case when the issue occurs, ISSUE is HERE
              // Trying to parse non-valid JSON
              // But I don't understand why server would echo something
              // The function isAjaxLogged would normally do the trick
              var result = JSON.parse(res);
  
              if (result.error) {
                  console.log(result.error);
  
                  // Here we can redirect to login page if ajaxIsLogged() was working
              }
  
              if (result.success) {
                  // Do things
              }
          }, error: function (err) {
              console.log(err);
          }
      });
  </code></pre>
  
  <p>I've tried many things without JSON.parse but error still occurs (not exactly the same without JSON.parse but error always occurs BEFORE my control functions, enjoy asynchronous :))</p>
  
  <p>Thanks for reading this, if any of you face this issue??
  I'm looking far with some headaches for CI / Sessions / Ajax and sometimes the solution is just a little thing.</p>
  
  <p>To summarize :</p>
  
  <ul>
  <li>Is there a way to cheat with sessions on shared / mutualise server with CI ?</li>
  <li>Is there a way to do a "proper trick" with AJAX requests when session is lost ?</li>
  </ul>