Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with require() when using a fully qualified URL

Tags:

php

So, my code was using relative paths, but running into some problems with common files which could be include/required from different directory levels.

Absolute paths are more efficient anyway, right? So, I changed all include/require to absolute paths, using require_once('http://' . $_SERVER['HTTP_HOST'] . 'file_name.php');

$_SERVER['HTTP_HOST'] is correct, isn't it? It seemed so when I googled.

That required me to set 'allow_url_include=on` in php.ini and restart Apache.

So, now I have a situation that looks something like this (simplified example):

File2.php contains

<?php
   function hello()
   {
     echo 'hello<br>';
   }
?>

and if file1.php contains

<?php
   require_once('file2.php');    
   hello();
?>

then I see the expected output "hello", but if I change that line to

   require_once('http://' . $_SERVER['HTTP_HOST'] . '/file2.php');

Then I get "Fatal error: Call to undefined function hello() in C:\xampp\htdocs\file1.php"

(I guess that the reference to c:\xammp\httdos came from Xdebug, because PhpInfo shows HTTP_HOST localhost)

Anyway, that's a long post to say that I am missing some simple point and to ask what it is.

like image 434
Mawg says reinstate Monica Avatar asked Dec 10 '25 22:12

Mawg says reinstate Monica


1 Answers

When you require a full URL, PHP makes a request to the server and gets back the output of the PHP script - this will not contain any actual PHP code (unless the script itself outputs PHP code).

Also, you aren't going to see any noticeable difference in performance between using relative and absolute paths, so don't worry too much about it. In fact, your path is not an absolute path but an absolute URI, and fetching a URI is actually going to be way slower than using local paths.

like image 110
casablanca Avatar answered Dec 12 '25 13:12

casablanca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!