因为要在.HTML中固定位置输出一个随机数,但写如下PHP语句却怎么也无法显示结果,单独写在.PHP中输出没有问题:
<?php
echo rand(9,99);
?>
经查阅,原来需在Apache的配置文件加入如下语句:
AddType application/x-httpd-php .html .htm
这个是让.html后缀的文件可执行PHP,如果html文件中包括PHP代码,则该文件和.php后缀的文件完全 一样,都成为动态文件,在服务器端处理。 如果在httpd.conf中不设置此语句,则以字符的形式直接读取html文件中的内容,不作PHP处理。
还可以加:
AddType application/x-httpd-php .htm
则.htm文件也可以执行php程序了
AddType application/x-httpd-php .txt
让普通的文本文件格式也能运行php程序
一般的虚拟主机,我们无法修改httpd.conf,但我们可以通过修改.htaccess来实现。
修改.htaccess
NEW PHP Add Handlers
SolutionIf you need an add handler for PHP please use the following:
#PHP5
AddHandler application/x-httpd-php5 .html .htm
#PHP4
AddHandler application/x-httpd-php4 .html .htm
That is if you need to parse .html files or any other extension as PHP you can place that in your .htaccess file.