有一個叫偉大的教程:Automatically Include Files with PHP & Apache解釋如何做到這一點與Apache指令和PHP代碼在末尾添加。首先,定義一個文件趕上頁它的輸出之前和追加任何你想要的:
<?php
{
$script = <<<GA
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxx-y']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
GA;
$replace = array('</body>','</BODY>');
$page = str_replace($replace, "{$script}\r</body>", $page);
// $replace2 = array('</title>','</TITLE>');
// $page = str_replace($replace2, " - My Awesome Site</title>", $page);
return $page;
}
ob_start("appendToFile");
?>
然後Apache的指令添加到您的虛擬主機。您需要前插 PHP文件,以使用ob_start()
方法:
<Directory "/path/to/folder">
AddType application/x-httpd-php .html .htm
php_value auto_prepend_file /absolute/path/to/apache-prepend.php
</Directory>
你使用什麼網絡服務器軟件? – Gumbo 2010-11-22 17:20:39
@Gumbo好點...剛更新說Apache 2 – CLJ 2010-11-22 17:21:29