PHP代码尚未执行,但代码在浏览器源代码中显示

发布于 2025-02-13 07:38:19 字数 421 浏览 0 评论 0原文

我试图在项目上执行一些PHP代码(使用Dreamweaver),但该代码未运行。

当我检查源代码时,PHP代码显示为HTML标签(我可以在源代码中看到它)。 Apache运行正常(我正在与XAMPP合作),PHP页面正在正确打开,但没有执行PHP代码。

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

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

编辑: 代码..:

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

“

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

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.

Does someone have a suggestion about what is happening?

Note: The file is already named as filename.php

Edit:
The Code..:

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

The print

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

握住你手 2025-02-20 07:38:19

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

  1. 确保使用url使用url http://localhost/file.php 不通过本地文件访问文件://localhost/www/file.php


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

  3. 确保php模块在apache的httpd.conf内部列出 ,这应该是load> loadsmodule php5_module“ c:/php/php/php5apache2_2.dll”在文件中。搜索loadModule php,并确保其前面没有注释(;)。

  4. 确保Apache的httpd.conf文件中具有php mime类型。这应该是addType应用程序/X-HTTPD-PHP .php。这告诉Apache以php的速度运行.php文件。搜索AddType,然后确保有php的条目,,并且它是未注销的

  5. 确保您的文件在其上具有.php扩展名,或第3点中的MIME定义中指定的任何扩展名,否则将不会以Php。

    执行。

  6. 确保您不使用短标签 php文件(&lt;?),默认情况下所有服务器均未启用这些文件,并且不建议使用它们的使用。使用&lt;?php代替(或在您的php.ini中启用短标签 short_open_tag = on如果您的代码依赖于代码他们)。

最后检查 PHP手册以获取更多设置提示

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

  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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).

And lastly check the PHP manual for further setup tips.

夜还是长夜 2025-02-20 07:38:19

PHP7:

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

php7 :

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.0-fpm
sudo service apache2 restart
小嗷兮 2025-02-20 07:38:19

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

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

我将其添加回了,PHP是正确解析的PHP文件。

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

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

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

淡淡の花香 2025-02-20 07:38:19

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

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

# 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>
臻嫒无言 2025-02-20 07:38:19

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

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

启用PHP简短代码。在您的情况下,您使用的是&lt;?哪个是&lt;?php的PHP简短代码。默认情况下,PHP简短代码被禁用。

另外使用:sudo apt-get安装php5 libapache2-mod-php5 php5-mcrypt如果您是Ubuntu用户。

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.

use: http://locahost/index.php or http: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.

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

鸵鸟症 2025-02-20 07:38:19

我发现了另一个引起此问题并已经解决的问题。我不小心将脚本保存在utf-16编码中。 PHP5似乎无法识别&lt;?php在16位中默认编码的标签。

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

波浪屿的海角声 2025-02-20 07:38:19

我要发布此答案,因为我的VirtualMin/Webmin Admin界面认为禁用我的PHP引擎是一个好主意。

另外,请务必检查您的网站配置文件是否与此特定主机或VirtualHost相关的任何网站配置文件中都有任何PHP_ADMIN_VALUE在其中关闭php,诸如此类:

php_admin_value engine Off

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

# php_admin_value engine Off

并重新启动您的Web服务器。

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:

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_admin_value engine Off

When in doubt, comment it...

# php_admin_value engine Off

And restart your webserver.

铁轨上的流浪者 2025-02-20 07:38:19

这再次发生在我身上,服务器下载HTML文件,而不是处理。我已经有一段时间在计算机上使用了Web服务器Apache,与此同时,Ubuntu更新了最初安装的LTS的两个版本。现在,

$ cat /etc/issue
Ubuntu 16.04 LTS

PHP像这样工作了:

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

Web服务器现在正在解析PHP。也许现在必须更新一些网络,因为PHP7.0现在在运行php5之前的位置运行。那好吧。

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

$ cat /etc/issue
Ubuntu 16.04 LTS

So the php worked after like so:

$ 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.

那请放手 2025-02-20 07:38:19

我知道这听起来应该很愚蠢...但是很少发生。

检查您是否试图从

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

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

PS&GT 执行PHP;您可以注意到,如果您从shell中写入

php info.php 

它,则用代码回答(这意味着PHP功能)。

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

php info.php 

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

怀念你的温柔 2025-02-20 07:38:19

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

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

恋你朝朝暮暮 2025-02-20 07:38:19

如果我们在同一页面中,请按照

sudo apt -get安装php -y
sudo apt-get install php- {bcmath,bz2,intl,gd,mbString,mysql,zip,zip,fpm} -y

以启用apache2中的php 7.2 fpm in:

a2enmod proxy_fcgi proxy_fcgi setenvif setenvif

a2enconf php7.2-fpm

更新2: Apache下载.php文件而不是渲染

之后,我面对上述问题。也有类似的问题,例如 this

我不知道为什么,但它仅发生在/var/www/html/ root文件夹中的.php文件。子目录一切都可以。 (例如,WordPress和PhpMyAdmin效果很好)

所以这是我的解决方案。我决定启用PHP模块。因此,我运行了此命令:

a2enmod php7.2

,但我遇到了此错误:

考虑php7.2的依赖性mpm_prefork:
考虑冲突mpm_event的mpm_prefork:
错误:启用模块MPM_Event - 由于冲突而无法进行。它需要首先被禁用!
考虑MPM_PREFORK的冲突MPM_Worker:
错误:无法为php7.2启用依赖性mpm_prefork,流产

因此我决定通过运行以下命令来禁用mpm:

sudo a2dismod mpm_prefork
sudo a2dismod mpm_worker
sudo a2dismod mpm_event

然后重新启动apache:

systemctl restart apache2

然后启用php7.2(我的安装版本)(我的安装版本):

<<<<<<<<<<<<<<<<<代码> sudo a2enmod php7.2

现在一切正常。

In case we are in the same page do following

sudo apt-get install php -y
sudo apt-get install php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y

To enable PHP 7.2 FPM in Apache2 do:

a2enmod proxy_fcgi setenvif

a2enconf php7.2-fpm

update 2: Apache downloads .php file instead of rendering

After that, I faced above issue. There are similar questions like this.

I don't know why but it only happened for my .php files in /var/www/html/ root folder. everything was ok for sub-directories. (for example wordpress and phpmyadmin worked fine)

So here is my solution. I decided to enable php module. so I ran this command:

a2enmod php7.2

but I got this errors:

Considering dependency mpm_prefork for php7.2:
Considering conflict mpm_event for mpm_prefork:
ERROR: Module mpm_event is enabled - cannot proceed due to conflicts. It needs to be disabled first!
Considering conflict mpm_worker for mpm_prefork:
ERROR: Could not enable dependency mpm_prefork for php7.2, aborting

so I decided to disable mpm by running following commands:

sudo a2dismod mpm_prefork
sudo a2dismod mpm_worker
sudo a2dismod mpm_event

then restart apache:

systemctl restart apache2

then enable php7.2 (my installed version):

sudo a2enmod php7.2

and right now everything works fine.

跨年 2025-02-20 07:38:19

确保脚本

<?php

在应执行的代码之前包含。 &lt;?php之间不应有空间。

Make sure the script contains

<?php

before the code that should be executed. There should be no space between <? and php in this.

橙幽之幻 2025-02-20 07:38:19

我在php 7.1上遇到了这个问题,该问题与高塞拉(OS X 10.13.5)有关,编辑/etc/apache2/httpd.conf以及以下更改有帮助:

  1. dockmuntion

      loadModule php7_module libexec/apache2/libphp7.so
     
  2. 粘贴在结尾处粘贴

     &lt; ifModule php7_module&gt;
        AddType应用程序/X-HTTPD-PHP .php
        AddType应用程序/X-HTTPD-PHP-source .phps
    
        &lt; ifModule dir_module&gt;
            DirectoryIndex index.html index.php
        &lt;/ifmodule&gt;
    &lt;/ifmodule&gt;
     

I faced this issue on php 7.1 that comes with High Sierra (OS X 10.13.5), editing /etc/apache2/httpd.conf with following changes helped:

  1. Uncomment this line

    LoadModule php7_module libexec/apache2/libphp7.so
    
  2. Paste following at the end

    <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>
    
栀子花开つ 2025-02-20 07:38:19

在我的Ubuntu 14.04,Apache 2.4,PHP 5.5.9安装中,我尝试使用sample.php on/var/www/html(默认文档root)尝试使用sample.php。
因此,问题在我的虚拟服务器配置上。
解决方案是在包含.php的目录DEF上包括此行:

    php_admin_flag engine on

on my ubuntu 14.04, apache 2.4, php 5.5.9 install, I tried with a sample.php on /var/www/html (default document root) and it worked ok.
So the problem was on my Virtual Servers config.
The solution was to include, on the Directory def containing the .php, this line:

    php_admin_flag engine on
杀手六號 2025-02-20 07:38:19

此问题的另一个可能原因可能是您正在尝试从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>

Another possible cause of this problem could be that you are trying to run the script in a "user directory" from the UserDir module. Running PHP scripts in user directories is disabled by default. You will run into this problem if the script is in the public_html directory in your home folder and you are trying to access it from http://localhost/~your_username.

To fix this, open up /etc/apache2/mods-enabled/php7.2.conf. You must comment or delete the tag block at the bottom that reads

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>
柠栀 2025-02-20 07:38:19

我有类似的问题,但在我的情况下解决方案有所不同。
我持有PHP代码的文件称为“ somename.html”
将其更改为“ somename.php” 工作正常

i had similar problem but in my case solution was different.
my file that held php code was called "somename.html"
changed it to "somename.php" worked fine

_畞蕅 2025-02-20 07:38:19

用于运行PHP 7的新鲜灯设置7
编辑文件/etc/httpd/conf/httpd.conf
注意:在更改任何内容之前,请确保为其做出备份。

将其粘贴到文件的底部:

<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>

然后,搜索LoadModule并粘贴以下行:

LoadModule php7_module modules/libphp7.so

此行只要求HTTPD加载PHP 7模块

,然后重新启动HTTPD

For fresh setup of LAMP running php 7
edit the file /etc/httpd/conf/httpd.conf
Note: make sure to make backup for it before changing anything.

Paste this at the very bottom of the file:

<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>

Then, search for LoadModule and paste the following line:

LoadModule php7_module modules/libphp7.so

This line will simply ask httpd to load the php 7 module

Then restart httpd

英雄似剑 2025-02-20 07:38:19

检查您为 php 安装的所有软件包使用:

yum list installed | grep remi

安装所有相关 PHP 软件包,尤其是机器上的 php-devel

Check all the packages you have installed for php using:

yum list installed | grep remi

Install all relevant php packages, especially php-devel on your machine.

哑剧 2025-02-20 07:38:19

如果您使用的是Apache 2.4

Add AddType application/x-httpd-php .php to your httpd.conf file if you are using Apache 2.4

吻安 2025-02-20 07:38:19

就我而言,没有加载PHP模块。尝试以下操作:

  1. 检查加载哪些模块:apache2ctl -m。查找模块,例如php7_module(共享)
  2. 如果未列出PHP模块,请尝试加载与您的PHP版本相对应的模块。就我而言,PHP数据包为libapache2-mod-php7.3。因此,我做到了:a2enmod php7.3,问题解决了。

In my case the php module was not loaded. Try this:

  1. Check which modules are loaded: apache2ctl -M. Look for module like php7_module (shared)
  2. If no php module is listed, then try to load the module that corresponds to your php version. In my case the php packet is libapache2-mod-php7.3. So I did: a2enmod php7.3 and the problem was solved.
甜中书 2025-02-20 07:38:19

使用Debian 10测试的安装Apache + PHP7的最简单方法:

apt-get update -y
apt-get install apache2 php7.0 libapache2-mod-php  -y
sudo service apache2 restart

Easiest way to install Apache + php7 tested using Debian 10:

apt-get update -y
apt-get install apache2 php7.0 libapache2-mod-php  -y
sudo service apache2 restart
临走之时 2025-02-20 07:38:19

这是在我的.htaccess

DirectoryIndex index.html index.htm

index.html中包含php代码的。默认情况下,PHP不会以HTM*为PHP代码之类的范围处理文件。

您可以通过将以下内容添加到.htaccess:

<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>

This was in my .htaccess

DirectoryIndex index.html index.htm

index.html contained PHP code. By default, PHP won't process files with extentions like htm* as PHP code.

You can override this, by adding the following to .htaccess:

<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>
风渺 2025-02-20 07:38:19

如果您具有这样的配置:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/var/www/example.com"

    <FilesMatch "\.php$">
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

httpd.conf中的下一行

LoadModule proxy_module lib/httpd/modules/mod_proxy.so
LoadModule proxy_fcgi_module lib/httpd/modules/mod_proxy_fcgi.so

对我有用

If you have configuration like this:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/var/www/example.com"

    <FilesMatch "\.php$">
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

Uncomment next lines in your httpd.conf

LoadModule proxy_module lib/httpd/modules/mod_proxy.so
LoadModule proxy_fcgi_module lib/httpd/modules/mod_proxy_fcgi.so

It works for me

无需解释 2025-02-20 07:38:19

尝试重新启动Apache服务器。这是我犯的错误 - 安装PHP后忘记重新启动服务器!

service httpd restart

Try restarting the apache server. This was the mistake I had made - forgetting to restart the server after installing php!

service httpd restart
謸气贵蔟 2025-02-20 07:38:19

对于 php7.3。*您可以尝试安装这些模块。它对我有用。

sudo apt-get install libapache2-mod-php7.3

sudo service apache2 restart

For php7.3.* you could try to install these modules. It worked for me.

sudo apt-get install libapache2-mod-php7.3

sudo service apache2 restart
不…忘初心 2025-02-20 07:38:19

我认为它显示代码而不是结果的问题是它不会去本地主机。重新检查您要进入的地址。您要去本地文件目录还是本地主机。

从您发送的屏幕截图中,它将转到您的计算机,而不是到Localhost。

“ file:///”应该是“ localhost/”

I think the problem that it is showing code instead of the result is that it is not going to local host . recheck what address u r going in. are u going to a local file directory or to the local host.

from the screenshot u sent it is going to ur computer not to the localhost.

"file:/// " it should be "localhost/"

最初的梦 2025-02-20 07:38:19

重新安装mcrypt模块对我有用。

$sudo apt-get install php5-mcrypt
$sudo php5enmod mcrypt

Reinstalling the mcrypt module worked for me.

$sudo apt-get install php5-mcrypt
$sudo php5enmod mcrypt
注定孤独终老 2025-02-20 07:38:19

我有一种情况,我意外地开始将文件目录解释为root。它从我的文件文件夹中添加了.htaccess文件,该文件将阻止所有PHP

# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
  php_flag engine off
</IfModule>

底线检查root上的.htaccess文件。

I had a case that I accidentally started untaring my files directory in root. It added the .htaccess file from my files folder that would block all php

# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
  php_flag engine off
</IfModule>

Bottom line check the .htaccess file on root.

各自安好 2025-02-20 07:38:19

只是花了几个小时尝试获得 php 5,以与Windows 10上的Apache 2.4一起运行。最后,对我来说,这是 httpd.conf loadmodule中的错别字。引起人们的注意和精确的模块路径穿过 上次答案这个 denny2018 的Apachelunge线程。谢谢你!

两个晚上后,我发现...我的目录是编写的c:(lower case)

我有loadmodule php5_module“ c:/php/php5apache2.dll”,但是 正确正确:

LoadModule php5_module "C:/php/php5apache2_4.dll"

Apache 2.4 的 对我有用的完整片段:

# PHP
LoadModule php5_module "C:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
<FilesMatch \.php
gt;
    SetHandler application/x-httpd-php
</FilesMatch>

刚刚尝试了 php 7 。那里的loadModule httpd.conf 中为apache 2.4读取

LoadModule php7_module "C:/php/php7apache2_4.dll"

当前显示php手册 c:/php/php5apache2.dll当然需要调整哪个。

Just spent hours of trying to get PHP 5 to run with Apache 2.4 on Windows 10. Finally for me it was a typo in httpd.conf LoadModule. Drew attention to writing and exact module path through the last answer in this apachelounge thread of denny2018. Thank you!

After two nights I discovered... My directory was written c: (lower case)

I had LoadModule php5_module "c:/php/php5apache2.dll" but correct for apache 2.4 is:

LoadModule php5_module "C:/php/php5apache2_4.dll"

So I also missed the _4 before (for apache 2.4). The full snippet that worked for me:

# PHP
LoadModule php5_module "C:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
<FilesMatch \.php
gt;
    SetHandler application/x-httpd-php
</FilesMatch>

Just tried PHP 7. There the LoadModule line in httpd.conf for Apache 2.4 reads

LoadModule php7_module "C:/php/php7apache2_4.dll"

Currently php manual shows up c:/php/php5apache2.dll which of course needs to be adjusted.

沉默的熊 2025-02-20 07:38:19

在我的情况下(apache/2.4.34),

在删除特定模块之后

“ loadModule php7_module libexec/apache2/libphp7.so”

“/etc/apache2/httpd.conf”

我的问题消失了。

in my case (Apache/2.4.34),

after uncommenting the specific module

"LoadModule php7_module libexec/apache2/libphp7.so"

from

"/etc/apache2/httpd.conf"

my problem was gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文