Heim >Backend-Entwicklung >PHP-Tutorial >CDN a,b,c三种鉴权的PHP代码
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