我试图在一个项目上执行一些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");
?>
当前回答
我在Ubuntu上运行Apache,我的问题是/etc/apache2/mods-available/php5.conf文件缺少这个:
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
我把它添加回来,php正确地解析php文件。
其他回答
今天,PHP自带一个内置的web服务器,所以不需要进一步的安装,因此呈现PHP文件用于开发使用的最简单的方法之一是:
打开一个终端,切换到包含web应用程序的源目录(它的主文件通常称为index.php)。 在这个目录中,键入php -S localhost:7777(端口号是任意选择的)。 打开web浏览器,在地址栏中输入localhost:7777,就会呈现所需的网页。
如果您已经完成了访问,那么返回您的终端并按CTRL-C关闭开发服务器。
这个信息也是通过在终端中调用php——help来给出的,进一步的信息在https://www.php.net/manual/en/features.commandline.webserver.php中给出
注意,对于PHP 7用户,添加到你的httpd.conf文件:
# PHP 7 specific configuration
<IfModule php7_module>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
我认为问题是,它显示的代码而不是结果是,它不会去本地主机。重新检查你要去的地址。是到本地文件目录还是到本地主机。
从你发送的屏幕截图,它将到你的计算机,而不是到本地主机。
"file:/// "应该是"localhost/"
在我的ubuntu 14.04, apache 2.4, php 5.5.9安装,我尝试在/var/www/html(默认文档根)上使用sample.php,它工作正常。 所以问题出在我的虚拟服务器配置上。 解决方案是在包含.php的Directory def中包含这一行:
php_admin_flag engine on
听起来你的配置好像有问题,下面是一些你可以检查的东西:
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手册以获得更多的设置技巧。