edit-icon download-icon

PHP Code for Authentication Method A, Method B, and Method C of Alibaba Cloud CDN

Last Updated: Jul 13, 2016


1  browse

PHP code for Authentication Method A

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

//sstring = "URI-Timestamp-rand-uid-PrivateKey" (The URI is the relative address of the object requested by the user, such as "/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";

}


PHP code for Authentication Method B

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

//timestamp: specifies the time when a user accesses the origin site server. It is part of the URL and also a factor used to calculate "md5hash". The format is YYYYMMDDHHMM. The validity period is 1,800s

//md5hash: The "timestamp", "FileName", and preset "PrivateKey" are used in the MD5 algorithm to get this string, i.e., "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";

}


PHP code for Authentication Method C

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

//md5hash = md5sum(

//time: specifies the time when a user accesses the origin site server. It is UNIX time expressed with a hexadecimal value.

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";

}


Status code 403 is returned for all authentication errors 

MD5 calculation error:

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

Time error:

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


If the problem persists, contact Alibaba Cloud After-Sales Technical Support.

 


Thank you! We've received your feedback.