<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.session.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.session-gc.php',
    1 => 'session_gc',
    2 => 'Perform session data garbage collection',
  ),
  'up' => 
  array (
    0 => 'ref.session.php',
    1 => 'Session Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.session-encode.php',
    1 => 'session_encode',
  ),
  'next' => 
  array (
    0 => 'function.session-get-cookie-params.php',
    1 => 'session_get_cookie_params',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/session/functions/session-gc.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.session-gc" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_gc</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.1.0, PHP 8)</p><p class="refpurpose"><span class="refname">session_gc</span> &mdash; <span class="dc-title">Perform session data garbage collection</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.session-gc-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>session_gc</strong></span>(): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">
   By default, PHP uses <a href="session.configuration.php#ini.session.gc-probability" class="link">session.gc_probability</a>
   to run the session garbage collector probabilistically on each
   request. There are some limitations with this approach:
  </p>
  <ul class="simplelist">
   <li>Low traffic sites may not have their session data deleted within the preferred duration.</li>
   <li>High traffic sites may have the garbage collector run too frequently, performing unnecessary extra work.</li>
   <li>Garbage collection is performed on the user's request, and the user may experience a delay.</li>
  </ul>
  <p class="simpara">
   For production systems, it is recommended to disable the
   probability-based garbage collection by setting
   <a href="session.configuration.php#ini.session.gc-probability" class="link">session.gc_probability</a> to <code class="literal">0</code>
   and explicitly trigger the garbage collector periodically, for example by using &quot;cron&quot; on
   UNIX-like systems to run a script that calls <span class="function"><strong>session_gc()</strong></span>.
  </p>

  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">
    When calling <span class="function"><strong>session_gc()</strong></span> from a command-line php script,
    the <a href="session.configuration.php#ini.session.save-path" class="link">session.save_path</a> must be set
    to the same value as web requests, and the script must have access and delete
    permissions for the session files. This may be affected by the user the script runs as,
    and container or sandboxing features such as systemd&#039;s <code class="literal">PrivateTmp=</code>
    option.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.session-gc-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">Questa funzione non contiene parametri.</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-gc-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="simpara">
   <span class="function"><strong>session_gc()</strong></span> returns the number of deleted session
   entries on success,  o <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> in caso di fallimento.
  </p>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">
    Old session save handlers do not return the number of deleted session entries, but
    rather only a success/failure flag. If this is the case, <code class="literal">1</code> is returned regardless of
    how many session entries are actually deleted.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 examples" id="refsect1-function.session-gc-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="function"><strong>session_gc()</strong></span> example for task managers like cron</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Note: This script should be executed by the same user of web server process.<br /><br />// Need active session to initialize session data storage access.<br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Executes GC immediately<br /></span><span style="color: #0000BB">session_gc</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Clean up session ID created by session_start()<br /></span><span style="color: #0000BB">session_destroy</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-2">
   <p><strong>Example #2 <span class="function"><strong>session_gc()</strong></span> example for user accessible script</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Note: session_gc() is recommended to be used by a task manager script, but<br />// it may be used as follows.<br /><br />// Used for last GC time check<br /></span><span style="color: #0000BB">$gc_time </span><span style="color: #007700">= </span><span style="color: #DD0000">'/tmp/php_session_last_gc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$gc_period </span><span style="color: #007700">= </span><span style="color: #0000BB">1800</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">// Execute GC only when GC period elapsed. <br />// i.e. Calling session_gc() every request is waste of resources. <br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$gc_time</span><span style="color: #007700">)) {<br />    if (</span><span style="color: #0000BB">filemtime</span><span style="color: #007700">(</span><span style="color: #0000BB">$gc_time</span><span style="color: #007700">) &lt; </span><span style="color: #0000BB">time</span><span style="color: #007700">() - </span><span style="color: #0000BB">$gc_period</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">session_gc</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">touch</span><span style="color: #007700">(</span><span style="color: #0000BB">$gc_time</span><span style="color: #007700">);<br />    }<br />} else {<br />    </span><span style="color: #0000BB">touch</span><span style="color: #007700">(</span><span style="color: #0000BB">$gc_time</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-gc-seealso">
  <h3 class="title">Vedere anche:</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.session-start.php" class="function" rel="rdfs-seeAlso">session_start()</a> - Inizializza i dati di sessione</span></li>
   <li><span class="function"><a href="function.session-destroy.php" class="function" rel="rdfs-seeAlso">session_destroy()</a> - Distrugge tutti i dati registrati in una sessione</span></li>
   <li><a href="session.configuration.php#ini.session.gc-probability" class="link">session.gc_probability</a></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>