Getting the base path/URL

Getting the base path/URL

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

Stefan Bogdanescu

Founder & Senior Architect

Why You Shouldn't Include Files via http:// URLs in PHP

Attempting to include a file using a full URL string like http://localhost/mysite/_include/db/qry.php is a common stumbling block for developers moving from simple scripts to structured web applications. The immediate error, "http wrapper is disabled," stems from a deliberate security configuration in modern PHP distributions. However, the root cause isn't just a server setting; it's a fundamental misunderstanding of how file paths work within the request lifecycle.

The code snippet provided attempts to construct a base URL by combining the protocol, host name, and directory path derived from $_SERVER['PHP_SELF']. While this logic correctly outputs http://localhost/mysite/, using this string for require_once is flawed for two critical reasons: it violates security best practices, and it breaks the separation between your application's internal file system structure and the public web root.

The Security Constraint: allow_url_include

The specific error message regarding the disabled http wrapper refers to the allow_url_include directive in your php.ini configuration. By default, this is set to Off. This restriction exists because allowing PHP to fetch remote resources via HTTP creates a massive attack surface. It effectively turns your server into a proxy that can execute arbitrary code from untrusted sources if an attacker manages to inject a malicious URL into your application.

Enabling this setting by changing allow_url_include to On is never recommended, even in local development environments like XAMPP. It undermines the principle of least privilege and exposes your server to potential remote code execution vulnerabilities. The error occurs because the PHP runtime refuses to open a file stream using the HTTP protocol when this directive is disabled.

The Correct Approach: Relative Paths

The solution lies in treating $_SERVER['DOCUMENT_ROOT'] or the web root as the anchor point for all includes, rather than constructing full URLs. You should never include files using their public URL; you must always use their filesystem path relative to your current directory or absolute paths starting from the document root.

Instead of calculating a URL and concatenating it with a filename, determine the script's physical location and navigate from there. Here is a robust pattern for constructing the base path that avoids URL construction entirely:

function getBasePath() {
    // Determine the directory where this script resides
    $currentDir = dirname($_SERVER['SCRIPT_FILENAME']);
    
    // Move up to the project root if necessary, or define it explicitly
    // For example, if includes are at /mysite/_include/db/ relative to webroot:
    return dirname($currentDir) . '/_include/db';
}

$base = getBasePath();
require_once $base . '/qry.php';
require_once $base . '/functions.php';

This approach uses filesystem paths (/mysite/_include/db/qry.php) rather than URL strings. PHP resolves these paths relative to the current working directory, which is typically the web root when running from a browser request. This method bypasses the allow_url_include restriction entirely because it relies on local file system access.

Handling Virtual Hosts and Deployment

A significant pitfall arises when moving from a local environment (like XAMPP with localhost) to a production server. In your original code, $currentPath derived from $_SERVER['PHP_SELF'] might return /myproject/index.php. If you strip the filename and dirname, you get /myproject.

If your actual file structure places includes directly in the root (e.g., mysite/_include/db/...), you must ensure your path calculation aligns with that specific hierarchy. Hardcoding paths relative to $_SERVER['DOCUMENT_ROOT'] is often safer than parsing $_SERVER['PHP_SELF'], as the latter can be manipulated by clients or behave differently depending on URL rewriting rules (like .htaccess).

Modern Alternatives: Autoloading and Dependency Injection

While fixing the immediate include path issue solves the error, relying on manual require_once statements with hardcoded paths creates tight coupling. As your application grows, managing these dependencies manually becomes unsustainable.

Frameworks like Laravel solve this by providing a robust autoloading system. In a Laravel context, you do not manually calculate base paths or use require_once for class files. Instead, you rely on the Composer autoloader (composer dump-autoload) and the framework's service container. If you are starting a new project today, consider using Laravel to handle dependency management out of the box. This eliminates the need to manually construct include paths and ensures your application structure remains scalable and secure. You can learn more about modern PHP architecture and best practices at https://laravelcompany.com.

By switching from URL-based includes to relative filesystem paths or framework-based autoloading, you eliminate security risks and ensure your code behaves consistently across all hosting environments.

Our network