Search code examples
phpincludeini-set

how to to include a file from main domain to subdomain


want to include a file from main domain to subdomain

index.php - on subdomain.example.com

ini_set("allow_url_fopen", 1);
ini_set("allow_url_include", 1);    
$host = $_SERVER['HTTP_HOST'];
if($host == 'localhost'){include("../navt.php");}
else{include("https://example.com/navt.php");}

on localhost - there is no problem (win 10, xampp, chrome)
on remote server - I'm still getting this:
include(): https:// wrapper is disabled in the server configuration by allow_url_include=0


Solution

  • The documentation for the allow_url_include setting includes two important details:

    • It is changeable at "PHP_INI_SYSTEM" level only, which is explained elsewhere in the manual. It cannot be set at run-time, nor by individual users on a shared server.
    • It is deprecated since PHP 7.4.

    Both of these are for the same reason: this setting is extremely dangerous. It's highly advisable to come up with an alternative solution to your problem, by thinking about how you deploy your code. For instance:

    • Put the files that need to be shared between the different sub-domains somewhere on disk, and reference those files with a normal (non-URL) include.
    • Create a shared library that you can deploy to all the sub-domains at once.
    • Use the same application for all the sub-domains, with code to detect which one is being displayed.