get around allow_url_include=0 error

后端 未结 2 737
挽巷
挽巷 2021-02-10 08:58

I am trying to include a file from a url, however I get the following error thrown up.

Warning: include(): http:// wrapper is disabled in the server configuratio         


        
相关标签:
2条回答
  • 2021-02-10 09:42

    You're using a full URL as you include path, which tells PHP to attempt to do an HTTP request to fetch that file. This is NOT how you do this. Unless that ...100/index.php outputs PHP code, you are going to get some HTML or whatever as the include result, NOT the php code in the file. Remember - you're fetching via URL, which means it's an HTTP request, which means the webserver will EXECUTE that script and deliver its output, not simply serve up its source code.

    There's no way for the webserver to tell that the HTTP request for that script is an include call from another PHP script on the same server. It could just as easily be a request for that script from some hacker hiding in Russia wanting to steal your source code. Do you want your source code visible to the world like this?

    For local files, you should never use a full-blown url. it's hideously inefficient, and will no do what you want. why not simply have

    include('/path/to/templates/100/index.php');
    

    instead, which will be a local file-only request, with no HTTP stage included?

    0 讨论(0)
  • 2021-02-10 10:02

    I have had a similar issue.

    Considering 'Marc B's' post, it is clear that using absolute URLs is not a great idea, even if it is possible by editing the php.ini as 'ficuscr' states. I'm not sure this workaround will work for your specific situation as it still requires adjustments to each page depending on where it is in your folder structure, but it does makes things simpler if, like me, you have a lot of includes in your website.

    <?php $root="../";?>
    <?php include($root . 'folder-A/file_to_be_included_1.php');?>
    <?php include($root . 'folder-A/file_to_be_included_2.php');?>
    <?php include($root . 'folder-B/file_to_be_included_3.php');?>
    <?php include($root . 'folder-B/file_to_be_included_4.php');?>
    <?php include($root . 'folder-B/sub-folder/file_to_be_included_5.php');?>
    

    For me, this means if I move a page to another location in the folder structure, for example in a sub-folder of its current location, all I need to do is amend <?php $root="../";?> to become <?php $root="../../";?> for example

    0 讨论(0)
提交回复
热议问题