下面是可以将我们的url地址加密.确保我们提交的数据安全
以下是代码片段:
1 <?php 2 function keyED($txt,$encrypt_key) //定义一个keyED 3 { 4 $encrypt_key = md5($encrypt_key); 5 $ctr=0; 6 $tmp = ''; 7 for($i=0;$i<strlen($txt);$i++) 8 { 9 if ($ctr==strlen($encrypt_key)) 10 $ctr=0; 11 $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); 12 $ctr++; 13 } 14 return $tmp; 15 } 16 function encrypt($txt,$key) 17 { 18 $encrypt_key = md5(mt_rand(0,100)); 19 $ctr=0; 20 $tmp = ''; 21 for ($i=0;$i<strlen($txt);$i++) 22 { 23 if ($ctr==strlen($encrypt_key)) 24 $ctr=0; 25 $tmp.=substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); 26 $ctr++; 27 } 28 return keyED($tmp,$key); 29 } 30 function decrypt($txt,$key) 31 { 32 $txt = keyED($txt,$key); 33 $tmp = ''; 34 for($i=0;$i<strlen($txt);$i++) 35 { 36 $md5 = substr($txt,$i,1); 37 $i++; 38 $tmp.= (substr($txt,$i,1) ^ $md5); 39 } 40 return $tmp; 41 } 42 function encrypt_url($url,$key) 43 { 44 return rawurlencode(base64_encode(encrypt($url,$key))); 45 } 46 function decrypt_url($url,$key) 47 { 48 return decrypt(base64_decode(rawurldecode($url)),$key); 49 } 50 function geturl($str,$key) 51 { 52 $str = decrypt_url($str,$key); 53 $url_array = explode('&',$str); 54 if (is_array($url_array)) 55 { 56 foreach ($url_array as $var) 57 { 58 $var_array = explode('=',$var); 59 $vars[$var_array[0]]=$var_array[1]; 60 } 61 } 62 return $vars; 63 } 64 ?>
下面是函数调用的实例
以下是代码片段:
1 $key = "key"; 2 //生产加密参数地址 3 for ($i=0;$i<10;$i++) 4 { 5 $url=encrypt_url('id=$i&sdf=asdf',$key); 6 echo "<a href=?url=".$url.">php技术文章$i</a><br>"; 7 } 8 //获取参数地址 9 $get = geturl($_GET['url'],$key); 10 var_dump($get);