<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.commandline.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'features.commandline.webserver.php',
    1 => 'Web server interno',
    2 => 'Web server interno',
  ),
  'up' => 
  array (
    0 => 'features.commandline.php',
    1 => 'Command line usage',
  ),
  'prev' => 
  array (
    0 => 'features.commandline.interactive.php',
    1 => 'Shell interattiva',
  ),
  'next' => 
  array (
    0 => 'features.commandline.ini.php',
    1 => 'INI settings',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'it',
    'path' => 'features/commandline.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.commandline.webserver" class="section">
  <h2 class="title">Web server interno</h2>

  <div class="warning"><strong class="warning">Avviso</strong>
   <p class="para">
    Questo server web è stato sviluppato per aiutare lo sviluppo di applicazioni.  Può
    anche essere utile per scopo di test o per dimostrazioni applicative che sono
    eseguite in ambienti controllati. Non è destinato ad essere un web server
    con tutte le funzionalità. Non dovrebbe essere usato su una rete pubblica.
   </p>
  </div>
  
  <p class="para">
   La <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> fornisce un web server interno.
  </p>

  <p class="para">
   Il web server esegue solo un processo con un thread, quindi
   le applicazioni PHP andranno in stallo se una richiesta è bloccata.
  </p>
  
  <p class="para">
    Le richieste URI sono eseguite dalla cartella di lavoro corrente, in cui
    PHP è stato eseguito, a meno che non sia stata usata l&#039;opzione -t per specificare una
    radice dei documenti. Se una richiesta URI non specifica un file,
    allora viene restituito o index.php o index.html nella directory
    data. Se i file non esistono, la ricerca per index.php e index.html
    continuerà nella cartella padre e così via finchè non ne viene trovato uno o
    non viene raggiunto il document root. Se viene trovato un index.php o un index.html,
    esso viene restituito e viene impostato $_SERVER[&#039;PATH_INFO&#039;] alla parte finale
    dell&#039;URI. In caso contrario viene restituita una risposta con il codice 404.
  </p>

  <p class="para">
    Se un file PHP viene specificato sulla linea di comando quando viene attivato il web server,
    viene trattato come script &quot;router&quot;  per il web server.
    Lo script è eseguito all&#039;inizio di ogni richiesta HTTP. Se
    restituisce <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, la risorsa richiesta viene ritornata
    così com&#039;è. altrimenti viene restituito al browser il risultato dello script.
  </p>

  <p class="para">
   I tipi MIME standard vengono restituiti per i file con le estensioni:
   <span class="simplelist"><code class="literal">.3gp</code>, <code class="literal">.apk</code>, <code class="literal">.avi</code>, <code class="literal">.bmp</code>, <code class="literal">.css</code>, <code class="literal">.csv</code>, <code class="literal">.doc</code>, <code class="literal">.docx</code>, <code class="literal">.flac</code>, <code class="literal">.gif</code>, <code class="literal">.gz</code>, <code class="literal">.gzip</code>, <code class="literal">.htm</code>, <code class="literal">.html</code>, <code class="literal">.ics</code>, <code class="literal">.jpe</code>, <code class="literal">.jpeg</code>, <code class="literal">.jpg</code>, <code class="literal">.js</code>, <code class="literal">.kml</code>, <code class="literal">.kmz</code>, <code class="literal">.m4a</code>, <code class="literal">.mov</code>, <code class="literal">.mp3</code>, <code class="literal">.mp4</code>, <code class="literal">.mpeg</code>, <code class="literal">.mpg</code>, <code class="literal">.odp</code>, <code class="literal">.ods</code>, <code class="literal">.odt</code>, <code class="literal">.oga</code>, <code class="literal">.ogg</code>, <code class="literal">.ogv</code>, <code class="literal">.pdf</code>, <code class="literal">.png</code>, <code class="literal">.pps</code>, <code class="literal">.pptx</code>, <code class="literal">.qt</code>, <code class="literal">.svg</code>, <code class="literal">.swf</code>, <code class="literal">.tar</code>, <code class="literal">.text</code>, <code class="literal">.tif</code>, <code class="literal">.txt</code>, <code class="literal">.wav</code>, <code class="literal">.webm</code>, <code class="literal">.wmv</code>, <code class="literal">.xls</code>, <code class="literal">.xlsx</code>, <code class="literal">.xml</code>, <code class="literal">.xsl</code>, <code class="literal">.xsd</code>, <code class="literal">.zip</code></span>
   .
  </p>

  <p class="simpara">
   As of PHP 7.4.0, the built-in webserver can be configured to fork multiple
   workers in order to test code that requires multiple concurrent requests
   to the built-in webserver.
   Set the <var class="envar">PHP_CLI_SERVER_WORKERS</var> environment variable to the
   number of desired workers before starting the server.
  </p>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">This feature is not supported on Windows.</span>
  </p></blockquote>
  <div class="warning"><strong class="warning">Avviso</strong>
   <p class="para">
    This <em>experimental</em> feature is <em>not</em>
    intended for production usage. Generally, the built-in Web Server is
    <em>not</em> intended for production usage.
   </p>
  </div>

  <div class="example" id="example-1">
   <p><strong>Example #1 Avvio del web server</strong></p> 
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ cd ~/public_html
$ php -S localhost:8000</pre>
</div>
   </div>

   <div class="example-contents"><p>
     Il terminale mostrerà:
   </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit
</pre></div>
   </div>
   <div class="example-contents"><p>
     Dopo le richiste URI per http://localhost:8000/ e
     http://localhost:8000/myscript.html il terminale mostrerà
     qualcosa di simile a:
   </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit.
[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read
[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read
[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read
[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read
[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read
</pre></div>
   </div>
   <div class="example-contents"><p>
    Notare che prima di PHP 7.4.0, le risorse statiche con collegamenti simbolici non erano
    accessibili su Windows, a meno che lo script del router non le gestisse.
   </p></div>
  </div>

  <div class="example" id="example-2">
   <p><strong>Example #2 Esecuzione con una cartella di radice dei documenti specifica</strong></p> 
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ cd ~/public_html
$ php -S localhost:8000 -t foo/</pre>
</div>
   </div>

   <div class="example-contents"><p>
     Il terminale mostrerà:
   </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011
Listening on localhost:8000
Document root is /home/me/public_html/foo
Press Ctrl-C to quit
</pre></div>
   </div>
  </div>

  <div class="example" id="example-3">
   <p><strong>Example #3 Uso di uno script router</strong></p>
<div class="example-contents"><p>
  In questo esempio, le richieste per delle immagini le mostreranno, ma richieste per file HTML mostreranno &quot;Welcome to PHP&quot;:
</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// router.php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/\.(?:png|jpg|jpeg|gif)$/'</span><span style="color: #007700">, </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"REQUEST_URI"</span><span style="color: #007700">])) {<br />    return </span><span style="color: #0000BB">false</span><span style="color: #007700">;    </span><span style="color: #FF8000">// serve la risorsa richiesta così com'è.<br /></span><span style="color: #007700">} else { <br />    echo </span><span style="color: #DD0000">"&lt;p&gt;Welcome to PHP&lt;/p&gt;"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php -S localhost:8000 router.php</pre>
</div>
   </div>

  </div>
  
  <div class="example" id="example-4">
   <p><strong>Example #4 Controllo dell&#039;Uso del Server Web CLI</strong></p>
<div class="example-contents"><p>
 Per riutilizzare uno script del router del framework durante lo sviluppo con il web server CLI e successivamente anche con un server web di produzione:
</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// router.php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">php_sapi_name</span><span style="color: #007700">() == </span><span style="color: #DD0000">'cli-server'</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">/* route static assets and return false */<br /></span><span style="color: #007700">}<br /></span><span style="color: #FF8000">/* prosegue con le normali operazioni di index.php */<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php -S localhost:8000 router.php</pre>
</div>
   </div>

  </div>

  <div class="example" id="example-5">
   <p><strong>Example #5 Gestione Tipi File Non Supportati</strong></p> 
<div class="example-contents"><p>
  Se si ha bisogno di restituire una risorsa statica il cui tipo MIME non viene gestito dal web server CLI, utilizzare:
</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// router.php<br /></span><span style="color: #0000BB">$path </span><span style="color: #007700">= </span><span style="color: #0000BB">pathinfo</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"SCRIPT_FILENAME"</span><span style="color: #007700">]);<br />if (</span><span style="color: #0000BB">$path</span><span style="color: #007700">[</span><span style="color: #DD0000">"extension"</span><span style="color: #007700">] == </span><span style="color: #DD0000">"el"</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-Type: text/x-script.elisp"</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"SCRIPT_FILENAME"</span><span style="color: #007700">]);<br />}<br />else {<br />    return </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php -S localhost:8000 router.php</pre>
</div>
   </div>

  </div>

  <div class="example" id="example-6">
   <p><strong>Example #6 Accedere Al Web Server CLI Da Macchine Remote</strong></p> 
   <div class="example-contents"><p>
    È possibile rendere accessibile il web server sulla porta 8000 per qualsiasi interfaccia con:
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php -S 0.0.0.0:8000</pre>
</div>
   </div>

   <div class="warning"><strong class="warning">Avviso</strong>
    <p class="para">
      Il server Web integrato non deve essere utilizzato su una rete pubblica.
    </p>
   </div>
  </div>
  
 </div><?php manual_footer($setup); ?>