考虑到网上很多的Copy&Paste,我自己编写了下面这个程序,可以有效的
保护网页源代码。
具体原理是用PHP乱序生成Javascript输出网页。
注意,$txt为原网页源代码,其中的"应该去掉。
<html>
<head>
<title>Random Text</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<?php
$line=explode("n",$txt);//将网页按行输出到数组line中
$length=count($line);//取得数组长度
for ($i=0;$i<$length;$i++)
{
$com1[$i]=$i;//乱序1
$com2[$i]=$i;//乱序2
$line[$i]=str_replace("r","",$line[$i]);//取消r字符
}
srand((double)microtime()*1000000);//初始化随机数
$rand_length=rand($length*10,$length*20);//产生随机乱序长度
for ($i=0;$i<$rand_length;$i++)//for语句产生正反两种顺序
{
$tmp1=rand(0,$length-1);
$tmp2=rand(0,$length-1);
$tmp=$com1[$tmp1];
$com1[$tmp1]=$com1[$tmp2];
$com1[$tmp2]=$tmp;
$com2[$com1[$tmp1]]=$tmp1;
$com2[$com1[$tmp2]]=$tmp2;
}
echo "<script language=javascript>n";
for ($i=0;$i<$length;$i++)//输出乱序后的网页源代码到Javascript中的字符串变量
{
echo "txt".$i."="".$line[$com1[$i]]."";n";
}
for ($i=0;$i<$length;$i++)//输出反乱序的Javascript语句,以达到将网页还原的目的
{
echo "document.write(txt".$com2[$i].");n";
}
echo "</script>n";
?>
</body>
</html>
|