title: PHP Garbage Collector Keeps Removing My Sessions
body: <p><strong>The Problem:</strong></p>
  
  <p>I have a website that uses PHP sessions to allow users to log in. It works fine. But the session expires too soon that 1 minute of inactivity will log out the user.</p>
  
  <p><strong>My Environment:</strong></p>
  
  <p>Php version: 7.1</p>
  
  <p>Server: NGINX</p>
  
  <p>Framework: CakePHP 3.5</p>
  
  <p><strong>What I did so far?</strong></p>
  
  <p>I did every single solution on the StackOverflow or any search result I get. I extended my session timeout both in php.ini and CakePHP configurations.</p>
  
  <p><strong>The solution</strong></p>
  
  <p>After 2 or 3 days of research, I found the solution. In my php.ini I found a configuration named <code>session.gc_probability</code> and I put value <code>0</code> for that. Now my sessions never get expired except if the user logs out intentionally.</p>
  
  <p><strong>And now my current problem is</strong>, I don't want my <code>session.gc_probability</code> configuration to be zero as it will not collect any garbage (Not really sure about this. Please correct me if this information is wrong.). And this will cause the sessions to remain for month or years which a real GARBAGE for the server.</p>
  
  <p>I got the idea of giving <code>session.gc_probability</code> zero value from <a href="http://php.net/manual/en/session.configuration.php#ini.session.gc-probability" rel="nofollow noreferrer">here</a></p>
  
  <pre><code>session.gc_divisor coupled with session.gc_probability defines the 
  probability that the gc (garbage collection) process is started on 
  every session initialization. The probability is calculated by using 
  gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that 
  the GC process starts on each request. session.gc_divisor defaults to 
  100.
  </code></pre>
  
  <p>What is exactly wrong with my configurations? What causes the garbage collection to remove my sessions that soon? <code>session.gc_probability</code> was 1 and <code>session.gc_divisor</code> was 1000. I think a process with 1/1000 probability should not start every 1 or 2 minutes.</p>