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

    |
  • Added:
  • |
  • In: Basic 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

This Question Has 19 Answeres | Orginal Question | Guilherme Cardoso

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

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.

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.

Oh God! I just saw your posted image again, you're just opening your php file into browser. you have open is 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.

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> 

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.

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

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.

One possible reason it is not executing is that the file contains only one tag and one line

However adding just one more line that is pressing enter after "

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> 

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 

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.

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

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

  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 .php files 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 .php extension 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 tags in the PHP file (<?), these are deprecated not enabled on all servers by default. Use <?php instead (or enable short tags in your php.ini with short_open_tag=On if 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.php not via local file access file://localhost/www/file.php

And lastly check the PHP manual for further setup tips.

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

php7 :

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

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.

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

Reinstalling the mcrypt module worked for me.

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

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


Search
I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

Connect Social With PHPAns
Top