<?php
if ( isset( $_GET['do'] ) && 'source' == $_GET['do'] ) {
//ONLY FOR YOU...
highlight_file( __FILE__ );
}
//是否开启URL加密模式
define('OPEN_URL_ENCODE', true);
//加密类型
//这里只有两种算法
//1.base64
//2.authcode
define('GET_ENCODE_TYPE', 'authcode');
//自定义GET接受标记
define('GET_TAG', 'get');
//加解密密钥
//只有在使用authcode加解密算法时用到
define('AUTH_CODE_KEY', 'paperen');
//需要传递的GET参数
$args = array(
'id' => 1533,
'type' => 2,
'name' => 'paperen',
'uuid' => '5f6830ij54',
);
echo '<p>要发送的参数为</p>';
print_r( $args );
//如果开启URL加密模式需要进行一些特殊处理
if ( OPEN_URL_ENCODE ) {
$_GET = extract_arg( $_GET );
}
//整理参数
function format_arg( $url, $args ) {
return (OPEN_URL_ENCODE) ? $url . '?' . GET_TAG . '=' . make_my_urlarg( arg_arr2str( $args ) ) : $url . '?' . $tmp;
}
//还原参数
function extract_arg( $arr ) {
if ( isset( $arr[GET_TAG] ) ) {
return arg_arr2str( make_my_urlarg( $arr[GET_TAG] , true ), true );
} else {
return array('');
}
}
//数组字符串互换
function arg_arr2str( $data, $is_str2arr = false ) {
$tmp = '';
if ( $is_str2arr && is_string( $data ) ) {
$arr = explode('&', $data);
foreach( $arr as $index => $data ) {
if ( false !== strpos($data, '=') ) {
list($k , $val) = explode('=', $data);
$tmp[$k] = $val;
}
}
} else if( !$is_str2arr && is_array( $data ) ) {
foreach( $data as $k => $val ) $tmp .= '&' . $k . '=' . $val;
$tmp = substr($tmp, 1);
}
return $tmp;
}
//根据相应的算法加解密参数
function make_my_urlarg($str, $is_decode = false) {
if ( 'base64' == GET_ENCODE_TYPE ) {
return ($is_decode) ? base64_decode($str) : base64_encode($str);
} else if( 'authcode' == GET_ENCODE_TYPE ) {
return ($is_decode) ? authcode($str, 'DECODE', AUTH_CODE_KEY) : urlencode( authcode($str, 'ENCODE', AUTH_CODE_KEY) );
}
}
/**
* authcode 摘自UChome
* @param string $string 明文
* @param string $operation ENCODE加密/DECODE解密
* @param string $key 密钥(可以在配置文件中定义)
* @param string $expiry 有效期?
* @return string
*/
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 1;
$key = md5($key ? $key : 'leqinet');
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya . md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.base64_encode($result);
}
}
//还是一如既往地接收GET参数,感觉这个加解密处理与我们无关
$id = isset( $_GET['id'] ) ? intval($_GET['id']) : '';
$type = isset( $_GET['type'] ) ? intval($_GET['type']) : '';
if ( $id ) {
//do something
}
$url = format_arg('index.php', $args);
echo "<p>请求URL(加密后) 加密方式为" .GET_ENCODE_TYPE. "</p>";
echo $url;
echo '<p>接收到的$_GET</p>';
print_r( $_GET );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GET ENCODE [paperen]</title>
<style>
*{font-size:14px;font-family:Verdana, Geneva, sans-serif;}
a{color:#2D7DF7;}
</style>
</head>
<body>
<p><a href="<?php echo $url; ?>">Link</a></p>
<p><a href="?do=source">查看PHP源码</a></p>
<p><a href="http://iamlze.cn">返回paperen博客</a></p>
</body>
</html>
要发送的参数为
Array ( [id] => 1533 [type] => 2 [name] => paperen [uuid] => 5f6830ij54 )请求URL(加密后) 加密方式为authcode
index.php?get=2u5dzc4IS3g4GgIecq36h%2FJorqsXAEeGuKQLSLLFlCGsfgMLWvsEORIDDHyooukwkEIUjhgczpIAcVlPcmd7yWoLbg%2Fwp接收到的$_GET
Array ( [0] => )