title: PHP session reset on every request
body: <p>Its have been few days since I'm trying to get this work. I have problem with PHP sessions. Server is <strong>nginx</strong>. I have three sub-directories (sub-domains): </p>
  
  <ul>
  <li><strong>app.dev/</strong>         (there is frontend, what client visit)</li>
  <li><strong>backend.app.dev/</strong> (php page which proccessing AJAX requests, and communicates with API)</li>
  <li><strong>api.app.dev/</strong>     (API)</li>
  </ul>
  
  <p>In <strong>app.dev/</strong> there is <code>template/</code> folder which contains PHP files like forms, header, and parts of site which get populated by depending of session data. This is loaded into <code>index.html</code> page via <code>jQuery.load()</code> function. </p>
  
  <p>On <strong>backend.app.dev/</strong> there is <code>page.php</code> file which process data, and send to API. </p>
  
  <p>And then, when I send AJAX request on login action, and after I get token from API, I need to store it into session on <code>page.php</code>. At that request, it stores it successful, but, when I send second request, which check is there a token in session, it says that session is empty. I tried print whole <code>$_SESSION</code> variable, and it's empty. <code>$_COOKIE</code> also. So session get dropped after every request. I googled a lot. I tried many solutions ( I will post a log of solutions that I've tried.) And I found that there is files created after every request in <code>/var/lib/php/sessions</code>. </p>
  
  <p>I tried to print session id on every request, and it's always different... </p>
  
  <p>From log file, send the same request few times: </p>
  
  <pre><code>[28.Sep.2017 04:39:12]: session id: c0556q1s47kger3l53tgrdadhu
  
  [28.Sep.2017 04:39:29]: session id: 83nmqq1l9d27v7jhh7748gq2ru
  
  [28.Sep.2017 04:39:33]: session id: ece5as0eb0egoolun8u9s0duqi
  </code></pre>
  
  <p>There is a log what I've tried: </p>
  
  <pre><code>//session_name("backend");
  //session_set_cookie_params(0, '/', '.dev');
  //session_start();
  
  //session_name('backend');
  //ini_set('session.cookie_domain', 'hr.dev');
  //ini_set('session.save_path', '/var/lib/php/session');
  //ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
  //session_start();
  
  //session_name("all");
  //$sess_life_time = 21600; //in seconds
  //$sess_path = "/";
  //$sess_domain = ".dev";
  //$sess_secure = true; // if you have secured session
  //$sess_httponly = true; // httponly flag
  
  //ini_set('suhosin.session.cryptdocroot', 'off');
  //ini_set('suhosin.cookie.cryptdocroot', 'off');
  
  //session_set_cookie_params($sess_life_time, $sess_path, $sess_domain, $sess_secure, $sess_httponly);
  
  //session_name("all");
  //ini_set("session.cokie_domain",".dev");
  //session_set_cookie_params(0, '/', '.dev');
  //session_start();
  
  //  session_name("mysession");
  //  session_set_cookie_params(0,"/",".dev",FALSE,FALSE);
  //  setcookie(session_name(), session_id(),0,"/",".dev");
  //  session_start();
  </code></pre>
  
  <p>What could be the problem? Is something missing on the server conf?</p>
  
  <p>I will be really, really thankful if we fix this as soon as possible. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I set session variables (start session) at the backend.app.dev/</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>There is <code>php.ini</code> info: </p>
  
  <pre><code>session.auto_start =&gt; Off =&gt; Off
  session.cache_expire =&gt; 180 =&gt; 180
  session.cache_limiter =&gt; nocache =&gt; nocache
  session.cookie_domain =&gt; no value =&gt; no value
  session.cookie_httponly =&gt; Off =&gt; Off
  session.cookie_lifetime =&gt; 0 =&gt; 0
  session.cookie_path =&gt; / =&gt; /
  session.cookie_secure =&gt; Off =&gt; Off
  session.gc_divisor =&gt; 1000 =&gt; 1000
  session.gc_maxlifetime =&gt; 1440 =&gt; 1440
  session.gc_probability =&gt; 0 =&gt; 0
  session.lazy_write =&gt; On =&gt; On
  session.name =&gt; PHPSESSID =&gt; PHPSESSID
  session.referer_check =&gt; no value =&gt; no value
  session.save_handler =&gt; files =&gt; files
  session.save_path =&gt; /var/lib/php/sessions =&gt; /var/lib/php/sessions
  session.serialize_handler =&gt; php =&gt; php
  session.sid_bits_per_character =&gt; 5 =&gt; 5
  session.sid_length =&gt; 26 =&gt; 26
  session.upload_progress.cleanup =&gt; On =&gt; On
  session.upload_progress.enabled =&gt; On =&gt; On
  session.upload_progress.freq =&gt; 1% =&gt; 1%
  session.upload_progress.min_freq =&gt; 1 =&gt; 1
  session.upload_progress.name =&gt; PHP_SESSION_UPLOAD_PROGRESS =&gt; PHP_SESSION_UPLOAD_PROGRESS
  session.upload_progress.prefix =&gt; upload_progress_ =&gt; upload_progress_
  session.use_cookies =&gt; On =&gt; On
  session.use_only_cookies =&gt; On =&gt; On
  session.use_strict_mode =&gt; Off =&gt; Off
  session.use_trans_sid =&gt; 0 =&gt; 0
  </code></pre>