How to call an HTML file from PHP

How to call an HTML file from PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Call an HTML File from PHP

You cannot use the include or require functions to load a static .html file directly into your PHP script's output stream if that HTML file is intended to render as a standalone page in the browser. The issue lies in how the web server interprets requests versus how PHP handles file inclusion. When you execute include 'test.html';, PHP reads the raw text of test.html and outputs it exactly as written. If test.html contains <html>...</html> tags, the browser will render those tags literally unless they are processed by a second layer of logic.

In your specific scenario, where you want to transition from a login check to a new interface, simply including the file often results in nested structures or broken layouts because the PHP output buffer has already started generating content (like the "you are not the admin" message) before the include happens, or vice versa depending on execution flow.

The standard architectural pattern for this requirement is not inclusion, but redirection. When a user successfully authenticates, the server should stop processing the current request and send the browser to a new URL that serves the next interface. This ensures clean state separation between the authentication logic and the dashboard content.

To achieve this in your code, you need to use header() followed by an exit. Here is how you modify the logic:

<?php

    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if ($username === 'admin' && $password === 'Parthurnax') {
        // Redirect to the target HTML file
        header('Location: test.html');
        exit; 
    }
    else {
        echo 'You are not the admin';
    }
?>

This approach forces the browser to make a new request for test.html. The server then serves that file as an independent page. This is crucial because static HTML files usually expect to be the root of the response, containing their own <html> and <body> tags. Including them inside another PHP block often leads to malformed markup or unexpected behavior if the surrounding PHP code adds its own header/footer wrappers.

If test.html is intended to be a full page with a form that posts back to page.php, redirecting ensures that the browser navigates away from the login script entirely. The URL in the address bar will change to test.html, and the browser will fetch that resource directly. This keeps your routing logic clean: one file handles authentication, another handles the dashboard.

However, there are specific trade-offs to consider with this method. Using a static .html file for dynamic content like an admin dashboard is generally discouraged in modern development. Static files cannot easily share state (like session variables) or include dynamic data without embedding PHP logic directly into them, which breaks separation of concerns. If you need the next page to access database information or user sessions, it is better to make test.html a .php file as well.

If you change test.html to test.php, you can now use include or require safely if you want to render partial content. But for full-page navigation after login, redirection remains the robust choice. You can also pass parameters via URL query strings if the next page needs different initial states, like header('Location: test.html?id=123');.

Another common pitfall occurs when the target file does not exist or is not readable by the web server. Since you are redirecting, a 404 error on the second request will be distinct from the login failure message. Ensure your file paths are relative to the document root and that permissions allow the web server user (often www-data or apache) to read the file.

If you are using a framework like Laravel, this pattern is abstracted away into named routes, but the underlying principle of redirecting after authentication remains the same. For raw PHP projects, explicitly setting headers and exiting immediately after redirection prevents partial page loads where you might see the login form sitting above your dashboard content.

Our network