Urgent help - Token Generation code!
| From: | Shreyas Agasthya | Date: | Fri, 27 May 2011 22:26:26 +0000 |
| Subject: | Urgent help - Token Generation code! | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-313175@lists.php.net to get a copy of this message | ||
I am re-visiting the world of PHP after a really big hiatus and I am finding
things veryslippery. Can someone please help me with the below code and let
me know how I can print the token that is getting generated?
I am using EasyPHP and I am trying to echo the $token but it wouldn't print
anything. I am trying it as : http://localhost/token/URLToken.php. May I
know where all I am going wrong here in my approach?
<?php
$sUrl = "/tstd_c_b1@s54782";
$sParam = "primaryToken";
$nTime = time();
$nEventDuration = 86400;
$nWindow = $nTime + $nEventDuration;
$sSalt = "akamai123!";
$sExtract = ""; // optional
function urlauth_gen_url($sUrl, $sParam, $nWindow,
$sSalt, $sExtract, $nTime) {
$sToken = urlauth_gen_token($sUrl, $nWindow, $sSalt,
$sExtract, $nTime);
echo $token;
if ($sToken == null) {
return ;
}
if (($sParam == "") || (!is_string($sParam))) {
$sParam = "__gda__";
}
if ((strlen($sParam) < 5) || (strlen($sParam) > 12)) {
return;
}
if (($nWindow < 0) || (!is_integer($nWindow))) {
return;
}
if (($nTime <= 0) || (!is_integer($nTime))) {
$nTime = time();
}
$nExpires = $nWindow + $nTime;
if (strpos($sUrl, "?") === false) {
$res = $sUrl . "?" . $sParam . "=" . $nExpires . "_" . $sToken;
} else {
$res = $sUrl . "&" . $sParam . "=" . $nExpires . "_" . $sToken;
}
return $res;
}
/**
* Returns the hash portion of the token. This function should not be
* called directly.
*/
function urlauth_gen_token($sUrl, $nWindow, $sSalt,
$sExtract, $nTime) {
if (($sUrl == "") || (!is_string($sUrl))) {
return;
}
if (($nWindow < 0) || (!is_integer($nWindow))) {
return;
}
if (($sSalt == "") || (!is_string($sSalt))) {
return;
}
if (!is_string($sExtract)) {
$sExtract = "";
}
if (($nTime <= 0) || (!is_integer($nTime))) {
$nTime = time();
}
$nExpires = $nWindow + $nTime;
$sExpByte1 = chr($nExpires & 0xff);
$sExpByte2 = chr(($nExpires >> 8) & 0xff);
$sExpByte3 = chr(($nExpires >> 16) & 0xff);
$sExpByte4 = chr(($nExpires >> 24) & 0xff);
$sData = $sExpByte1 . $sExpByte2 . $sExpByte3 . $sExpByte4
. $sUrl . $sExtract . $sSalt;
$sHash = _unHex(md5($sData));
$sToken = md5($sSalt . $sHash);
return $sToken;
}
/**
* Helper function used to translate hex data to binary
*/
function _unHex($str) {
$res = "";
for ($i = 0; $i < strlen($str); $i += 2) {
$res .= chr(hexdec(substr($str, $i, 2)));
}
return $res;
}
?>
--
Regards,
Shreyas Agasthya