how to require_once in codeigniter

how to require_once in codeigniter

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

Stefan Bogdanescu

Founder & Senior Architect

Why require_once Fails in CodeIgniter and How to Load Libraries Correctly

Attempting to extend a library in CodeIgniter by manually invoking require_once on the core file before using $this->load->library() is a common stumbling block, especially when transitioning from a local development environment like XAMPP to a live Linux server. The behavior you are experiencing—where the code works locally but throws a "failed to open stream" error on production—is rarely about the logic of extending a class. It is almost exclusively a path resolution issue driven by how the web server interprets file paths versus how PHP does when running in CLI or a different working directory context.

When you execute require_once('ion_auth.php') without an absolute path prefix, PHP resolves the filename relative to the current working directory of the script, not necessarily the root of your project or the location of the library itself. On XAMPP, the default document root often aligns perfectly with the project structure, making a relative path like 'home/SITE_NAME/...' coincidentally correct if the script is served from that specific location. However, on a standard Linux web server (Apache/Nginx), the entry point for your application is typically index.php. If you are inside a controller or a library being loaded by CI, the "current directory" is often the project root or a subdirectory thereof, not /home/SITE_NAME/public_html/.

Consequently, the path /home/SITE_NAME/public_html/FOLDER_NAME/application/libraries/ion_auth.php provided in your attempts is likely an absolute filesystem path. While this works if PHP's open_basedir restrictions allow it and the script is executed from a directory where that path is valid, it breaks the abstraction layer CodeIgniter relies on. Hardcoding absolute paths into your application code couples your logic to a specific server environment, which is why it fails when you deploy to another machine or even a different environment on the same machine.

To extend a library properly in CodeIgniter, you should leverage the framework's built-in loading mechanism rather than bypassing it with require_once. The standard workflow involves placing your extended class in the application/libraries directory and using the loader to instantiate it. If you must include an external file (like Ion Auth) that sits outside the standard CI library structure, you need to determine the absolute path dynamically at runtime.

You can achieve this by utilizing the base path constants provided by CodeIgniter itself. Instead of hardcoding /home/SITE_NAME/..., use $this->config->base_url() for web paths or APPPATH and PATH (depending on your CI version) to construct the file path relative to the application root. For example:

$path = APPPATH . 'libraries/ion_auth.php';
if (file_exists($path)) {
    require_once $path;
} else {
    // Fallback or error handling
    show_error('Library not found');
}

This approach ensures that regardless of whether you are running on XAMPP, a shared hosting environment, or a dedicated server, the path resolution remains consistent because it is anchored to the application's own structure.

Once the file is successfully included via require_once, you can proceed with your extension logic. However, keep in mind that loading a library this way manually often bypasses CodeIgniter's service detection and initialization hooks unless you are careful to replicate what $this->load->library() would do. If the goal is simply to extend functionality, it is often cleaner to place the extended class directly within application/libraries with the same namespace as the original file, ensuring no conflicts arise during autoloading or manual loading attempts.

If you are maintaining legacy codebases where Ion Auth is tightly coupled and cannot be moved, ensure that your server's open_basedir restriction includes the directory containing the library. A common pitfall on Linux servers is attempting to access paths outside the configured docroot, which PHP will block for security reasons. Always verify file permissions as well; a 403 Forbidden error might look like a "file not found" issue if the web server user cannot read the specific directory.

For modern migrations or new projects, consider using Composer to manage such dependencies. This removes the ambiguity of relative paths entirely and integrates the library into your vendor directory, where it can be autoloaded cleanly without manual require statements cluttering your controllers or libraries.

Our network