HTTP-аутентификация в PHP

Возможно использовать функцию header() для отправки сообщения "Authentication Required" браузеру, заставив его показать окошко для ввода логина и пароля. Как только пользователь заполнит логин и пароль, ссылка, содержащая PHP-скрипт будет вызвана еще раз с предопределенными переменными PHP_AUTH_USER, PHP_AUTH_PW, и AUTH_TYPE, установленными в логин, пароль и тип аутентификации соответственно. Эти предопределенные переменные хранятся в массивах $_SERVER и $HTTP_SERVER_VARS. Поддерживаются оба типа: "Basic" и "Digest" (начиная с версии PHP 5.1.0). Подробнее смотрите функцию header().

Замечание: Замечание касательно версии PHP

Суперглобальные переменные, такие как $_SERVER, доступны, начиная с » 4.1.0.

Пример фрагмента скрипта, который вынуждает клиента авторизоваться для просмотра страницы:

Пример #1 Пример Basic HTTP-аутентификации

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    
header('WWW-Authenticate: Basic realm="My Realm"');
    
header('HTTP/1.0 401 Unauthorized');
    echo 
'Текст, отправляемый в том случае,
    если пользователь нажал кнопку Cancel'
;
    exit;
} else {
    echo 
"<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo 
"<p>Вы ввели пароль {$_SERVER['PHP_AUTH_PW']}.</p>";
}
?>

Пример #2 Пример Digest HTTP-аутентификации

Это пример реализации простого скрипта Digest HTTP-аутентификации. За подробностями обращайтесь к » RFC 2617.

<?php
$realm 
'Запретная зона';

//user => password
$users = array('admin' => 'mypass''guest' => 'guest');


if (empty(
$_SERVER['PHP_AUTH_DIGEST'])) {
    
header('HTTP/1.1 401 Unauthorized');
    
header('WWW-Authenticate: Digest realm="'.$realm.
           
'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');

    die(
'Текст, отправляемый в том случае, если пользователь нажал кнопку Cancel');
}


// анализируем переменную PHP_AUTH_DIGEST
if (!($data http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
    !isset(
$users[$data['username']]))
    die(
'Неправильные данные!');


// генерируем корректный ответ
$A1 md5($data['username'] . ':' $realm ':' $users[$data['username']]);
$A2 md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);

if (
$data['response'] != $valid_response)
    die(
'Неправильные данные!');

// ok, логин и пароль верны
echo 'Вы вошли как: ' $data['username'];


// функция разбора заголовка http auth
function http_digest_parse($txt)
{
    
// защита от отсутствующих данных
    
$needed_parts = array('nonce'=>1'nc'=>1'cnonce'=>1'qop'=>1'username'=>1'uri'=>1'response'=>1);
    
$data = array();
    
$keys implode('|'array_keys($needed_parts));

    
preg_match_all('@(' $keys ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@'$txt$matchesPREG_SET_ORDER);

    foreach (
$matches as $m) {
        
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
        unset(
$needed_parts[$m[1]]);
    }

    return 
$needed_parts false $data;
}
?>

Замечание: Замечание касательно совместимости

Будьте особенно внимательны при указании HTTP-заголовков. Для того, чтобы гарантировать максимальную совместимость с наибольшим количеством различных клиентов, слово "Basic" должно быть написано с большой буквы "B", регион (realm) должен быть взят в двойные (не одинарные!) кавычки, и ровно один пробел должен предшествовать коду 401 в заголовке HTTP/1.0 401. Параметры аутентификации должны разделяться запятыми, как это было показано в примере Digest аутентификации выше.

Вместо простого отображения на экране переменных PHP_AUTH_USER и PHP_AUTH_PW, вам, возможно, понадобится проверить их корректность. Используйте для этого запрос к базе данных или поиск пользователя в dbm-файле.

Вы можете пронаблюдать особенности работы браузера Internet Explorer. Он очень требователен к параметру передаваемых заголовков. Трюк с указанием заголовка WWW-Authenticate перед отправкой статуса HTTP/1.0 401 пока что работает для него.

Для того, чтобы предотвратить написание кем-либо скрипта, раскрывающего пароль к странице, которая использует внешнюю аутентификацию, переменные PHP_AUTH не устанавливаются в случае, если данная страница использует внешнюю аутентификацию и установлен безопасный режим. Несмотря на это, переменная REMOTE_USER может использоваться для аутентификации пользователя, прошедшего внешнюю аутентификацию. Таким образом, вы всегда можете воспользоваться переменной $_SERVER['REMOTE_USER'].

Замечание: Замечание касательно конфигурации

PHP использует указание директивы AuthType для указания того, используется внешняя аутентификация или нет.

Следует заметить, что все вышесказанное не предотвращает похищения паролей к страницам, требующим авторизацию, кем-либо, кто контролирует страницы без авторизации, расположенные на том же сервере.

И Netscape Navigator и Internet Explorer очищают кэш аутентификации текущего окна для заданного региона (realm) при получении от сервера статуса 401. Это может использоваться для реализации принудительного выхода пользователя и повторного отображения диалогового окна для ввода имени пользователя и пароля. Некоторые разработчики используют это для ограничения авторизации по времени или для предоставления кнопки "Выход".

Пример #3 Пример HTTP-аутентификации с принудительным вводом новой пары логин/пароль

<?php
function authenticate() {
    
header('WWW-Authenticate: Basic realm="Test Authentication System"');
    
header('HTTP/1.0 401 Unauthorized');
    echo 
"Вы должны ввести корректный логин и пароль для получения доступа к ресурсу \n";
    exit;
}

if (!isset(
$_SERVER['PHP_AUTH_USER']) ||
    (
$_POST['SeenBefore'] == && $_POST['OldAuth'] == $_SERVER['PHP_AUTH_USER'])) {
    
authenticate();
} else {
    echo 
"<p>Добро пожаловать: " htmlspecialchars($_SERVER['PHP_AUTH_USER']) . "<br />";
    echo 
"Предыдущий логин: " htmlspecialchars($_REQUEST['OldAuth']);
    echo 
"<form action='' method='post'>\n";
    echo 
"<input type='hidden' name='SeenBefore' value='1' />\n";
    echo 
"<input type='hidden' name='OldAuth' value=\"" htmlspecialchars($_SERVER['PHP_AUTH_USER']) . "\" />\n";
    echo 
"<input type='submit' value='Авторизоваться повторно' />\n";
    echo 
"</form></p>\n";
}
?>

Это поведение не регламентируется стандартами HTTP Basic-аутентификации, следовательно, вы не должны зависеть от этого. Тестирование браузера Lynx показало, что Lynx не очищает кэш авторизации при получении от сервера статуса 401, и, нажав последовательно "Back", а затем "Forward" возможно открыть такую страницу, при условии, что требуемые атрибуты авторизации не изменились. Однако, пользователь может нажать клавишу '_' для очистки кеша аутентификации.

Для того, чтобы добиться корректной работы HTTP-аутентификации в IIS сервере с CGI версией PHP, вы должны отредактировать конфигурационную настройку IIS под названием "Directory Security". Щелкните на надписи "Edit" и установите опцию "Anonymous Access", все остальные поля должны остаться неотмеченными.

Замечание: Замечание касательно IIS:
Для того, чтобы HTTP-аутентификация корректно работала в IIS, в конфигурации PHP опция cgi.rfc2616_headers должна быть установлена значением 0 (значение по умолчанию).

Замечание:

В случае, если используется безопасный режим, UID текущего скрипта будет добавлен в realm-часть заголовка WWW-Authenticate.

Коментарии

To get it to work with IIS try using this code before setting your "$auth = 0" and the "if (isset($PHP_AUTH_USER) && isset($PHP_AUTH_PW))"

<?php
//////////////////////////////////////////

if ($PHP_AUTH_USER == "" && $PHP_AUTH_PW == "" && ereg("^Basic "$HTTP_AUTHORIZATION)) 

  list(
$PHP_AUTH_USER$PHP_AUTH_PW) = 
   
explode(":"base64_decode(substr($HTTP_AUTHORIZATION6))); 
}

//////////////////////////////////////////
?>

It worked for me on IIS 5 and PHP 4 in ISAPI
2002-01-29 17:00:50
http://php5.kiev.ua/manual/ru/features.http-auth.html
Some servers won't support the HTTP1.0 specification and will give an error 500 (for instance). This happened with a server where I uploaded an authentication script.

If it happens, you can try the HTTP1.1 header syntax :

<?php
header
("WWW-Authenticate: Basic realm=\"My Realm\"");
header('status: 401 Unauthorized');
?>
2003-01-14 16:14:08
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
Here is a extremely easy way to successfully logout.

<?php
if ( $realm == '' )
$realm mt_rand11000000000 );
   
header'WWW-Authenticate: Basic realm='.$realm ); 
?>

To log the user out simply change the value of $realm
2003-11-08 17:53:58
http://php5.kiev.ua/manual/ru/features.http-auth.html
Regarding HTTP authentication in IIS with the php cgi 4.3.4, there's one more step. I searched mightily and didn't find this information anywhere else, so here goes. When using HTTP auth with the php CGI, you need to do the following things:

1. In your php.ini file, set "cgi.rfc2616_headers = 0"

2. In Web Site Properties -> File/Directory Security -> Anonymous Access dialog box, check the "Anonymous access" checkbox and uncheck any other checkboxes (i.e. uncheck "Basic authentication," "Integrated Windows authentication," and "Digest" if it's enabled.) Click OK.

3. In "Custom Errors", select the range of "401;1" through "401;5" and click the "Set to Default" button.

It's this last step that is crucial, yet not documented anywhere. If you don't, instead of the headers asking for credentials, IIS will return its own fancy but useless 'you are not authenticated' page. But if you do, then the browser will properly ask for credentials, and supply them in the $_SERVER['PHP_AUTH_*'] elements.
2004-03-03 22:47:25
http://php5.kiev.ua/manual/ru/features.http-auth.html
Don't use apache authentification in plain text. Is more better to use own script to generete new ID which is relevant to password. Apache auth data are sent to every page, so the posible mistake are known.
2004-04-27 08:08:50
http://php5.kiev.ua/manual/ru/features.http-auth.html
Quite a good solution for the logout problem:

Just tell browser that it is successfully logged in!
This works as follows:
1. User clicks logout button
2. Script sends 401 Header
3. User does NOT enter a password
4. If no password is entered, script sends 200 Header

So the browser remembers no password as a valid password.

Example:

<?php
if (
       (!isset(
$_SERVER['PHP_AUTH_USER']))
    ||(
           (
$_GET["login"]=="login"
    && !(
             (
$_SERVER['PHP_AUTH_USER']=="validuser"
             && (
$_SERVER['PHP_AUTH_PW']=="validpass")
           )
        )
    ||(
           (
$_GET["logout"]=="logout"
     && !(
$_SERVER['PHP_AUTH_PW']=="")
        )
     ) {
Header("WWW-Authenticate: Basic realm=\"Realm\"");
Header("HTTP/1.0 401 Unauthorized");
echo 
"Not logged out...<br>\n";
echo 
"<a href=\"index.php?login=login\">Login</a>";
exit;
} else if (
$_SERVER['PHP_AUTH_PW']=="") {
echo 
"Logged out...<br>\n";
echo 
"<a href=\"index.php?login=login\">Login</a>";
exit;
}
?>
2004-05-07 07:15:27
http://php5.kiev.ua/manual/ru/features.http-auth.html
In Windows 2003 Server/IIS6 with the php4+ cgi I only get HTTP authentication working with:
<?php header("Status: 401 Access Denied"); ?>
with
<?php header('HTTP/1.0 401 Unauthorized'); ?>
doesn't work !
I also need in "Custom Errors" to select the range of "401;1" through "401;5" and click the "Set to Default" button.
Thanks rob at theblip dot com
2004-05-13 18:33:40
http://php5.kiev.ua/manual/ru/features.http-auth.html
Note that Microsoft has released a 'security update' which disables the use of username:password@host in http urls.

   http://support.microsoft.com/default.aspx?scid=kb;en-us;834489

The methods described above which rely on this will no longer work in Microsoft browsers, sadly.

You can re-enable this functionality as described at

   http://weblogs.asp.net/cumpsd/archive/2004/02/07/69366.aspx

but your users will probably be unwilling to do this.
2004-08-12 19:18:35
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
on the php+mysql auth code by tigran at freenet dot am

There are some security weaknesses.

First
$user 
  and 
$pass

are both insecure, they could leave this code open to SQL injection, you should always remove invalid characters in both, or at least encode them.

Actually storing passwords as MD5 hashes leaves you less work to secure.

Second security risks
The same mysql user has rights to both update and select, and possibly even insert and on your auth database no less.
Again the SQL inject attack may occur with this., and the end user could then change the users username, password, or anything else in relation to this.

Third items is more of a performance issue,
 
Do you really need to update the database, as updates are slower then selects, and if you do them every time they access the page, you are costing some speed penalty. 

One option, if you want to use sql (I think mysql has it) is memory only databases, and create a table within memory, the stores a unique session identifier for each user, that is logged in, or alternatively if it's a single front end system, you could use db files.
2004-08-15 01:12:34
http://php5.kiev.ua/manual/ru/features.http-auth.html
A simpler approach on the post of:
bernard dot paques at bigfoot dot com
24-Sep-2004 01:42

This is another "patch" to the PHP_AUTH_USER and PHP_AUTH_PW server variables problem running PHP as a CGI.

First of all don't forget this fragment of code in your .htaccess (it's the only thing you need to make it work with mod_rewrite):

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
</IfModule>

Then login.php

<?php
$a 
base64_decodesubstr($_SERVER["REMOTE_USER"],6)) ;
if ( (
strlen($a) == 0) || ( strcasecmp($a":" )  == ))
{
   
header'WWW-Authenticate: Basic realm="Private"' );
   
header'HTTP/1.0 401 Unauthorized' );
}
else
{
   list(
$name$password) = explode(':'$a);
   
$_SERVER['PHP_AUTH_USER'] = $name;
   
$_SERVER['PHP_AUTH_PW']    = $password;

}

echo 
'PHP_AUTH_USER =' $_SERVER['PHP_AUTH_USER'] . '<br>';
echo 
'PHP_AUTH_PW =' $_SERVER['PHP_AUTH_PW'] . '<br>';
echo 
'REMOTE_USER =' $_SERVER['REMOTE_USER'] . '<br>';
?>

First, we decode the base64 encoded string discarding the first 6 characters of "Basic " and then we do a regular validation.
At the end of the script we print the variables to verify it's working. This should be ommited in the production version.

It's a variation of the script by Bernard Paques.
Thanks to him for that snippet.
2005-04-29 13:13:44
http://php5.kiev.ua/manual/ru/features.http-auth.html
It forces a auth each time the page is accessed:
(maybe can save someone)

<?
header
("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
header("Pragma: no-cache");
session_cache_limiter("public, no-store");
session_start();

function 
http_auth()
{
   
$_SESSION['AUTH'] = 1;
   
header('HTTP/1.0 401 Unauthorized');
   
header('WWW-Authenticate: Basic realm="sn4g auth system"');
   
// The actions to be done when the user clicks on 'cancel'
   
exit();
}

if( !isset(
$_SERVER['PHP_AUTH_USER']) or @$_SESSION['AUTH'] != )
{
   
http_auth();
    exit();
}

// Actions do be done when the user has logged

// rest, must clean the session array
$_SESSION = array();
session_destroy();
?>
2005-06-18 18:39:58
http://php5.kiev.ua/manual/ru/features.http-auth.html
I came up with another approach to work around the problem of browsers caching WWW authentication credentials and creating logout problems. While most browsers have some kind of way to wipe this information, I prefer having my website to take care of the task instead of relying on the user's sanity.

Even with Lalit's method of creating a random realm name, it was still possible to get back into the protected area using the back button in Firefox, so that didn't work. Here's my solution:

Since browsers attach the credentials to specific URLs, use virtual paths where a component of the path is actually a PHP script, and everything following it is part of the URI, such as:

http://www.example.com/some_dir/login.php/auth/8f631b92/

By choosing a different number for the last component of the URL, browsers can be tricked into thinking that they are dealing with a completely different website, and thus prompting the user for credentials again.

Note that using a random, unrestricted number will still allow the user to hit the back button to get back into the page. You should keep track of this number in a server-side file or database and regenerate it upon each successful login, so that the last number(s) become invalid. Using an invalid number might result in a 403 response or, depending on how you feel that day, a 302 to a nasty website.

Care should be taken when linking from the page generated in this case, since relative links will be relative to the virtual and non-existant directory rather than the true script directory.

Hope this helps somebody.
2005-08-24 12:11:05
http://php5.kiev.ua/manual/ru/features.http-auth.html
I suggest to demand user's authentication and management to the web server (by .htaccess, ...):

1. configure a global /logon/ directory with a .htaccess file restricted access

2. use fopen wrapper:

  $hh = @fopen("http://{$_SERVER['PHP_AUTH_USER']}:{$_SERVER['PHP_AUTH_PW']}".
    @{$_SERVER['SERVER_NAME']}/logon/", "r");
  if (!$hh) authenticate(); // usual header WWW-Authenticate ...
  fclose($hh);
2006-01-09 08:29:31
http://php5.kiev.ua/manual/ru/features.http-auth.html
Well, I think it's easy to make authentification works correctly. I use a session var to force authentication everytime a user visit the logging area.

<?php
if (!isset ($_SESSION['firstauthenticate'])) {
   
session_start();
}
  function 
authenticate() {
   
header('WWW-Authenticate: Basic realm="Sistema autentificaci?n UnoAutoSur"');
   
header('HTTP/1_0 401 Unauthorized');
//    header("Status: 401 Access Denied"); 
   
echo "Unauthorized\n";
    exit;
  }
 if (!isset(
$_SERVER['PHP_AUTH_USER']) || strcmp ($_SERVER['PHP_AUTH_USER'],$user)!=|| 
      !isset (
$_SERVER['PHP_AUTH_PW']) || strcmp($_SERVER['PHP_AUTH_PW'],$pass)!=|| !isset ($_SESSION['firstauthenticate']) || !$_SESSION['firstauthenticate']) {
     
$_SESSION['firstauthenticate']=true;
   
authenticate();
 } else {
           
//I destroy the session var now
   
session_unset();
           
//Your code below
 
}
?>
2006-02-15 04:14:18
http://php5.kiev.ua/manual/ru/features.http-auth.html
# PHP (CGI mode) HTTP Authorization with ModRewrite: 
# most right example with header check for non empty:
 RewriteEngine on
 RewriteCond %{HTTP:Authorization}  !^$
 RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}, \
E=PHP_AUTH_USER:%{HTTP:Authorization},L]
2006-05-23 11:06:16
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
I couldn't get authentication to work properly with any of the examples. Finally, I started from ZEND's tutorial example at:
http://www.zend.com/zend/tut/authentication.php?article=authentication (validate using .htpasswd) and tried to deal with the additional cases. My general conclusion is that changing the realm is the only reliable way to cause the browser to ask again, and I like to thank the person who put that example in the manual, as it got me on the right path. No matter what, the browser refuses to discard the values that it already has in mind otherwise. The problem with changing the realm, of course, is that you don't want to do it within a given session, else it causes a new request for a password. So, here goes, hopefully the spacing isn't too messed up by the cut'n'paste.

I spent the better part of a day getting this to work right. I had a very hard time thinking through what the browser does when it encounters an authentication request: seems to me that it tries to get the password, then reloads the page... so the HTML doesn't get run. At least, this was the case with IE, I haven't tested it with anything else.

<?php
session_start
() ;
if (!isset(
$_SESSION['realm'])) {
       
$_SESSION['realm'] = mt_rand11000000000 ).
               
" SECOND level: Enter your !!!COMPANY!!! password.";

       
header"WWW-Authenticate: Basic realm=".$_SESSION['realm'] );

       
//  Below here runs HTML-wise only if there isn't a $_SESSION,
        // and the browser *can't* set $PHP_AUTH_USER... normally
        // the browser, having gotten the auth info, runs the page
        // again without getting here.
        //  What I'm basically getting to is that the way to get
        // here is to escape past the login screen. I tried
        // putting a session_destroy() here originally, but the
        // problem is that the PHP runs regardless, so the
        // REFRESH seems like the best way to deal with it.
       
echo "<meta http-equiv=\"REFRESH\"
                content=\"0;url=index.php\">" 
;
        exit;
        }

if (
$_POST['logout'] == "logout") {
       
session_destroy() ;
       
header('Location: comeagain.php');
        exit ;
        }

// "standard" authentication code here, from the ZEND tutorial above.

comeagain.php is as follows:

<?
session_start();
unset(
$_SESSION['realm']);
session_destroy();
echo 
"<html><head><title>Logged Out</title><h1>Logout Page</h1><body>" ;
echo 
"You have successfully logged out of TOGEN";
echo 
" at ".date("h:m:s")." on ".date("d F Y") ;
echo 
"<p><a href=\"index.php\">Login Again</a>" ;
echo 
"</body></html>" ;
?>

The idea is to be able to trash the session (and thus reset the realm) without prompting the browser to ask again... because it has been redirected to logout.php.

With this combination, I get things to work. Just make sure not to have apache run htpasswd authentication at the same time, then things get really weird :-).
2006-06-03 18:51:08
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
While Digest authentication is still far superior to Basic authentication, there are a number of security issues that one must keep in mind.

In this respect, the Digest example given above is somewhat flawed, because the nonce never times out or otherwise become invalid. It thus becomes a password-equivalent (although to that specific URL only) and can be used by an eavesdropper to fetch the page at any time in the future, thus allowing the attacker to always access the latest version of the page, or (much worse) repeatedly invoke a CGI script -- for instance, if the user requests the URL "/filemanager?delete=somefile", the attacker can repeat this deletion at any point in the future, possibly after the file has been recreated.

And while it might not be possible to change GET data without reauthentication, cookies and POST data *can* be changed.

To protect against the first problem, the nonce can be made to include a timestamp, and a check added to ensure that nonces older than e.g. 30 minutes result in a new authentication request. 

To solve the second problem, a one-time only nonce needs to be generated -- that is, all further requests using a particular nonce must be refused.

One way to do this: When the user requests an action such as "deletefile", store a randomly generated nonce in a session variable, issue a 401 authentication challenge with that nonce, and then check against the stored value when receiving the authentication (and clear the session variable).

This way, although a possible eavesdropper receives the nonce and thus gains the ability to perform the action, he can only perform it once -- and the user was going to perform it anyway. (Only the user or the attacker, but not both, gets to perform the action, so it's safe.)

Of course, at some point, the security can only be improved by switching to HTTPS / SSL / TLS (this is for instance the only way to defend against man-in-the-middle attacks). You decide the level of security.
2006-07-12 13:23:18
http://php5.kiev.ua/manual/ru/features.http-auth.html
For PHP with CGI, make sure you put the rewrite rule above any other rewrite rule you might have.

In my case, I put this at the top of the .htaccess (below RewriteEngine On):
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]

My symptom was that the REMOTE_USER (or REDIRECT_REMOTE_USER in my case) was not being set at all.
The cause: I had some other RewriteRule that was kickin in and was set as LAST rule.
I hope this helps.
2006-10-10 23:12:42
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
I used Louis example (03-Jun-2006) and it works well for me (thanks).

However, I added some lines, to make sure, the user does only get the Authentification-Window a few times:

<?php
$realm 
mt_rand11000000000)."@YourCompany";
$_SESSION['realm'] = $realm;

// In the beginning, when the realm ist defined:
$_SESSION['CountTrials'] = 1;
?>

And then when it comes to check the authentification (ZEND-Tutorial):

<?php

// Not more than 3 Trials
if (!$auth) {
   
$_SESSION['CountTrials']++;
   if (
$_SESSION['CountTrials'] == 4) {   
       
session_destroy() ;
       
header('Location: noentry.php');
       exit ;   
   } else {
       
header("WWW-Authenticate: Basic realm=".$_SESSION['realm']);
       
header("HTTP/1.0 401 Unauthorized");
       echo 
'Authorization Required.';
       exit;
   }
} else {
         echo 
'<P>You are authorized!</P>';
}
?>

noentry.php is slightely different from comeagain.php.
2006-10-24 02:28:00
http://php5.kiev.ua/manual/ru/features.http-auth.html
There are .htaccess which actually works for us (cPanel + phpsuexec) unless others failed. Perhaps it may help someone.

# PHP (CGI mode) HTTP Authorization with ModRewrite:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

Then you need small piece of php code to parse this line and then everything will work like with mod_php:

if (isset($_SERVER['HTTP_AUTHORIZATION']))
{
$ha = base64_decode( substr($_SERVER['HTTP_AUTHORIZATION'],6) );
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', $ha);
unset $ha;
}

Enjoy!
2006-11-01 08:21:24
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
Back to the problem of authenticating in CGI mode... mcbethh suggested using this to set a local variable in php:
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]

It didn't work. I couldn't see the variable. My solution is pretty round-about, but it works:

RewriteEngine on 
RewriteCond %{HTTP:Authorization} !^$ 
RewriteCond %{REQUEST_METHOD} =GET 
RewriteCond %{QUERY_STRING} ="" 
RewriteRule ^page.php$ page.php?login=%{HTTP:Authorization}$1 

This causes the Auth string to be added to the URL if there are no parameters and it's a GET request. This prevents POSTs and parameter lists from being corrupted. 

Then, in the PHP script, I store the Auth string as a session cookie.

So the only way to log in to my script is to go to the url with no parameters.
2006-11-10 09:05:21
http://php5.kiev.ua/manual/ru/features.http-auth.html
This is the simplest form I found to do a Basic authorization with retries.

<?php

$valid_passwords 
= array ("mario" => "carbonell");
$valid_users array_keys($valid_passwords);

$user $_SERVER['PHP_AUTH_USER'];
$pass $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user$valid_users)) && ($pass == $valid_passwords[$user]);

if (!
$validated) {
 
header('WWW-Authenticate: Basic realm="My Realm"');
 
header('HTTP/1.0 401 Unauthorized');
  die (
"Not authorized");
}

// If arrives here, is a valid user.
echo "<p>Welcome $user.</p>";
echo 
"<p>Congratulation, you are into the system.</p>";

?>
2007-02-20 18:53:57
http://php5.kiev.ua/manual/ru/features.http-auth.html
Be careful using http digest authentication (see above, example 34.2) if you have to use the 'setlocale' function *before* validating response with the 'http_digest_parse' function, because there's a conflict with \w in the pattern of 'preg_match_all' function :

In fact, as \w is supposed to be any letter or digit or the underscore character, you must not forgot that this may vary depending on your locale configuration (eg. it accepts accented letters in french)...

Due to this different pattern interpretation by the 'preg_match_all' function, the 'http_digest_parse' function will always return a false result if you have modified your locale (I mean if your locale accepts some extended characters, see http://fr.php.net/manual/en/reference.pcre.pattern.syntax.php for further information).

IMHO, I suggest you not to use setlocale before having your authentication completed...

PS : Here's a non-compatible setlocale declaration...
setlocale ( LC_ALL, 'fr_FR', 'fr', 'FR', 'french', 'fra', 'france', 'French', 'fr_FR.ISO8859-1' ) ;
2007-03-05 11:37:02
http://php5.kiev.ua/manual/ru/features.http-auth.html
Back to the autherisation in CGI mode. this is the full working example:

#  Create the .htaccess file with following contents:
# also you can use the condition (search at this page)
RewriteEngine on
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]

# In the beginning the script checking the authorization place the code:

$userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"],6)) ;

$userpass = explode(":", $userpass);

if (  count($userpass) == 2  ){
     #this part work not for all.
     #print_r($userpass);die; #<- this can help find out right username and password
     list($name, $password) = explode(':', $userpass);
     $_SERVER['PHP_AUTH_USER'] = $name;
     $_SERVER['PHP_AUTH_PW'] = $password;

}
2007-07-27 02:48:49
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
To anybody who tried the digest example above and didn't get it to work.

For me the problem seemed to be the deprecated use of '\' (backslash) in the regex instead of the '$' (Dollar) to indicate a backreference. Also the results have to be trimmed off the remaining double and single quotes. 

Here's the working example:

// function to parse the http auth header
function http_digest_parse($txt)
{
   
    // protect against missing data
    $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
    $data = array();

    preg_match_all('@(\w+)=(?:([\'"])([^$2]+)$2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
   
    foreach ($matches as $m) {
        $data[$m[1]] = $m[3] ? trim($m[3],"\",'") : trim($m[4],"\",'"); 
        unset($needed_parts[$m[1]]);
    }
   
    return $needed_parts ? false : $data;
}

Probably there's a more sophisticated way to trim the quotes within the regex, but I couldn't be bothered :-)

Greets, Lars
2008-02-12 12:23:34
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
Good day.
Sorry for my english.
This example shows programming "LOGIN", "LOGOUT" and "RE-LOGIN".
This script must use in the protected pages.
For work this script the browser address string must be following:
"http://localhost/admin/?login" - for Login,
"http://localhost/admin/?logout" - for Logout,
"http://localhost/admin/?logout&login" - for Re-Login.
<?php
session_start
();

$authorized false;

# LOGOUT
if (isset($_GET['logout']) && !isset($_GET["login"]) && isset($_SESSION['auth']))
{
   
$_SESSION = array();
    unset(
$_COOKIE[session_name()]);
   
session_destroy();
    echo 
"logging out...";
}

# checkup login and password
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{
   
$user 'test';
   
$pass 'test';
    if ((
$user == $_SERVER['PHP_AUTH_USER']) && ($pass == ($_SERVER['PHP_AUTH_PW'])) && isset($_SESSION['auth']))
    {
   
$authorized true;
    }
}

# login
if (isset($_GET["login"]) && !$authorized ||
# relogin
   
isset($_GET["login"]) && isset($_GET["logout"]) && !isset($_SESSION['reauth']))
{
   
header('WWW-Authenticate: Basic Realm="Login please"');
   
header('HTTP/1.0 401 Unauthorized');
   
$_SESSION['auth'] = true;
   
$_SESSION['reauth'] = true;
    echo 
"Login now or forever hold your clicks...";
    exit;
}
$_SESSION['reauth'] = null;
?>
<h1>you have <? echo ($authorized) ? (isset($_GET["login"]) && isset($_GET["logout"]) ? 're' '') : 'not '?>logged!</h1>
2009-02-15 08:11:44
http://php5.kiev.ua/manual/ru/features.http-auth.html
<?php

// try to mimic cpanel logout style
// only diff is usr & pwd field is cleared when re-login
// tested with ff2 & ie8

session_start();

$username "test";
$password "test";

if(isset(
$_GET['logout']))
{
  unset(
$_SESSION["login"]);
  echo 
"You have logout ... ";
  echo 
"[<a href='" $_SERVER['PHP_SELF'] . "'>Login</a>]";
  exit;
}

if (!isset(
$_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"]))
{
 
header("WWW-Authenticate: Basic realm=\"Test\"");
 
header("HTTP/1.0 401 Unauthorized");
 
$_SESSION["login"] = true;
  echo 
"You are unauthorized ... ";
  echo 
"[<a href='" $_SERVER['PHP_SELF'] . "'>Login</a>]";
  exit;
}
else
{
  if(
$_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password)
  {
    echo 
"You have logged in ... ";
    echo 
"[<a href='" $_SERVER['PHP_SELF'] . "?logout'>Logout</a>]";
  }
  else
  {
    unset(
$_SESSION["login"]);
   
header("Location: " $_SERVER['PHP_SELF']);
  }
}

// content here

?>
2009-09-06 15:34:39
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
The regex in http_digest_parse from Example #2 does not work for me (PHP 5.2.6), because back references are not allowed in a character class.  This worked for me:

<?php

// function to parse the http auth header
function http_digest_parse($txt)
{
   
// protect against missing data
   
$needed_parts = array('nonce'=>1'nc'=>1'cnonce'=>1'qop'=>1'username'=>1'uri'=>1'response'=>1);
   
$data = array();

   
preg_match_all('@(\w+)=(?:(?:\'([^\']+)\'|"([^"]+)")|([^\s,]+))@'$txt$matchesPREG_SET_ORDER);

   foreach (
$matches as $m) {
       
$data[$m[1]] = $m[2] ? $m[2] : ($m[3] ? $m[3] : $m[4]);
       unset(
$needed_parts[$m[1]]);
   }

   return 
$needed_parts false $data;
}

?>
2009-09-09 13:02:06
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
a link to http://logout:logout@<?=$_SERVER['HTTP_HOST'];?>/SECRET/ would force a fresh login for the /SECRET directory if no user logout with password logout exists.


[NOTE BY danbrown AT php DOT net: The following note was added by "Anonymous" on 01-APR-2010 (though we presume it's not an April Fool's Day joke).]

this logout method does not work 100% anymore, because of another bulls**t from M$:
http://support.microsoft.com/kb/834489
2010-03-25 05:04:09
http://php5.kiev.ua/manual/ru/features.http-auth.html
First of all, sorry for my English.
One more authorization script with logout solution.
In script given by meint_at_meint_dot_net (features.http-auth#93859) rewrite_module is used. If there are any problems with that module and possibilities of administration of the web-server are restricted (including restrictions for use of .htaccess) then you can use this solution.

index.php
---------

<?php

$auth_realm 
'My realm';

require_once 
'auth.php';

echo 
"You've logged in as {$_SESSION['username']}<br>";
echo 
'<p><a href="?action=logOut">LogOut</a></p>'

?>

auth.php
--------

<?php

$_user_ 
'test';
$_password_ 'test';

session_start();

$url_action = (empty($_REQUEST['action'])) ? 'logIn' $_REQUEST['action'];
$auth_realm = (isset($auth_realm)) ? $auth_realm '';

if (isset(
$url_action)) {
    if (
is_callable($url_action)) {
       
call_user_func($url_action);
    } else {
        echo 
'Function does not exist, request terminated';
    };
};

function 
logIn() {
    global 
$auth_realm;

    if (!isset(
$_SESSION['username'])) {
        if (!isset(
$_SESSION['login'])) {
           
$_SESSION['login'] = TRUE;
           
header('WWW-Authenticate: Basic realm="'.$auth_realm.'"');
           
header('HTTP/1.0 401 Unauthorized');
            echo 
'You must enter a valid login and password';
            echo 
'<p><a href="?action=logOut">Try again</a></p>';
            exit;
        } else {
           
$user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
           
$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
           
$result authenticate($user$password);
            if (
$result == 0) {
               
$_SESSION['username'] = $user;
            } else {
               
session_unset($_SESSION['login']);
               
errMes($result);
                echo 
'<p><a href="">Try again</a></p>';
                exit;
            };
        };
    };
}

function 
authenticate($user$password) {
    global 
$_user_;
    global 
$_password_;

    if ((
$user == $_user_)&&($password == $_password_)) { return 0; }
    else { return 
1; };
}

function 
errMes($errno) {
    switch (
$errno) {
        case 
0:
            break;
        case 
1:
            echo 
'The username or password you entered is incorrect';
            break;
        default:
            echo 
'Unknown error';
    };
}

function 
logOut() {

   
session_destroy();
    if (isset(
$_SESSION['username'])) {
       
session_unset($_SESSION['username']);
        echo 
"You've successfully logged out<br>";
        echo 
'<p><a href="?action=logIn">LogIn</a></p>';
    } else {
       
header("Location: ?action=logIn"TRUE301);
    };
    if (isset(
$_SESSION['login'])) { session_unset($_SESSION['login']); };
    exit;
}

?>
2010-08-12 05:31:18
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
I tried example 7, and at first I couldn't get it to work. It took me a while to spot that somewhere along the line, probably by the server, a seemingly random number was being added to the realm - so the valid_result variable wasn't calculated using the correct realm.

To get around this, or any similar problems, make the following changes to the example:

Around line 43 (44 if after next step ;) ):
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1, 'realm'=>1);

Before line 24:
$realm = $data['realm'];

These two steps get the real realm used for the authentication request, and substitute it into the "valid_response" query.

Hope this helps :)
2010-09-24 19:16:51
http://php5.kiev.ua/manual/ru/features.http-auth.html
To force a logout with Basic Auth, you can change the Realm out from under them to a different Realm.

This forces a new set of credentials for a new "Realm" on your server.

You just need to track the Realm name with the user/pass and change it around to something new/random as they log in and out.

I believe that this is the only 100% guaranteed way to get a logout in HTTP Basic Auth, and if it were part of the docs a whole lot of BAD user-contributed comments here could be deleted.
2010-10-13 06:51:35
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
On my configuration with php-cgi - after setting the RewriteRule - the correct variable would be: $_SERVER['REDIRECT_HTTP_AUTHORIZATION']

So the workaround for PhpCGI is:
Set in your .htaccess:

RewriteEngine on
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

Php workaround:
<?php
//set http auth headers for apache+php-cgi work around
if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i'$_SERVER['HTTP_AUTHORIZATION'], $matches)) {
    list(
$name$password) = explode(':'base64_decode($matches[1]));
   
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
   
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
}

//set http auth headers for apache+php-cgi work around if variable gets renamed by apache
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i'$_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches)) {
    list(
$name$password) = explode(':'base64_decode($matches[1]));
   
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
   
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
}
?>
2011-10-24 20:32:26
http://php5.kiev.ua/manual/ru/features.http-auth.html
You shouldn't use the "last" ("L") directive in the RewriteRule! This will prevent all further rewrite rules to be skipped whenever a Basic or Digest Auth is given, which is almost certainly not what you want.

So the following lines are sufficient for the .htaccess (or httpd.conf) file:

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
2012-02-23 13:54:25
http://php5.kiev.ua/manual/ru/features.http-auth.html
In case of CGI/FastCGI you would hot be able to access PHP_AUTH* info because CGI protocol does not declare such variables (that is why their names start from PHP) and server would not pass them to the interpreter. In CGI server should authenticate user itself and pass REMOTE_USER to CGI script after it. 

So you need to "fetch" request headers and pass them to your script somehow. 

In apache you can do it via environment variables if mod_env is installed. 

Following construction in .htaccess copies request header "Authorization" to the env variable PHP_AUTH_DIGEST_RAW

SetEnvIfNoCase ^Authorization$ "(.+)" PHP_AUTH_DIGEST_RAW=$1

You can now access it via $_ENV.

Do not forget to strip auth type ("Digest" in my case) from your env variable because PHP_AUTH_DIGEST does not have it.

If mod_env is not installed you probably have mod_rewrite (everyone has it because of "human readable URLs").

You can fetch header and pass it as GET parameter using rewrite rule: 

RewriteRule ^.*$ site.php?PHP_AUTH_DIGEST_RAW=%{HTTP:Authorization} [NC,L]

Here HTTP request header Authorization would be acessible as PHP_AUTH_DIGEST_RAW via $_GET.

---
If you use ZF you probably use Zend_Auth_Adapter_Http to auth user. 

It takes Authorization info using "Zend_Controller_Request::getHeader"
This method uses apache_request_header which is likely not to be accessible in old CGI/FastCGI installations or _$_SERVER['HTTP_<HeaderName>] , so you need to put your authentication data, obtained via _GET or ENV to 
_$_SERVER['HTTP_AUTHORIZATION']. 
It will make ZF work transparently with you solution and I believe any other framework should work also
2012-03-30 23:20:22
http://php5.kiev.ua/manual/ru/features.http-auth.html
New auth:

<?php
$login 
'test_login';
$pass 'test_pass';

if((
$_SERVER['PHP_AUTH_PW']!= $pass || $_SERVER['PHP_AUTH_USER'] != $login)|| !$_SERVER['PHP_AUTH_USER'])
{
   
header('WWW-Authenticate: Basic realm="Test auth"');
   
header('HTTP/1.0 401 Unauthorized');
    echo 
'Auth failed';
    exit;
}
?>
2012-10-02 12:32:14
http://php5.kiev.ua/manual/ru/features.http-auth.html
In the german example #2 (digest), the <?php $realm "Geschützter Bereich"?>. As far as I have tested the umlaut ü is problematic, resulting in an password enter infinity loop. In my case it was written in UTF-8. So I suggest using only plain ASCII characters for the realm.
2014-01-09 18:55:39
http://php5.kiev.ua/manual/ru/features.http-auth.html
Workaround for missing Authorization header under CGI/FastCGI Apache:

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0

Now PHP should automatically declare $_SERVER[PHP_AUTH_*] variables if the client sends the Authorization header.
2014-04-19 18:55:25
http://php5.kiev.ua/manual/ru/features.http-auth.html
The only effective way I've found to wipe out the PHP_AUTH_DIGEST or PHP_AUTH_USER AND PHP_AUTH_PW credentials is to call the header HTTP/1.1 401 Unauthorized.

function clear_admin_access(){
    header('HTTP/1.1 401 Unauthorized');
    die('Admin access turned off');
}
2015-06-07 19:01:03
http://php5.kiev.ua/manual/ru/features.http-auth.html
I came up with another approach to work around the problem of browsers caching WWW authentication credentials and creating logout problems. While most browsers have some kind of way to wipe this information, I prefer having my website to take care of the task instead of relying on the user's sanity.

Even with Lalit's method of creating a random realm name, it was still possible to get back into the protected area using the back button in Firefox, so that didn't work. Here's my solution:

Since browsers attach the credentials to specific URLs, use virtual paths where a component of the path is actually a PHP script, and everything following it is part of the URI, such as:

http://velocitypress.ca/some_dir/login.php/auth/8f631b92/

By choosing a different number for the last component of the URL, browsers can be tricked into thinking that they are dealing with a completely different website, and thus prompting the user for credentials again.

Note that using a random, unrestricted number will still allow the user to hit the back button to get back into the page. You should keep track of this number in a server-side file or database and regenerate it upon each successful login, so that the last number(s) become invalid. Using an invalid number might result in a 403 response or, depending on how you feel that day, a 302 to a nasty website.

Care should be taken when linking from the page generated in this case, since relative links will be relative to the virtual and non-existant directory rather than the true script directory.
2015-10-02 01:42:54
http://php5.kiev.ua/manual/ru/features.http-auth.html
A simple script for SSL Client Certificate authentication with a basic authentication fall-back. I use this on my site using LDAP server to check username/passwords and client certificate to user mapping.

<? 
// Check if and how we are authenticated
if ($_SERVER['SSL_CLIENT_VERIFY'] != "SUCCESS") { // Not using a client certificate
   
if ((!$_SERVER['PHP_AUTH_USER']) && (!$_SERVER['PHP_AUTH_PW'])) { // Not logged in using basic authentication
       
authenticate(); // Send basic authentication headers
   
}
}

if (
$_SERVER['SSL_CLIENT_S_DN_CN'] != "chris") { // Check CN name of cert

   
if (!(($_SERVER['PHP_AUTH_USER'] == "test") && ($_SERVER['PHP_AUTH_PW'] == "123"))) { // Check username and password
       
authenticate(); // Send basic authentication headers because username and/or password didnot match
   



phpinfo();

// Call authentication display
function authenticate() {
   
Header("WWW-Authenticate: Basic realm=Website");
       
Header("HTTP/1.0 401 Unauthorized");
       
error401();
        exit;
}
?>

See my website (http://velocitypress.ca/index.php?page=/manuals/) for more details on client certificate with Apache and PHP.
2015-10-02 01:45:01
http://php5.kiev.ua/manual/ru/features.http-auth.html
Here is my attempt to create a digest authentication class that will log the user in and out without using a cookie,session,db,or file. At the core is this simple code to parse the digest string into variables works for several browsers.
<?php
// explode the digest with multibrowser support by Tony Wyatt 21jun07
public function explodethedigest($instring) {
$quote '"';
$equal '=';
$comma ',';
$space ' ';
$a explode$comma$instring);
$ax explode($space$a[0]);
$b explode$equal$ax[1], 2);
$c explode$equal$a[1], 2);
$d explode$equal$a[2], 2);
$e explode$equal$a[3], 2);
$f explode$equal$a[4], 2);
$g explode$equal$a[5], 2);
$h explode$equal$a[6], 2);
$i explode$equal$a[7], 2);
$j explode$equal$a[8], 2);
$k explode$equal$a[9], 2);
$l explode$equal$a[10], 2);
$parts = array(trim($b[0])=>trim($b[1], '"'), trim($c[0])=>trim($c[1], '"'), trim($d[0])=>trim($d[1], '"'), trim($e[0])=>trim($e[1], '"'), trim($f[0])=>trim($f[1], '"'), trim($g[0])=>trim($g[1], '"'), trim($h[0])=>trim($h[1], '"'), trim($i[0])=>trim($i[1], '"'), trim($j[0])=>trim($j[1], '"'), trim($k[0])=>trim($k[1], '"'), trim($l[0])=>trim($l[1], '"'));

return 
$parts;
}
?>
Give it a try at http://www.creativetheory.ca/ /tests/ta1.php Log in with user test password pass or user guest password guest. Go to page two for links to the code. Comments, ideas, suggestions, or critique welcome.
2015-11-22 21:05:02
http://php5.kiev.ua/manual/ru/features.http-auth.html
Автор:
Simpler WorkAround for missing Authorization header under CGI/FastCGI available in Apache HTTP Server 2.4.13 and later

     CGIPassAuth On

Please don't enable Authorization header with Basic Authentication, is very insecure.
2017-11-22 12:44:59
http://php5.kiev.ua/manual/ru/features.http-auth.html
Samples Login / Logout script

login.php :

<?php

   
// Initisalition
   
if ($_COOKIE["SESSION"]=='') {
       
setcookie("SESSION"'AUTH'0,'/'); 
       
   
// Identification perdu (time-out ou logoff)
   
} else if ($_SERVER['PHP_AUTH_USER']!='' && $_COOKIE["USER_SESSION"]=='') {
       
$_SERVER['PHP_AUTH_USER'] = '';
    }

   
// Controle identification à la base 
   
$ident executeSQL("SELECT * FROM UTILISATEURS WHERE upper(IDENTIFIANT)=Upper('".$_SERVER['PHP_AUTH_USER']."')");
    if (
$_SERVER['PHP_AUTH_USER']!='' && strtoupper($ident['IDENTIFIANT'])==strtoupper($_SERVER['PHP_AUTH_USER']) && $ident['MOT_DE_PASSE']==$_SERVER['PHP_AUTH_PW']) {
       
$user $_SERVER['PHP_AUTH_USER'];
       
setcookie("USER_SESSION"$usertime()+300,'/'); // 5 Minutes !
   
    // Mot de passe incorecte : demande identification
   
} else {
       
setcookie("SESSION"''1,'/'); 
       
header('WWW-Authenticate: Basic realm="My Realm"');
       
header('HTTP/1.0 401 Unauthorized');
        die;
    }
   
?>
<html>
    <body >

        Bonjour <?php echo $ident['NOM'].' '.$ident['PRENOM']; ?>
        <br>
        <br>
        <a href="http://logout.php">Deconnexion</a>
    </body>
</html>

logout.php :

<?php
setcookie
("USER_SESSION"''1,'/');
header('Location: http://login.php');
?>
2020-06-17 15:12:47
http://php5.kiev.ua/manual/ru/features.http-auth.html
the alternative text should contain the complete text af a (small) valid HTML-Ressource.  It also can contain link relations to CSS.
2021-04-22 15:14:09
http://php5.kiev.ua/manual/ru/features.http-auth.html
public function loginUser(){
        $username = filter_var($_GET['username'],FILTER_SANITIZE_STRING);
        $pw = filter_var($_GET['pw'],FILTER_SANITIZE_STRING);
        $row = $this->userModel->selectUser($username);

       if (password_verify($pw,$row['pw'])){
           $_SESSION["userId"] = $row['id'];

           $this->isLogin();
       } else{
           new Msg(true,"Benutzername und Passwort stimmen nicht überein!");
       }

    }

A short example for an user Login
2021-05-16 20:35:52
http://php5.kiev.ua/manual/ru/features.http-auth.html

    Поддержать сайт на родительском проекте КГБ