我們先寫出函式:
<?php
function encode_file_contents($filename) {
$type=strtolower(substr(strrchr($filename,"."),1));
if ("php" == $type && is_file($filename) && is_writable($filename)) { //
如果是PHP檔案 並且可寫 則進行壓縮編碼
$contents = file_get_contents($filename); // 判斷檔案是否已經被編碼處
理
$contents = php_strip_whitespace($filename);
// 去除PHP頭部和尾部標識
$headerPos = strpos($contents,"<?php");
$footerPos = strrpos($contents,"?>");
$contents = substr($contents, $headerPos + 5, $footerPos -
$headerPos);
$encode = base64_encode(gzdeflate($contents)); // 開始編碼
$encode = "<?php"."eval(gzinflate(base64_decode(".""".
$encode.""".")));?>";
return file_put_contents($filename, $encode);
}
return false;
呼叫此函式:
$filename = "result1.php";
encode_file_contents($filename);
echo "OK,加密完成!";
?>
3
測試是否加密成功:檔名為result1.php,執行程式碼
4
執行成功。
我們先寫出函式:
<?php
function encode_file_contents($filename) {
$type=strtolower(substr(strrchr($filename,"."),1));
if ("php" == $type && is_file($filename) && is_writable($filename)) { //
如果是PHP檔案 並且可寫 則進行壓縮編碼
$contents = file_get_contents($filename); // 判斷檔案是否已經被編碼處
理
$contents = php_strip_whitespace($filename);
// 去除PHP頭部和尾部標識
$headerPos = strpos($contents,"<?php");
$footerPos = strrpos($contents,"?>");
$contents = substr($contents, $headerPos + 5, $footerPos -
$headerPos);
$encode = base64_encode(gzdeflate($contents)); // 開始編碼
$encode = "<?php"."eval(gzinflate(base64_decode(".""".
$encode.""".")));?>";
return file_put_contents($filename, $encode);
}
return false;
}
呼叫此函式:
$filename = "result1.php";
encode_file_contents($filename);
echo "OK,加密完成!";
?>
3
測試是否加密成功:檔名為result1.php,執行程式碼
4
執行成功。