64

I am trying to include a php file in a page via

  require_once(http://localhost/web/a.php)

I am getting an error

 Warning: require_once(): http:// wrapper is disabled in the server configuration by   allow_url_include=0

I changed allow_url_include=1 in the php.ini and that worked but I don't think that everybody will let me change their php.ini file.

So, is there any way to accomplish this?

Burgi
  • 421
  • 8
  • 24
Gags
  • 3,759
  • 8
  • 49
  • 96

11 Answers11

88

The warning is generated because you are using a full URL for the file that you are including. This is NOT the right way because this way you are going to get some HTML from the webserver. Use:

require_once('../web/a.php');

so that webserver could EXECUTE the script and deliver its output, instead of just serving up the source code (your current case which leads to the warning).

Nadeem Khan
  • 3,408
  • 1
  • 30
  • 41
26

I had this same error while trying to include a PHP file in my Wordpress theme. I was able to get around it by referencing the file name using dirname(__FILE__). I couldn't use relative paths since my file was going to be included in different places throughout the theme, so something like require_once '../path-to/my-file' wouldn't work.

Replacing require_once get_template_directory_uri() . '/path-to/my-file' with require_once dirname( __FILE__ ) . '/path-to/my-file' did the trick.

keepyourreceipt
  • 321
  • 3
  • 7
  • This may work in cases where the require_once can be evaluated later, as it seems to do so not in the pre-compile phase, but later in the function evaluation phase. In cases where you need to require the file prior to another file being required with out the use of the dirname() call (in a parent theme for example), it prevents any opportunity for your file to be evaluated first... At least this appears to be the case for WordPress servers. – Damian Green Nov 17 '15 at 18:37
  • 2
    If You are using WordPress, just use get_template_directory() instead of get_template_directory_uri() it`s working – Botond Vajna Jul 01 '20 at 16:31
20

try to use

<?php require_once($_SERVER['DOCUMENT_ROOT'].'/web/a.php'); ?>
7

You have to put the path to the file. For example:

require_once('../web/a.php');

You cannot get the file to require it from internet (with http protocol) it's restricted. The files must be on the same server. With Possibility to see each others (rights)

Dir-1 -
         > Folder-1 -> a.php
Dir-2 -
         > Folder-2 -> b.php

To include a.php inside b.php => require_once('../../Dir-1/Folder-1/a.php');
To include b.php inside a.php => require_once('../../Dir-2/Folder-2/b.php');
SoftGuide
  • 326
  • 1
  • 8
  • i know that.. but what if i want to include same file in two directories.. like file `a.php` is placed in DIR 1 and now i want to include it in DIR 3 file.. and one file having `a.php` from dir 3 is included in one of dir 1 files... so it gives errors `a.php` does not exist – Gags Apr 25 '14 at 06:26
  • Please show me the directory structure. Without it it is hard for me to help. – SoftGuide Apr 25 '14 at 06:27
  • it is Directory 1 --> Folder 1 --> a.php and Directory 2 --> Folder 2 --> b.php [it has require_once('../../a.php')] .. now when i include b.php in one of the files in Directory 1 then it throws me an error that a.php does not exist which is true as per directory structure – Gags Apr 25 '14 at 06:33
  • I have edit my previous answer, because here there is no post structure for a better "view". – SoftGuide Apr 25 '14 at 06:36
5

WORDPRESS is having this error mostly:
SOLUTION:
Locate your PHP installed directory on Remote live hosting SERVER or "Local Server"
In case of Windows os
for example if you using xampp or wamp webserver. it will be in xammp directory 'c:\xammp\php'
Note: For Unix/Linux OS, locate your PHP directory in Webserver

Find & Edit PHP.INI file
Find 'allow_url_include'
replace it with value 'on'
allow_url_include=On
Save you php.ini & RESTART you web-server.

ziapcland
  • 51
  • 1
  • 2
4
require_once('../web/a.php');

If this is not working for anyone, following is the good Idea to include file anywhere in the project.

require_once dirname(__FILE__)."/../../includes/enter.php";

This code will get the file from 2 directory outside of the current directory.

Anand agrawal
  • 492
  • 6
  • 25
1

include and require functions require file path, but you are giving file URI instead. The parameter should not be the one that includes http/https.

Your code should be something like:

include ("folder/file.php");
Usman Ahmed
  • 2,392
  • 1
  • 20
  • 17
1

For me the resolution with sending data on a PHP request:

$ch = curl_init('https://localhost/request.php');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_REQUEST));

echo curl_exec($ch);

curl_close($ch);
Syscall
  • 19,327
  • 10
  • 37
  • 52
1

If you're using WordPress and have trouble with this error on require or include, use get_template_directory() instead of get_theme_file_uri(). The included HTTP protocol is the problem in this case.

Example:

require_once( get_template_directory() . '\inc\file.php');

Results in (on localhost):

C:\XAMPP\htdocs\your-db\wp-content\themes\your-theme\inc\file.php
elvee
  • 36
  • 3
0
require_once(APPPATH.'web/a.php');

worked for me in codeigniter

check reference

0

echo file_get_contents('http://localhost/web/a.php'); //Best Example