Thinkphp对视图进行加密处理
19 July 2016
效果图:
代码如下:
// 控制器
public function index()
{
$data = $this->fetch('Public/index');
$data = $this->encode($data);
echo $this->output($data);
}
// 字符替换
private function encode( $content )
{
return str_replace('%', "#",$this->phpescape($content) );
}
// 编码转换
private function phpescape($str)
{
preg_match_all ( "/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e", $str, $r );
//匹配utf-8字符,
$str = $r [0];
$l = count ( $str );
for($i = 0; $i < $l; $i ++) {
$value = ord ( $str [$i] [0] );
if ($value < 223) {
$str [$i] = rawurlencode ( utf8_decode ( $str [$i] ) );
//先将utf8编码转换为ISO-8859-1编码的单字节字符,urlencode单字节字符.
//utf8_decode()的作用相当于iconv("UTF-8","CP1252",$v)。
} else {
// $str [$i] = "%u" . strtoupper ( bin2hex ( iconv ( "UTF-8", "UCS-2", $str [$i] ) ) );
$str [$i] = "%u" . strtoupper ( bin2hex ( mb_convert_encoding($str [$i], "UCS-2", "UTF-8") ) );
}
}
return join ( "", $str );
}
// 加密到视图
private function output( $content )
{
$str = "<meta charset=\"utf-8\"><script charset='UTF-8'>function PSbqMNtFvsDgBsKj(PSbqMNtFvsDgBsKj){document.write( unescape(PSbqMNtFvsDgBsKj.replace(/#/g,'%')) );}";
$str .= "PSbqMNtFvsDgBsKj('".$content."'); </script>";
return $str;
}