>백엔드 개발 >PHP 튜토리얼 >CDN a,b,c三种鉴权的PHP代码

CDN a,b,c三种鉴权的PHP代码

WBOY
WBOY원래의
2016-06-23 13:26:011343검색

A鉴权方式的代码

//http://DomainName/Filename?auth_key=timestamp-rand-uid-md5hash

//sstring = "URI-Timestamp-rand-uid-PrivateKey" (URI是用户的请求对象相对地址, 如 /Filename)

//HashValue = md5sum(sstring)

function PrivateKeyA(){

                  $time=strtotime("+8 hours");

                  $key="youkey";

                  $domain="http://www.a.com";

                  $filename="/mulu/1.jpg";

                  //$sstring = "URI-Timestamp-rand-uid-PrivateKey"

                  $sstring = $filename."-".$time."-0-0-".$key;

                  $md5=md5($sstring);

                  $auth_key="auth_key=".$time."-0-0-".$md5;

                  $url=$domain.$filename."?".$auth_key;

                  echo $url."\n";

}


B鉴权方式的代码

//http://DomainName/timestamp/md5hash/FileName

//timestamp:用户访问时客户源服务器的时间,作为URL的一部分,同时作为计算 md5hash 的一个因子,格式为: YYYYMMDDHHMM ,有效时间1800s

//md5hash:以timestamp、FileName和预先设定好的 PrivateKey 共同做MD5获得的字符串,即 md5(PrivateKey + timestamp + FileName)

function PrivateKeyB(){

                  $time=date("YmdHi");

                  $key="youkey";

                  $domain="http://www.a.com/";

                  $filename="/mulu/1.jpg";

                  //$sstring = "PrivateKeytimestampFileName"

                  $sstring = $key.$time.$filename;

                  $md5=md5($sstring);

                  $url=$domain.$time."/".$md5.$filename;

                  echo $url."\n";

}


C鉴权方式的代码:

//http://DomainName/{/}/FileName

//md5hash = md5sum()

//time: 用户访问源服务器时间,取 UNIX 时间,以十六进制数字字符表示。

function PrivateKeyC(){

                  $time2=dechex(time());

                  $key="youkey";

                  $domain="http://ww.a.com/";

                  $filename="/mulu/1.jpg";

                  //$sstring=PrivateKeyFileNametime

                  $sstring=$key.$filename.$time2;

                  $md5=md5($sstring);

                  $url=$domain.$md5."/".$time2.$filename;  

                  echo $url."\n";

}


鉴权错误都是返回 403 

Md5计算类错误:

X-Tengine-Error:denied by req auth: invalid md5hash=de7bfdc915ced05e17380a149bd760be

时间类报错:

X-Tengine-Error:denied by req auth: expired timestamp=1439469547


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.