代码:
<?php function random($length, $numeric = 0) { //返回随机数 mt_srand((double)microtime() * 1000000); if($numeric){ $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1)); }else{ $hash = ''; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; for($i = 0; $i < $length; $i++){ $hash .= $chars[mt_rand(0, $max)]; } } return $hash; } function query_encode($sQuery) { //加密链接 if(strlen($sQuery)==0){ return ''; }else{ $s_tem = preg_replace("/&/i", '&', $sQuery); $s_tem = preg_replace("/&/i", '&', $s_tem); $a_tem = explode('&', $s_tem); shuffle($a_tem); $s_tem = implode('&', $a_tem); $s_tem = random(3). $s_tem . random(3); $s_tem = rawurlencode($s_tem); $s_tem = base64_encode($s_tem); $s_tem = strrev($s_tem); return $s_tem; } } function query_decode($sEncode) { //解密链接 if(strlen($sEncode)==0){ return ''; }else{ $s_tem = strrev($sEncode); $s_tem = base64_decode($s_tem); $s_tem = rawurldecode($s_tem); $s_tem = substr($s_tem, 0, -3); $s_tem = substr($s_tem, 3); return $s_tem; } } function rebuild_GET() { //重写$_GET全局变量 $_GET = array(); $s_query = $_SERVER['QUERY_STRING']; if(strlen($s_query)==0){ return; }else{ $s_tem = query_decode($s_query); $a_tem = explode('&', $s_tem); foreach($a_tem as $val){ $tem = explode('=', $val); $_GET[$tem[0]] = $tem[1]; } } } rebuild_GET(); echo 'GET:<pre>'.print_r($_GET, true).'</pre>'; function testGET(){ echo 'Function GET:<pre>'.print_r($_GET, true).'</pre>'; } testGET(); ?> <br /><br /><br /> <? for($i=1; $i<10; $i++){ $s_url = query_encode('ac=index:logo&class=45&id='.$i); echo sprintf('<a href="?%s">TEST: %s</a><br />', $s_url, $s_url); } ?>
加密解密的其他方法:
<? // Encript String function encrypt($string) { $key = 'tkdcks9796'; $result = ''; $string .= '---'.$key; for($i=0; $i<strlen($string); $i++) { $char = substr($string, $i, 1); $keychar = substr($key, ($i % strlen($key))-1, 1); $char = chr(ord($char)+ord($keychar)); $result.=$char; } return base64_encode($result); } // Decript String function decrypt($string) { $key = 'tkdcks9796'; $result = ''; $string = base64_decode($string); for($i=0; $i<strlen($string); $i++) { $char = substr($string, $i, 1); $keychar = substr($key, ($i % strlen($key))-1, 1); $char = chr(ord($char)-ord($keychar)); $result.=$char; } $tmp = explode('---',$result); return $tmp[0]; } echo encrypt('test'),'<br>',decrypt(encrypt('test')); ?>
或者
function encrypt($data, $key) { $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= $key{$x}; $x++; } for ($i = 0; $i < $len; $i++) { $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); } function decrypt($data, $key) { $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); } else { $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; } $data = 'PHP加密解密算法'; // 被加密信息 $key = '123'; // 密钥 $encrypt = encrypt($data, $key); $decrypt = decrypt($encrypt, $key); echo $encrypt, "\n", $decrypt;
邮件地址加密:
function encode_email($email='[email protected]', $linkText='Contact Us', $attrs ='class="emailencoder"' ) { // remplazar aroba y puntos $email = str_replace('@', '@', $email); $email = str_replace('.', '.', $email); $email = str_split($email, 5); $linkText = str_replace('@', '@', $linkText); $linkText = str_replace('.', '.', $linkText); $linkText = str_split($linkText, 5); $part1 = '<a href="ma'; $part2 = 'ilto:'; $part3 = '" '. $attrs .' >'; $part4 = '</a>'; $encoded = '<script type="text/javascript">'; $encoded .= "document.write('$part1');"; $encoded .= "document.write('$part2');"; foreach($email as $e) { $encoded .= "document.write('$e');"; } $encoded .= "document.write('$part3');"; foreach($linkText as $l) { $encoded .= "document.write('$l');"; } $encoded .= "document.write('$part4');"; $encoded .= '</script>'; return $encoded; } echo encode_email();