Why is my website’s PHP code displaying directly on the page?

Chris Wright
Published: 6 September 2022Last updated: 3 November 2023
Share:

PHP code loading directly onto your site page rather than executing usually indicates an issue with the PHP version being defined on your site. Either it’s a broken definition, or the version being loaded isn’t using the correct path. When this happens, it’s best to check your site’s .htaccess files to see if you can isolate the cause.

A PHP version definition on our system should look like this by default:

#+PHPVersion
#="php74"
AddHandler x-httpd-php74 .php
#-PHPVersion

The core line is the one starting with ‘AddHandler’. If your rule looks different to the above (beyond the version number) then it’s best to remove the rule and use the PHP Version tool in your site’s package to manually set this, or copy the above code and add it to your .htaccess to replace it.

Sites migrated from cPanel often have a rule similar to this:

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
# php -- END cPanel-generated handler, do not edit

 

This isn’t a definition that would work on our system, so it would be worth removing it entirely. Once done, the site should then begin parsing the PHP code correctly and loading as normal.