PHP 代码没有被执行,而是代码显示在页面上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5121495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:52:25  来源:igfitidea点击:

PHP code is not being executed, instead code shows on the page

phpapache

提问by Gui

I'm trying to execute some PHP code on a project (using Dreamweaver) but the code isn't being run.

我正在尝试在一个项目上执行一些 PHP 代码(使用 Dreamweaver),但代码没有运行。

When I check the source code, the PHP code appears as HTML tags (I can see it in the source code). Apache is running properly (I'm working with XAMPP), the PHP pages are being opened properly but the PHP code isn't being executed.

当我查看源代码时,PHP 代码显示为 HTML 标记(我可以在源代码中看到它)。Apache 运行正常(我正在使用 XAMPP),PHP 页面可以正常打开,但 PHP 代码没有被执行。

Does someone have a suggestion about what is happening?

有人对正在发生的事情有什么建议吗?

Note:The file is already named as filename.php

注意:该文件已被命名为filename.php

Edit:The Code..:

编辑:代码..:

<?
include_once("/code/configs.php");
?>

The print

印刷品

回答by shmeeps

Sounds like there is something wrong with your configuration, here are a few things you can check:

听起来您的配置有问题,您可以检查以下几点:

  1. 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 -vfrom a command line and see if returns version information or any errors.

  2. Make sure that the PHP module is listed and uncommentedinside 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.

  3. 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 .phpfiles as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.

  4. Make sure your file has the .phpextension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP.

  5. Make sure you are not using short tagsin the PHP file (<?), these are not enabled on all serversby default and their use is discouraged. Use <?phpinstead (or enable short tags in your php.iniwith short_open_tag=Onif you have code that relies on them).

  6. Make sure you are accessing your file over your webserver using an URL like http://localhost/file.phpnot via local file access file://localhost/www/file.php

  1. 确保 PHP 已安装并正确运行。这听起来可能很傻,但你永远不知道。一种简单的检查方法是从命令行运行php -v并查看是否返回版本信息或任何错误。

  2. 确保 PHP 模块在 Apache 的 httpd.conf 中列出并取消注释。这应该类似于LoadModule php5_module "c:/php/php5apache2_2.dll"文件中的内容。搜索LoadModule php,并确保其;前面没有注释 ( )。

  3. 确保 Apache 的 httpd.conf 文件中包含 PHP MIME 类型。这应该类似于AddType application/x-httpd-php .php. 这告诉 Apache 将.php文件作为 PHP运行。搜索 AddType,然后确保有一个 PHP 条目,并且没有注释

  4. 确保您的文件具有.php扩展名,或第 3 点中 MIME 定义中指定的任何扩展名,否则它将不会作为 PHP 执行。

  5. 确保您没有在 PHP 文件 ( ) 中使用短标签,默认情况下并非所有服务器<?启用这些短标签,因此 鼓励使用它们。使用<?php替代(或启用短标记在php.inishort_open_tag=On,如果你有依赖于它们的代码)。

  6. 确保您使用 URLhttp://localhost/file.php通过网络服务器访问您的文件,而不是通过本地文件访问file://localhost/www/file.php

And lastly check the PHP manual for further setup tips.

最后查看PHP 手册以获得进一步的设置提示

回答by sj59

php7 :

php7:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.0-fpm
sudo service apache2 restart

回答by Matt McDonald

I'm running Apache on Ubuntu and my issue was that the /etc/apache2/mods-available/php5.conffile was missing this:

我在 Ubuntu 上运行 Apache,我的问题是/etc/apache2/mods-available/php5.conf文件丢失了这个:

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

I added it back in and php was parsing php files correctly.

我重新添加了它,php 正确解析了 php 文件。

回答by EdC

note for php 7 users, add this to your httpd.conf file:

请注意 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>

回答by Arrvi

I found another problem causing this issue and already solved it. I accidentally saved my script in UTF-16encoding. It seems that PHP5 can't recognize <?phptag in 16 bit encoding by default.

我发现了另一个导致此问题的问题,并且已经解决了。我不小心用UTF-16编码保存了我的脚本。<?php默认情况下,PHP5 似乎无法识别16 位编码中的标签。

回答by Henry van Megen

I'm posting this answer because my Virtualmin/Webmin admin interface decided it was a good idea to disable my PHP engine.. took me a while to find the solution, so I thought I'd share it with you guys:

我发布这个答案是因为我的 Virtualmin/Webmin 管理界面决定禁用我的 PHP 引擎是个好主意..我花了一段时间才找到解决方案,所以我想我会与你们分享:

Also, be sure to check that none of your website config files related to this specific host or virtualhost have any php_admin_value's in them that turn off PHP, like this:

另外,请务必检查与此特定主机或虚拟主机相关的网站配置文件中是否没有任何关闭 PHP 的 php_admin_value,如下所示:

php_admin_value engine Off

When in doubt, comment it...

如有疑问,请发表评论...

# php_admin_value engine Off

And restart your webserver.

并重新启动您的网络服务器。

回答by Hasan Baig

You're just opening your php file into browser. You have to open it using localhost url. if you open a file directly from your directory it will not execute the php code in any case.

您只是将 php 文件打开到浏览器中。您必须使用 localhost url 打开它。如果您直接从您的目录中打开一个文件,它无论如何都不会执行 php 代码。

use: http://locahost/index.phpor http:127.0.0.1/index.php

使用:http://locahost/index.phphttp:127.0.0.1/index.php

Enable php short code. In your case, you are using <?which is php short code for <?php. By default php short codes are disabled.

启用php短代码。在您的情况下,您正在使用<?which 是 .php 的 php 短代码<?php。默认情况下,禁用 php 短代码。

Also use: sudo apt-get install php5 libapache2-mod-php5 php5-mcryptif you are a ubuntu user.

还可以使用:sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt如果您是 ubuntu 用户。

回答by user3257072

I know it should sound silly... but seldom it happens.

我知道这听起来很傻……但很少发生。

Check if you are trying to execute php from

检查您是否正在尝试从

**http://localhost/info.php**

and not from

而不是来自

file:///var/www/info.php

ps> you can notice that if you write from shell

ps> 你可以注意到,如果你从 shell 写

php info.php 

it answer with the code (it means php functions)..

它用代码回答(这意味着 php 函数)。

回答by ndasusers

This just happened to me again, along with the server downloading html files, rather than processing. I had not use the webserver apache for some time on the computer and meanwhile Ubuntu updated like two more versions from originally installed LTS. Now it is

这又发生在我身上,服务器下载 html 文件,而不是处理。我有一段时间没有在计算机上使用网络服务器 apache,同时 Ubuntu 从最初安装的 LTS 更新了两个版本。现在它是

$ cat /etc/issue
Ubuntu 16.04 LTS

So the php worked after like so:

所以 php 像这样工作:

$ sudo apt-get install lamp-server^
$ sudo a2enmod php7.0
$ sudo service apache2 restart 

The webserver was now parsing the php. Maybe now got to update some webs since php7.0 now running where as it was before running php5. Oh well.

网络服务器现在正在解析 php.ini 文件。也许现在必须更新一些网站,因为 php7.0 现在运行在运行 php5 之前的位置。那好吧。

回答by Gui

I've solved this by uninstalling XAMPP, and installing WAMP. Thanks for the help.

我已经通过卸载 XAMPP 并安装 WAMP 解决了这个问题。谢谢您的帮助。