在做項目的時候用的401認證,今天總結下來,分享給大家,用的到的可以看下:
代碼如下:
<?php
$user = "test";
$pw = "mytest";
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_USER'])) {
if($_SERVER['PHP_AUTH_USER'] != $user || $_SERVER['PHP_AUTH_PW'] != $pw){
header('WWW-Authenticate: Basic realm="My control panel"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
}else{
echo "Hi {$_SERVER['PHP_AUTH_USER']}.";
echo "Please entered {$_SERVER['PHP_AUTH_PW']} as your password.";
}
*