CodeIgniter include common file in view
Attempting to centralize reusable JavaScript or PHP functions across multiple views is a standard requirement for maintaining clean, DRY (Don't Repeat Yourself) codebases. However, the approach used to load these files often reveals fundamental misunderstandings about how web servers handle file paths and security wrappers. The error you are encountering stems from trying to include a local filesystem resource using a URL protocol, which triggers specific server-level restrictions designed to prevent remote code execution.
The core issue lies in the line require(base_url().'application/libraries/common.php');. The base_url() helper returns a full HTTP path starting with your domain name. When PHP executes require or include, it expects a file system path, not a URL string. Even if you enable allow_url_include in your php.ini, this setting only permits including files from remote URLs (like http://other.com/file.php). It does not allow you to treat a local web path as a URL wrapper for local includes.
When the server processes require('http://example.com/path/to/file') while allow_url_include is disabled, it throws the "no suitable wrapper could be found" error. This happens because PHP cannot find an appropriate stream wrapper to handle the request as a file system operation given the current configuration. Enabling allow_url_include is generally considered a security risk and is disabled by default on production servers for good reason; turning it on simply shifts the error from a missing wrapper to a failed connection attempt if the path doesn't resolve externally.
To resolve this, you must convert the URL-based string into an actual filesystem path using realpath() or by utilizing CodeIgniter's internal path constants. The most robust solution involves using FCPATH (Framework Path) combined with realpath() to ensure the file is resolved relative to your application root, regardless of the current script location.
Here is the corrected implementation for your common.php include:
// Inside backend/hotel_view.php or any other view file
// Get the absolute path to the framework directory
$ci_path = realpath(__DIR__) . DIRECTORY_SEPARATOR;
// Navigate up to the application folder if necessary, or use base_path() helper
// For CodeIgniter 3, FCPATH is often set in config/autoload.php
// A safer, direct approach using CI's environment:
$path = FCPATH . 'application/libraries/common.php';
if (file_exists($path)) {
require_once $path;
}
Using FCPATH is preferred because it abstracts the location of your framework files. If you are working with a custom directory structure, ensure that common.php actually resides in application/libraries/. In modern CodeIgniter setups (version 4+), the architecture has shifted to app and Config directories, so the path would be $appPath . 'Common' instead.
A more elegant alternative often used in CodeIgniter is to place this logic in a library class rather than a raw PHP file. You can define your functions inside a Class at application/libraries/Common_helper.php. This allows you to access CI's superglobal objects like $this->load, $CI, or session data, which are unavailable when including a raw script via require.
// application/libraries/My_common_helper.php
class My_common_helper {
public static function my_function() {
// Access CI components here
return 'Function executed';
}
}
You can then call this from your view using echo \My_common_helper::my_function();. This approach keeps your code organized within the MVC structure and avoids the pitfalls of mixing PHP logic with raw file inclusion. If you strictly need to include a standalone script, always verify the path resolution before execution to prevent fatal errors in production environments.