include_once in header breaks when an absolute path is used

I have my website on my local server (localhost - XAMPP). My website is broken up into three parts: 1)Header 2)Body and 3)Footer/Jscripts. The header.php calls other basic files for the header section of the webpage. It works fine when I used relative paths, for example: include_once('./_css/main.css'); to call my CSS files or include_once('./_inc/metadata.php); to call my metadata for my pages.

The problem arises when I use an absolute path: include_once('http://localhost/mywebsite.com/_css/main.css'); the same for the other files.

Why is that? This all started because I want to create subfolders because I had a general page, but now that I'm breaking it down to subpages I need a folder to contain the subpages

I was trying to include the css files and all the other resource files to the subpages, but they break using the relative paths method (./_css/main.css) being pulled from the header file located in the root directory. On the subpages, I'm using include_once('../_inc/header.php') to include the initial header.php file.

I want to avoid having to duplicate all the resource files from the root directory and adding them in the subfolder with their respective relative paths. That's why I was trying to use the absolute path method.

Any insight or clue would be appreciate as to:

  1. Fix the 'Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0' error when using the absolute path.

2)Sharing css files in subfolders.

2 answers

  • answered 2021-11-29 06:47 Aaron Junker

    You have to edit your php.ini file, search for allow_url_include=0 and then edit it to allow_url_include=1

    This option prevents that you can include giles with an absolute path.

  • answered 2021-11-29 08:19 Ammar Aslam

    Using an absolute path may be a bad idea, what if you move from localhost to a real server? You'd need to make massive changes. Changing the project structure so that you could avoid all of this might be better, or if you really want to keep things as they are, use a variable.

    <?php
        $include_path = "whatever/path"
        include('../header.php');
    ?>
    

    and in header.php

    <?php
        include($include_path.'/my_css.css');
    ?>
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum