实现效果需要2个文件:
.htaccess根据高亮标记转向代理处理文件highlightFile.php高亮代理处理文件
文件内容:
.htaccess
RewriteCond %{REQUEST_FILENAME} -fRewriteCond %{QUERY_STRING} hl|highlight [NC]RewriteCond %{REQUEST_FILENAME} ^D:/xampp/htdocs/test [NC]RewriteCond %{REQUEST_FILENAME} ^D:/xampp/htdocs/test(.+) [NC]RewriteCond %{REQUEST_FILENAME} !/highlightFile\.php [NC]RewriteRule ^.*$ /highlightFile.php?file=%1 [L]
其中D:/xampp/htdocs/test是项目根目录,可以通过打印$_SERVER['DOCUMENT_ROOT']得到。
2. highlightFile.php
<?php/**** @author Zjmainstay* @website http://zjmainstay.cn* @copyright GPL* @version 1.0* @year 2014**/header("Content-type: text/html; charset=utf-8");//单词过滤$skipWords = array('localhost','127.0.0.1','mysql_connect','mysqli_connect','phpinfo','password','passwd',);if(empty($_GET['file'])) return false;$filePath = $_SERVER['DOCUMENT_ROOT'] . $_GET['file'];$content = file_get_contents($filePath);foreach($skipWords as $skip) {if(stripos($content, $skip) !== false) {echo '敏感文件不显示 -_-!';exit;}}highlight_file($filePath); //高亮文件源码//End_php
演示:代码高亮测试
未经同意禁止转载!
转载请附带本文原文地址:PHP利用.htaccess实现代理高亮源码,首发自 Zjmainstay学习笔记




