include external php files

include external php files

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

Stefan Bogdanescu

Founder & Senior Architect

Moving Beyond include for Shared PHP Logic

Using raw include or require statements to pull in database connections and timeout checks is a common pattern you'll encounter when transitioning from procedural PHP to modern architectures. In your specific example, you are loading check_timeout.php and connect_to_db.php at the top of every page. While this consolidates logic, relying on these functions inside HTML templates introduces significant architectural friction and security risks that often go unnoticed until scaling becomes an issue.

The immediate red flag in your code snippet is the use of the deprecated mysql_close() function combined with mysql_* extensions. This extension has been removed from core PHP for years. You are likely using a legacy application, but continuing to manage raw connections via included files prevents you from leveraging modern connection pooling and environment-specific configuration features found in frameworks like Laravel or Symfony.

The Problem with Direct Includes

When you place include "connect_to_db.php" at the top of every HTML file, you create a tight coupling between your presentation layer and your infrastructure layer. Every single page now knows exactly how to connect to the database. If you need to switch databases for a specific environment (e.g., moving from a local SQLite to a production MySQL cluster), you must remember to update every single include file or risk runtime errors.

Furthermore, includes execute code immediately. If connect_to_db.php contains logic that initializes heavy resources, your HTML rendering is blocked until that logic completes. This creates performance bottlenecks where the server spends CPU cycles parsing and connecting before it even starts generating HTML output.

The Standard: Autoloading and Service Containers

The industry-standard approach for handling shared dependencies like database connections is to decouple them from the request lifecycle using a service container or an autoloader. Instead of manually including files in every script, you define a single entry point (like public/index.php) that initializes your application context.

In a modern framework like Laravel, this looks like defining a connection in the config/database.php file and injecting it into controllers or views. The framework handles the loading of these dependencies automatically. If you are building a custom framework without an autoloader, you should utilize SplClassLoader to map namespaces to files. This ensures that when you need a database instance, you can instantiate a class like new Database\Connection, and the system will locate the underlying implementation file dynamically rather than relying on brittle string-based includes.

Refactoring Legacy Code

If you are stuck with existing legacy code using mysql_* functions and want to improve maintainability without rewriting everything immediately, consider creating a dedicated bootstrap file. Instead of including logic in HTML files, create a config.php or bootstrap.php that sets up the environment variables and initializes the connection object.

You can then modify your HTML templates to source this bootstrap file once at the very beginning:

<?php
// config/bootstrap.php
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/timeout.php';

// Initialize singleton or global instance
$database = new Database\Connection();
// Set timeout logic if applicable
set_timeout_handler($database->getTimeoutHandler());

Then, your main entry point becomes the sole responsibility for setting up these dependencies:

<?php
// public/index.php
require_once __DIR__ . '/config/bootstrap.php';

// Now you can access $database anywhere in your app without including files manually
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Render view
echo $view->render('home');

This approach keeps the "glue" code out of your HTML and ensures that connection logic is centralized. It also allows you to easily switch connection drivers by changing a single configuration line rather than hunting through dozens of included files.

Security Considerations

Manually including files exposes your application to path traversal vulnerabilities if the include paths are not strictly validated. When using framework-based autoloading or service containers, these risks are mitigated because the file locations are defined statically at compile time or load time, not dynamically based on user input during the request cycle. Additionally, by moving connection logic out of HTML files, you reduce the surface area for SQL injection attacks if your connect_to_db.php file accidentally exposes raw query strings to the template engine.

For developers looking to migrate from this procedural style to a more robust architecture, resources like https://laravelcompany.com offer detailed guides on setting up service containers and managing database connections in a scalable way. The goal is always to separate concerns: HTML handles display, configuration handles infrastructure, and logic handles business rules.

Our network