url加密并计算时间
将URL地址参数进行加密传输提高网站安全性
加密算法,直接调用就好
function keyED($txt,$encrypt_key){ $encrypt_key = md5($encrypt_key); $ctr=0; $tmp = ""; for($i=0;$i<strlen($txt);$i++) { if ($ctr==strlen($encrypt_key)) $ctr=0; $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); $ctr++; } return $tmp; } function encrypt($txt,$key) { $encrypt_key = md5(mt_rand(0,100)); $ctr=0; $tmp = ""; for ($i=0;$i<strlen($txt);$i++) { if ($ctr==strlen($encrypt_key)) $ctr=0; $tmp.=substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); $ctr++; } return keyED($tmp,$key); } function decrypt($txt,$key){ $txt = keyED($txt,$key); $tmp = ""; for($i=0;$i<strlen($txt);$i++) { $md5 = substr($txt,$i,1); $i++; $tmp.= (substr($txt,$i,1) ^ $md5); } return $tmp; } function encrypt_url($url,$key){ return rawurlencode(base64_encode(encrypt($url,$key))); } function decrypt_url($url,$key){ return decrypt(base64_decode(rawurldecode($url)),$key); } function geturl($str,$key){ $str = decrypt_url($str,$key); $url_array = explode('&',$str); if (is_array($url_array)) { foreach ($url_array as $var) { $var_array = explode("=",$var); $vars[$var_array[0]]=$var_array[1]; } } return $vars; }
(封装一个比较时间戳大小的方法)
功能:计算两个时间戳之间相差的日时分秒 $begin_time 开始时间戳 $end_time 结束时间戳
function timediff($begin_time,$end_time) { $begin_time = strtotime($begin_time); $end_time = strtotime($end_time); if($begin_time < $end_time){ $starttime = $begin_time; $endtime = $end_time; }else{ $starttime = $end_time; $endtime = $begin_time; } //计算天数 $timediff = $endtime-$starttime; $days = intval($timediff/86400); //计算小时数 $remain = $timediff%86400; $hours = intval($remain/3600); //计算分钟数 $remain = $remain%3600; $mins = intval($remain/60); //计算秒数 $secs = $remain%60; $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs); return $res; }
下面是进行加密解密具体操作
1、首先对url进行加密,参数这里就写了一个时间
$key = 'key'; //url 加密字符 $time = date('Y-m-d h:i:s',time()); //获取时间 $url = "http://XXXXXXX/XXX/XXX.html?url=".encrypt_url("&agent_phone=&agent_level=&&time=$time",$key); echo $url; //看下效果
// http://XXXXXXX/XXX/XXX.html?url=UiQDMVJnXmJabgh4VwgEIQ9tC25UYVVoXjhZL1E2VWdXYlNuUnQBBww6VzZRJwExAGYGMQF8BXsBLlduAGtVZVI%2FA2JSMF42WjgIIVdnBGAPKAswVDhVLV41WTpRbVUzVzNTOlIzAW4%3D
2、访问加密后的url,进行打印
$time = date('Y-m-d h:i:s',time()); $decodeurl = geturl($_GET['url'],$key); p($decodeurl); //打印解密后的url echo $time; //当前时间 $diff_time = timediff($time,$decodeurl['time']); //比较时间戳大小 p($diff_time); exit;
3、查看打印后的结果
这样每次的链接地址都是动态的,而且可以在接收页面设置页面限制超时的有效期了
至此 一个简单的加密+时间判断就完成了。