PHP代码尚未执行,但代码在浏览器源代码中显示
我试图在项目上执行一些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");
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
听起来您的配置有问题,您可以检查以下几件事:
确保使用url使用url
http://localhost/file.php 不通过本地文件访问
文件://localhost/www/file.php
确保已安装并正确运行PHP。这听起来可能很愚蠢,但您永远不会知道。一种简单的检查方法是从命令行中运行
php -v
,查看返回版本信息还是任何错误。确保php模块在apache的httpd.conf内部列出 ,这应该是
load> loadsmodule php5_module“ c:/php/php/php5apache2_2.dll”
在文件中。搜索loadModule php
,并确保其前面没有注释(;
)。确保Apache的httpd.conf文件中具有php mime类型。这应该是
addType应用程序/X-HTTPD-PHP .php
。这告诉Apache以php的速度运行.php
文件。搜索AddType,然后确保有php的条目,,并且它是未注销的。确保您的文件在其上具有
.php
扩展名,或第3点中的MIME定义中指定的任何扩展名,否则将不会以Php。执行。
确保您不使用短标签 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:
Make sure you are accessing your file over your webserver using an URL like
http://localhost/file.php
not via local file accessfile://localhost/www/file.php
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 forLoadModule 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 withshort_open_tag=On
if you have code that relies on them).And lastly check the PHP manual for further setup tips.
PHP7:
php7 :
我正在Ubuntu上运行Apache,我的问题是
/etc/apache2/mods-available/php5.conf
文件丢失了这一点:我将其添加回了,PHP是正确解析的PHP文件。
I'm running Apache on Ubuntu and my issue was that the
/etc/apache2/mods-available/php5.conf
file was missing this:I added it back in and php was parsing php files correctly.
注意PHP 7用户,将其添加到您的httpd.conf文件中:
note for php 7 users, add this to your httpd.conf file:
您只是将PHP文件打开到浏览器中。您必须使用localhost URL打开它。如果您直接从目录打开文件,则无论如何都不会执行PHP代码。
使用:
http://locahost/index.php
或http: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
orhttp: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.我发现了另一个引起此问题并已经解决的问题。我不小心将脚本保存在
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.我要发布此答案,因为我的VirtualMin/Webmin Admin界面认为禁用我的PHP引擎是一个好主意。
另外,请务必检查您的网站配置文件是否与此特定主机或VirtualHost相关的任何网站配置文件中都有任何PHP_ADMIN_VALUE在其中关闭php,诸如此类:
如有疑问,请发表评论...
并重新启动您的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:
When in doubt, comment it...
And restart your webserver.
这再次发生在我身上,服务器下载HTML文件,而不是处理。我已经有一段时间在计算机上使用了Web服务器Apache,与此同时,Ubuntu更新了最初安装的LTS的两个版本。现在,
PHP像这样工作了:
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
So the php worked after like so:
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.
我知道这听起来应该很愚蠢...但是很少发生。
检查您是否试图从
中
PS&GT 执行PHP;您可以注意到,如果您从shell中写入
它,则用代码回答(这意味着PHP功能)。
I know it should sound silly... but seldom it happens.
Check if you are trying to execute php from
and not from
ps> you can notice that if you write from shell
it answer with the code (it means php functions)..
我已经通过卸载XAMPP并安装WAMP解决了这一问题。
感谢您的帮助。
I've solved this by uninstalling XAMPP, and installing WAMP.
Thanks for the help.
如果我们在同一页面中,请按照
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
,但我遇到了此错误:
因此我决定通过运行以下命令来禁用mpm:
然后重新启动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:
so I decided to disable mpm by running following commands:
then restart apache:
systemctl restart apache2
then enable php7.2 (my installed version):
sudo a2enmod php7.2
and right now everything works fine.
确保脚本
在应执行的代码之前包含。
&lt;?
和php
之间不应有空间。Make sure the script contains
before the code that should be executed. There should be no space between
<?
andphp
in this.我在php 7.1上遇到了这个问题,该问题与高塞拉(OS X 10.13.5)有关,编辑
/etc/apache2/httpd.conf
以及以下更改有帮助:dockmuntion
粘贴在结尾处粘贴
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:Uncomment this line
Paste following at the end
在我的Ubuntu 14.04,Apache 2.4,PHP 5.5.9安装中,我尝试使用sample.php on/var/www/html(默认文档root)尝试使用sample.php。
因此,问题在我的虚拟服务器配置上。
解决方案是在包含.php的目录DEF上包括此行:
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:
此问题的另一个可能原因可能是您正在尝试从
userDir
模块中运行脚本。默认情况下,在用户目录中运行PHP脚本。如果脚本位于主文件夹中的public_html
目录中,您将遇到此问题,并且您正在尝试从http:// localhost/〜your_username
访问它。要解决此问题,请打开
/etc/apache2/mods-enabled/php7.2.conf
。您必须在读取的底部发表评论或删除标签块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 thepublic_html
directory in your home folder and you are trying to access it fromhttp://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我有类似的问题,但在我的情况下解决方案有所不同。
我持有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
用于运行PHP 7的新鲜灯设置7
编辑文件/etc/httpd/conf/httpd.conf
注意:在更改任何内容之前,请确保为其做出备份。
将其粘贴到文件的底部:
然后,搜索LoadModule并粘贴以下行:
此行只要求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:
Then, search for LoadModule and paste the following line:
This line will simply ask httpd to load the php 7 module
Then restart httpd
检查您为 php 安装的所有软件包使用:
安装所有相关 PHP 软件包,尤其是机器上的 php-devel 。
Check all the packages you have installed for php using:
Install all relevant php packages, especially php-devel on your machine.
如果您使用的是Apache 2.4
Add
AddType application/x-httpd-php .php
to your httpd.conf file if you are using Apache 2.4就我而言,没有加载PHP模块。尝试以下操作:
apache2ctl -m
。查找模块,例如php7_module(共享)
libapache2-mod-php7.3
。因此,我做到了:a2enmod php7.3
,问题解决了。In my case the php module was not loaded. Try this:
apache2ctl -M
. Look for module likephp7_module (shared)
libapache2-mod-php7.3
. So I did:a2enmod php7.3
and the problem was solved.使用Debian 10测试的安装Apache + PHP7的最简单方法:
Easiest way to install Apache + php7 tested using Debian 10:
这是在我的.htaccess
index.html中包含php代码的。默认情况下,PHP不会以HTM*为PHP代码之类的范围处理文件。
您可以通过将以下内容添加到.htaccess:
This was in my .htaccess
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:
如果您具有这样的配置:
httpd.conf中的下一行
对我有用
If you have configuration like this:
Uncomment next lines in your httpd.conf
It works for me
尝试重新启动Apache服务器。这是我犯的错误 - 安装PHP后忘记重新启动服务器!
Try restarting the apache server. This was the mistake I had made - forgetting to restart the server after installing php!
对于 php7.3。*您可以尝试安装这些模块。它对我有用。
For php7.3.* you could try to install these modules. It worked for me.
我认为它显示代码而不是结果的问题是它不会去本地主机。重新检查您要进入的地址。您要去本地文件目录还是本地主机。
从您发送的屏幕截图中,它将转到您的计算机,而不是到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/"
重新安装
mcrypt
模块对我有用。Reinstalling the
mcrypt
module worked for me.我有一种情况,我意外地开始将文件目录解释为root。它从我的文件文件夹中添加了.htaccess文件,该文件将阻止所有PHP
底线检查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
Bottom line check the .htaccess file on root.
只是花了几个小时尝试获得 php 5,以与Windows 10上的Apache 2.4一起运行。最后,对我来说,这是 httpd.conf
loadmodule
中的错别字。引起人们的注意和精确的模块路径穿过 上次答案这个 denny2018 的Apachelunge线程。谢谢你!我有
loadmodule php5_module“ c:/php/php5apache2.dll”
,但是 正确正确:Apache 2.4 的 对我有用的完整片段:
刚刚尝试了 php 7 。那里的
loadModule
在 httpd.conf 中为apache 2.4读取当前显示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!I had
LoadModule php5_module "c:/php/php5apache2.dll"
but correct for apache 2.4 is:So I also missed the
_4
before (for apache 2.4). The full snippet that worked for me:Just tried PHP 7. There the
LoadModule
line in httpd.conf for Apache 2.4 readsCurrently php manual shows up
c:/php/php5apache2.dll
which of course needs to be adjusted.在我的情况下(apache/2.4.34),
在删除特定模块之后
我的问题消失了。
in my case (Apache/2.4.34),
after uncommenting the specific module
from
my problem was gone.