Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding the "Unknown: failed to open stream" Session Error

You are encountering a classic PHP session conflict caused by misusing auto_prepend_file in your .htaccess configuration. The error message Warning: Unknown: failed to open stream: No such file or directory indicates that PHP is trying to include prepend.php before processing your script, but it cannot find the file in the location where it expects it to be.

The root cause lies in how auto_prepend_file interacts with your Apache rewrite rules and the specific syntax used to enable it. In your .htaccess file, you have configured the following:

php_value auto_prepend_file "prepend.php"

This directive tells the PHP engine to execute a file before every script request. However, there are two critical issues here that lead to your fatal errors.

The RewriteRule Trap

Your .htaccess contains this logic:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This rule attempts to map any request (like index.html) to a corresponding PHP file (index.php). When you access your root URL or a directory, Apache rewrites the request to index.php. If prepend.php is located in a different directory than where the rewritten script resides, or if the path provided to auto_prepend_file is relative and resolved incorrectly by the web server, the stream open fails.

Furthermore, auto_prepend_file paths must be absolute. Relative paths like "prepend.php" often fail when combined with rewrite rules because the physical file location might not match the logical URL path. You should specify the full filesystem path or ensure the file is in a standard include path like include/.

The Double-Session Initialization Problem

Even if you fix the file path, your session logic contains a fatal flaw that will cause unpredictable behavior across different entry points.

In classes/class.Login.php, you have this check inside the constructor:

if (session_id() == "") {
    session_start();    
}

And later in _setSession():

if (session_id() == '') {
    session_start();    
}

You also have a separate prepend.php that runs this exact logic:

if (!isset($_SESSION) ) {
    session_start();    
}

The issue is timing and scope. When auto_prepend_file executes, it starts a session. Then, your main script (index.php or Login.php) also attempts to start a session if it thinks one doesn't exist. While PHP usually handles multiple session_start() calls gracefully by reusing the existing session ID, mixing this with file prepending creates a fragile architecture. If the prepended file fails (due to the path error mentioned above), your main script tries to start the session again, potentially creating a new session or throwing errors depending on the PHP version and server configuration.

The Correct Approach

To resolve the failed to open stream error and stabilize your session handling:

  1. Fix the Path: Change php_value auto_prepend_file "prepend.php" to an absolute path, for example:

    php_value auto_prepend_file "/full/path/to/prepend.php"
    

    Alternatively, move prepend.php into your project's includes/ directory and ensure the path is relative to the document root in a way that works with your rewrite rules.

  2. Consolidate Session Logic: Remove session_start() from prepend.php. Let your main application logic handle session initialization. This gives you full control over when sessions are created, allowing for better error handling and cookie configuration (e.g., setting secure or httpOnly flags).

  3. Remove Redundant Checks: Ensure you only call session_start() once per request lifecycle. Your current code checks session_id() multiple times in different classes. Rely on a single initialization point, preferably at the very top of your entry script before any output is sent.

By centralizing session management and fixing the file path resolution, you eliminate the "Unknown: failed to open stream" warning and ensure your authentication flow behaves consistently regardless of which script initiates the request.

Our network