简体   繁体   English

为什么我的php标签转换为html评论?

[英]Why are my php tags converted to html comments?

A friend's lamp host seems to be misconfigured. 朋友的灯主机似乎配置错​​误。 I try to execute php, but it doesn't seem to be working. 我尝试执行php,但它似乎没有工作。

In Chrome's inspect element: 在Chrome的inspect元素中:

<?php echo 'test'; ?> 

becomes : 成为:

<!--?php echo 'test'; ?-->

Furthermore, its been triggering a file download, rather than opening it as a webpage. 此外,它一直在触发文件下载,而不是将其作为网页打开。

I've tried various code in an .htaccess file, but it doesn't seem to have any effect: 我在.htaccess文件中尝试了各种代码,但似乎没有任何影响:

AddType x-mapp-php5 .php
AddType application/x-httpd-php .php
AddHandler x-mapp-php5 .php

The place to correctly configure PHP operation is the httpd.conf file, which resides in the conf subdirectory of your Apache installation directory. 正确配置PHP操作的地方是httpd.conf文件,该文件位于Apache安装目录的conf子目录中。

In there, you'll want to look for the module loading section, which will be a bunch of lines that start with LoadModule . 在那里,你需要寻找模块加载部分,它将是一堆以LoadModule开头的行。 Somewhere in there, you should have the following (or something very similar): 在那里的某个地方,你应该有以下(或类似的东西):

LoadModule php5_module "location\of\your\php\installation"
AddType application/x-httpd-php .php
PHPIniDir "location\of\your\php\configuration\file"

I'm not all too familiar with Linux, but in Windows (WAMP) installations, those would be something along the lines of: 我不太熟悉Linux,但在Windows(WAMP)安装中,这些将是:

LoadModule php5_module "c:/program files/php/php5apache2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/program files/php"

And the httpd.conf file, on my machine, is at C:\\Program Files\\Apache Group\\Apache2\\conf\\httpd.conf . 我的机器上的httpd.conf文件位于C:\\Program Files\\Apache Group\\Apache2\\conf\\httpd.conf

It could also be that PHP is simply not installed at all on your machine, in which case, you will have to download it and install it. 也可能是您的计算机上根本没有安装PHP,在这种情况下,您必须下载并安装它。 Brad's already posted the relevant link in one of his comments, (+1, by the way, Brad), but for the sake of having everything in one spot: 布拉德已经在他的一条评论中发布了相关链接(顺便说一句,顺便说一句,布拉德),但是为了让一切都在一个地方:

PHP: Installation and Configuration - Manual PHP:安装和配置 - 手动

Your Chrome is lying to you. 你的Chrome对你说谎。

Your PHP source file is <?php echo 'test'; ?> 你的PHP源文件是<?php echo 'test'; ?> <?php echo 'test'; ?> . <?php echo 'test'; ?> Because PHP is not executed, this file is sent to the browser. 由于未执行PHP,因此将此文件发送到浏览器。 If the browser should interpret this text, it will stumble upon the <? 如果浏览器应该解释这个文本,它会偶然发现<? ?> marks. ?>标记。 They have a meaning - they are "XML processing instructions", and the text after the opening angle defines the target. 它们有一个含义 - 它们是“XML处理指令”,而打开角度后的文本定义了目标。

Obviously the browser does not know about a target named "PHP", so this text is ignored. 显然,浏览器不知道名为“PHP”的目标,因此忽略此文本。

And then the element inspector tries to display the DOM and is lying about the original source code, because he is working on the PARSED source - which is great because you usually want to know on which data the browser acts, and this includes how the browser interpreted your source. 然后元素检查器尝试显示DOM并且对原始源代码撒谎,因为他正在处理PARSED源 - 这很好,因为您通常想知道浏览器所处理的数据,这包括浏览器的方式解释你的来源。

But if you make any error, the browser will try to fix it, and the fix is included in the element inspector. 但是,如果您出现任何错误,浏览器将尝试修复它,并且修复程序包含在元素检查器中。

Obviously the fix for an unknown XML processing instruction is to disable it by commenting it out. 显然,对未知XML处理指令的修复是通过将其注释掉来禁用它。

This just happened to me. 这恰好发生在我身上。 Turned out I had forgotten to change the filetype from .html to .php 原来我忘记将文件类型从.html更改为.php

Sounds to me that your PHP is not correctly configured or installed in your lamp configuration. 听我说您的PHP未正确配置或安装在灯泡配置中。 What distribution are you using? 你使用什么发行版? It might be as simple as running a command to re-install PHP, otherwise you will likely need to compile apache with php support. 它可能就像运行重新安装PHP的命令一样简单,否则您可能需要使用php支持编译apache。

I was faced with exact same problem when I accidently tried to test local php file in browser on server through file:// protocol, not through installed site. 当我意外地尝试通过file://协议在服务器上的浏览器中测试本地php文件时,我遇到了完全相同的问题,而不是通过安装的站点。

So the answer is one: "Mr. PHP has left the building". 所以答案是:“PHP先生离开了大楼”。 We need to check the configuration, location of a file or access. 我们需要检查文件的配置,位置或访问。

And browser is just trying to fix a web page and help us. 浏览器只是试图修复网页并帮助我们。

If you are placing your code outside the standard directories (development scenario, in my case) you should check in your /etc/apache2/mod-enabled or /etc/apache2/mod-available in the php5.conf (for ubuntu) and comment the lines that the comment indicates: 如果您将代码放在标准目录之外(在我的情况下是开发场景),您应该在php5.conf中检查/ etc / apache2 / mod-enabled或/ etc / apache2 / mod-available(对于ubuntu)和评论评论指出的行:

# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_value engine Off
    </Directory>
</IfModule>

This answer doesn't apply to the OP's specific variant of this problem, but I had the basic same issue – <? var_dump($test); ?> 这个答案不适用于OP这个问题的具体变体,但我有基本相同的问题 - <? var_dump($test); ?> <? var_dump($test); ?> <? var_dump($test); ?> being converted to <!--? var_dump($test); ?--> <? var_dump($test); ?>转换为<!--? var_dump($test); ?--> <!--? var_dump($test); ?--> <!--? var_dump($test); ?--> – which I could solve by enabling short_open_tag in php.ini . <!--? var_dump($test); ?--> - 我可以通过在php.ini中启用short_open_tag来解决这个问题。

Sounds like you are using an editor that is changing what you enter. 听起来你正在使用一个改变你输入内容的编辑器。 Make sure that what you want in the file is what is actually in the file. 确保文件中的内容与文件中的内容完全相同。 Using FTP to upload a php file should ensure this. 使用FTP上传php文件应该确保这一点。

I just solved this same problem. 我刚刚解决了同样的问题。 You need to open your file from your WAMP, and not from your hard drive directrory. 您需要从WAMP打开文件,而不是从硬盘驱动器中打开文件。

In your browser, put: localhost/...../yourfile.php 在浏览器中,输入: localhost/...../yourfile.php

Otherwise, your browser will replace all <?php ?> with <!-- ?php ?--> 否则,您的浏览器将用<!-- ?php ?-->替换所有<?php ?> <!-- ?php ?-->

看来你必须明确指示apache将html文件作为php文件处理,我遇到了同样的问题但是将文件重命名为.php为我解决了这个问题。

On an Ubuntu system, installing the apache php5 plugin worked for me: 在Ubuntu系统上,安装apache php5插件对我有用:

sudo apt-get install libapache2-mod-php5
sudo service apache2 restart

I have same problem sometimes, probably the extension of your file is "html". 我有时会遇到同样的问题,可能你文件的扩展名是“html”。 Change It to "php" and it'll OK. 将其更改为“php”,它就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM