<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'features.http-auth.php',
    1 => 'HTTP authentication with PHP',
    2 => 'HTTP authentication with PHP',
  ),
  'up' => 
  array (
    0 => 'features.php',
    1 => 'Можливості',
  ),
  'prev' => 
  array (
    0 => 'features.php',
    1 => 'Можливості',
  ),
  'next' => 
  array (
    0 => 'features.cookies.php',
    1 => 'Куки',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'features/http-auth.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.http-auth" class="chapter">
  <h1 class="title">HTTP authentication with PHP</h1>


  <p class="simpara">
   It is possible to use the 
   <span class="function"><a href="function.header.php" class="function">header()</a></span> function to send an <code class="literal">&quot;Authentication Required&quot;</code> 
   message to the client browser causing it to pop up a Username/Password 
   input window.  Once the user has filled in a username and a password, 
   the URL containing the PHP script will be called again with the 
   <a href="reserved.variables.php" class="link">predefined variables</a> 
   <var class="varname">PHP_AUTH_USER</var>, <var class="varname">PHP_AUTH_PW</var>, 
   and <var class="varname">AUTH_TYPE</var> set to the user name, password and 
   authentication type respectively.  These predefined variables are found 
   in the <var class="varname"><a href="reserved.variables.server.php" class="classname">$_SERVER</a></var> array. <em>Only</em>
   the &quot;Basic&quot; authentication method is supported. See the
   <span class="function"><a href="function.header.php" class="function">header()</a></span> function for more information.
  </p>

  <p class="para">
   An example script fragment which would force client authentication
   on a page is as follows:
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Basic HTTP Authentication example</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: #007700">if (!isset(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">])) {<br />    </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'WWW-Authenticate: Basic realm="My Realm"'</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'HTTP/1.0 401 Unauthorized'</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">'Text to send if user hits Cancel button'</span><span style="color: #007700">;<br />    exit;<br />} else {<br />    echo </span><span style="color: #DD0000">"&lt;p&gt;Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]}</span><span style="color: #DD0000">.&lt;/p&gt;"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"&lt;p&gt;You entered </span><span style="color: #007700">{</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_PW'</span><span style="color: #007700">]}</span><span style="color: #DD0000"> as your password.&lt;/p&gt;"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <strong>Compatibility Note</strong><br />
   <p class="para">
    Please be careful when coding the HTTP header lines. In order to guarantee maximum
    compatibility with all clients, the keyword &quot;Basic&quot; should be written with an
    uppercase &quot;B&quot;, the realm string must be enclosed in double (not single) quotes,
    and exactly one space should precede the <em>401</em> code in the 
    <em>HTTP/1.0 401</em> header line. Authentication parameters have
    to be comma-separated.
   </p>
  </p></blockquote>

  <p class="para">
   Instead of simply printing out <var class="varname">PHP_AUTH_USER</var> 
   and <var class="varname">PHP_AUTH_PW</var>, as done in the above example, 
   you may want to check the username and password for validity.  
   Perhaps by sending a query to a database, or by looking up the 
   user in a dbm file.
  </p>

  <p class="para">
   Watch out for buggy Internet Explorer browsers out there.  They
   seem very picky about the order of the headers.  Sending the
   <em>WWW-Authenticate</em> header before the
   <code class="literal">HTTP/1.0 401</code> header seems to do the trick
   for now.
  </p>

  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <strong>Configuration Note</strong><br />
   <p class="para">
    PHP uses the presence of an <code class="literal">AuthType</code> directive
    to determine whether external authentication is in effect.
   </p>
  </p></blockquote>

  <p class="simpara">
   Note, however, that the above does not prevent someone who
   controls a non-authenticated URL from stealing passwords from
   authenticated URLs on the same server.
  </p>
  <p class="simpara">
   Both Netscape Navigator and Internet Explorer will clear the local browser
   window&#039;s authentication cache for the realm upon receiving a
   server response of 401. This can effectively &quot;log out&quot; a user,
   forcing them to re-enter their username and password. Some people
   use this to &quot;time out&quot; logins, or provide a &quot;log-out&quot; button.
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Приклад #2 HTTP Authentication example forcing a new name/password</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: #007700">function </span><span style="color: #0000BB">authenticate</span><span style="color: #007700">() {<br />    </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'WWW-Authenticate: Basic realm="Test Authentication System"'</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'HTTP/1.0 401 Unauthorized'</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">"You must enter a valid login ID and password to access this resource\n"</span><span style="color: #007700">;<br />    exit;<br />}<br /> <br />if (!isset(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]) ||<br />    (</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'SeenBefore'</span><span style="color: #007700">] == </span><span style="color: #0000BB">1 </span><span style="color: #007700">&amp;&amp; </span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'OldAuth'</span><span style="color: #007700">] == </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">])) {<br />    </span><span style="color: #0000BB">authenticate</span><span style="color: #007700">();<br />} else {<br />    echo </span><span style="color: #DD0000">"&lt;p&gt;Welcome: " </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]) . </span><span style="color: #DD0000">"&lt;br /&gt;"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Old: " </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'OldAuth'</span><span style="color: #007700">]);<br />    echo </span><span style="color: #DD0000">"&lt;form action='' method='post'&gt;\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"&lt;input type='hidden' name='SeenBefore' value='1' /&gt;\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"&lt;input type='hidden' name='OldAuth' value=\"" </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]) . </span><span style="color: #DD0000">"\" /&gt;\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"&lt;input type='submit' value='Re Authenticate' /&gt;\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"&lt;/form&gt;&lt;/p&gt;\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="simpara">
   This behavior is not required by the <code class="literal">HTTP Basic</code>
   authentication standard, so you should never depend on this. Testing with
   <code class="literal">Lynx</code> has shown that <code class="literal">Lynx</code> does not clear
   the authentication credentials with a 401 server response, so pressing back
   and then forward again will open the resource as long as the credential
   requirements haven&#039;t changed. The user can press the
   <code class="literal">&#039;_&#039;</code> key to clear their authentication information, however.
  </p>
  <p class="simpara">
   In order to get HTTP Authentication to work using IIS server with the CGI version
   of PHP you must edit your IIS configuration &quot;<code class="literal">Directory Security</code>&quot;.
   Click on &quot;<code class="literal">Edit</code>&quot; and only check
   &quot;<code class="literal">Anonymous Access</code>&quot;, all other fields
   should be left unchecked.
  </p>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <strong>IIS Note:</strong><br />
   <span class="simpara">
    For HTTP Authentication to work with IIS, the PHP directive
    <a href="ini.core.php#ini.cgi.rfc2616-headers" class="link">cgi.rfc2616_headers</a> must
    be set to <code class="literal">0</code> (the default value).
   </span>
  </p></blockquote>

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