Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP inside an .html file is shown as text or ignored

Error<?php echo ... ?> appears in the page source

Short answer

Apache runs PHP only for the file types it is told to hand to PHP, normally .php. An .html file is sent as plain HTML, so the PHP tags are never run. Rename the file to .php, or add .html to the handler pattern in the Apache config. Without any handler, even .php files show their source.

  • Applies to PHP 8.3, Apache 2.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    View the page source in the browser. Visible PHP tags mean the file was sent without being run. Then test a plain .php file in the same folder.

    echo '<?php echo "php works";' | sudo tee /var/www/html/test.php
    curl http://localhost/test.php
  2. 2Match what you see, then jump to its fix

    1. 1test.php prints the text, the .html page shows PHP tagsThe file ends in .html and only .php is handled by PHPFix
    2. 2even test.php is shown as source or offered as a downloadNo PHP handler is configured for Apache at allFix

What does the error look like?

  • <?php echo ... ?> appears in the page source
  • the PHP code shows up as text in the browser

Cause 1 of 2

The file ends in .html and only .php is handled by PHP

1Check

The PHP manual's Apache setup hands only files that match .php to PHP, through a FilesMatch block. Look for it in your config.

grep -rn "x-httpd-php" /etc/apache2/ /etc/httpd/ 2>/dev/null

2Fix

The simple fix is to rename the page to .php and update the links and form actions that point to it. If the .html name must stay, add html to the pattern. This sends every .html file through PHP, so use it only where needed.

<FilesMatch "\.(php|html)$">
    SetHandler application/x-httpd-php
</FilesMatch>

3Verify

Test the config, reload Apache and request the page again.

sudo apachectl configtest && sudo systemctl reload apache2
curl http://localhost/page.html

Confirmed by php.net

Did this fix it?

Cause 2 of 2

No PHP handler is configured for Apache at all

1Check

If the grep above prints nothing, Apache has no rule to hand .php to PHP. This happens on a fresh XAMPP or a hand-built install.

apachectl -M | grep -i php
grep -rn "SetHandler" /etc/apache2/ 2>/dev/null

2Fix

Add the handler block from the PHP manual. The manual recommends SetHandler inside FilesMatch over AddType, so files such as exploit.php.jpg are not run as PHP. For new installs it advises PHP-FPM with mod_proxy_fcgi rather than mod_php.

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

3Verify

Reload Apache and request the test file.

sudo systemctl reload apache2
curl http://localhost/test.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep pages that contain PHP named .php from the start, so no server setting is needed.
  • Use the SetHandler and FilesMatch form, not a bare AddType, when you widen the pattern.
  • Remove the test.php file after checking.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.