我试图在一个项目上执行一些PHP代码(使用Dreamweaver),但代码没有运行。
当我检查源代码时,PHP代码显示为HTML标记(我可以在源代码中看到它)。Apache运行正常(我正在使用XAMPP), PHP页面正在正常打开,但PHP代码没有被执行。
有人有什么建议吗?
注意:该文件已经命名为filename.php
编辑: 代码. .:
<?
include_once("/code/configs.php");
?>
我试图在一个项目上执行一些PHP代码(使用Dreamweaver),但代码没有运行。
当我检查源代码时,PHP代码显示为HTML标记(我可以在源代码中看到它)。Apache运行正常(我正在使用XAMPP), PHP页面正在正常打开,但PHP代码没有被执行。
有人有什么建议吗?
注意:该文件已经命名为filename.php
编辑: 代码. .:
<?
include_once("/code/configs.php");
?>
当前回答
刚刚花了几个小时试图让PHP 5与Windows 10上的Apache 2.4一起运行。最后对我来说,这是httpd.conf LoadModule中的一个拼写错误。通过denny2018这个apachelounge线程的最后一个回答,提请注意写作和准确的模块路径。谢谢你!
过了两晚,我发现……我的目录是c:(小写)
我有LoadModule php5_module“c:/php/php5apache2.dll”,但正确的apache 2.4是:
LoadModule php5_module "C:/php/php5apache2_4.dll"
所以我之前也错过了_4(对于apache 2.4)。对我有用的完整片段:
# PHP
LoadModule php5_module "C:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
刚试过PHP 7。在Apache 2.4的httpd.conf中读取LoadModule行
LoadModule php7_module "C:/php/php7apache2_4.dll"
目前php手册显示c:/php/php5apache2.dll,当然需要调整。
其他回答
此问题的另一个可能原因可能是您试图在UserDir模块的“用户目录”中运行脚本。在用户目录下运行PHP脚本默认是禁用的。如果脚本位于主文件夹的public_html目录中,并且您试图从http://localhost/~your_username访问它,那么就会遇到这个问题。
要解决这个问题,打开/etc/apache2/mods-enabled/php7.2.conf。必须注释或删除底部的标记块
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>
</IfModule>
听起来你的配置好像有问题,下面是一些你可以检查的东西:
Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php -v from a command line and see if returns version information or any errors. Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule php5_module "c:/php/php5apache2_2.dll" in the file. Search for LoadModule php, and make sure that there is no comment (;) in front of it. Make sure that Apache's httpd.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented. Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP. Make sure you are not using short tags in the PHP file (<?), these are not enabled on all servers by default and their use is discouraged. Use <?php instead (or enable short tags in your php.ini with short_open_tag=On if you have code that relies on them). Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php
最后,查看PHP手册以获得更多的设置技巧。
使用Debian 10安装Apache + php7测试的最简单方法:
apt-get update -y
apt-get install apache2 php7.0 libapache2-mod-php -y
sudo service apache2 restart
我发布这个答案是因为我的Virtualmin/Webmin管理界面决定禁用我的PHP引擎是一个好主意。我花了一段时间才找到解决方案,所以我想和你们分享一下:
同时,确保你的网站配置文件中没有任何与这个特定主机或虚拟主机相关的php_admin_value在它们中关闭PHP,就像这样:
php_admin_value engine Off
有疑问的时候,评论一下…
# php_admin_value engine Off
并重新启动您的web服务器。
你只是把php文件打开到浏览器。你必须使用localhost url打开它。如果您直接从目录中打开一个文件,在任何情况下它都不会执行PHP代码。
使用:http://locahost/index.php或http:127.0.0.1/index.php
启用php短代码。在本例中,您使用<?这是<? PHP的PHP短代码。默认情况下,php短代码是禁用的。
如果你是ubuntu用户,也可以使用:sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt。