Apache shows PHP code instead of executing it
You must enable php! Check the folder mods-enabled
in the Apache directory (default: /etc/apache2/
) to see if you find a file named php
. I don't remember the extension but I think it's .so
.
Also check in /var/log/apache2/error.log
to see if you have any other errors.
PHP code is not being executed, but the code shows in the browser source code
Sounds like there is something wrong with your configuration, here are a few things you can check:
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).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
And lastly check the PHP manual for further setup tips.
PHP not interpreted, showing in view source
Are you sure you have php installed? If it is you need to make sure that apache is associating .php
files with the php handler. Look for an entry similar to the following in /etc/apache/apache.conf
LoadModule php5_module modules/libphp5.so
and
application/x-httpd-php php php5
upon changing the file you will need to restart apache via sudo service httpd restart
How do I prevent PHP displaying as plain text on my browser?
The title of your post is wrong. It should be something like "How to prevent My php code from displaying on the screen/browser".
Below are possible scenario.
If your PHP code is being shown in the browser, it implies that your server has not been setup to serve PHP scripts. Below are among few things you will need to do.
1.)PHP Xampp or Wamp Installation:. I prefer Xampp. First step is to ensure that PHP is installed and running correctly. You can download and Install Xampp if you have not done so.
An easy way to check if php is installed is to run php -v from a command line and see if returns version information or any errors
2.) Restarting your Server: If you have alter any files prior to this event, you will need to restart your server
3.) PHP File Extension Name: Ensure that you properly save your code as .php file extension name. Code save as .html or .txt will not be executable.
4.) In case if you are using Xampp. Ensure that your php files resides on htdocs folder if you are using xampp Eg
C:\xampp\htdocs\your-php-projects.
5.)Misconfiguration: This may be the last thing to check. But if you care about it, You can also check for misconfigurations.
For example: In Apache’s httpd.conf file, you will need to make sure that the line Eg as case may be "LoadModule php5_module" has been uncommented and that there is no semi-colon (;) at the beginning of the line.
Apache is downloading php files instead of displaying them
The correct AddType for php is application/x-httpd-php
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Also make sure your php module is loaded
LoadModule php5_module modules/mod_php55.so
When you're configuring apache then try to view the page from another browser - I've had days when chrome stubbornly caches the result and it keeps downloading the source code while in another browser it's just fine.
How to make viewforum.php?foo=bar request serve a file instead of a PHP code?
Interesting problem indeed! force me to dig many Apache docs. In the end solution was simple i.e. to escape ?
so that Apache doesn't treat ?
and part after that as query string.
You may use this rewrite rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/forum/viewforum\.php$ [NC]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI}\%3F%{QUERY_STRING} [L,NC]
PS: \%3F
is escaped ?
so make Apache load /forum/viewforum.php?f=2&start=25
as a file.
Related Topics
Multiple Inputs With Same Name Through Post in PHP
Sort Array of Objects by One Property
Simplest Way to Profile a PHP Script
Prevent Direct Access to a PHP Include File
Accessing $_Cookie Immediately After Setcookie()
PHP File_Get_Contents() Returns "Failed to Open Stream: Http Request Failed!"
How to Sort an Array of Associative Arrays by Value of a Given Key in PHP
How to Create a Pdo Parameterized Query With a Like Statement
Create Subdomains on the Fly With .Htaccess (PHP)
How to Check If a Url Exists Via PHP
How to Convert an Image to Base64 Encoding
PHP Elvis Operator VS Null Coalescing Operator