PHP 开启Authorization Header验证,及访问方法

使用Authorization 做登录验证

<?php
     $authorization = false;
     if($_SERVER['PHP_AUTH_USER'] == "admin" && $_SERVER['PHP_AUTH_PW'] == "admin888"){
         echo "login";
         $authorization = true;
         exit;
     }
     if(!$authorization){
        header("WWW-Authenticate:Basic realm='Private'");
        header('HTTP/1.0 401 Unauthorized');
        print "You are unauthorized to enter this area.";
     }

如何访问

1:浏览器访问,手动输入账号密码  
2:http://admin:admin888@test.yiihua.com/tool/test4.php
3:也可以使用 header 头存储密码 Authorization: Basic base64(帐号:密码)
例子 :

<?php
    function http_post($sUrl,$aHeader, $aData){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aData));
        $sResult = curl_exec($ch);
        if($sError=curl_error($ch)){
            die($sError);
        }
        curl_close($ch);
        return $sResult;
    }
    $url = 'http://test.yiihua.com/tool/test4.php';
    $header = array("Authorization: Basic ".base64_encode('admin:admin888'));
    $data = array(123);
    $data = http_post($url,$header,$data);
    var_dump($data);     

“PHP 开启Authorization Header验证,及访问方法”的2个回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注