Hi All,

I have the following code:

<?php

ini_set('allow_url_include', 'On');
ini_set('allow_url_fopen', 'On');

require("http://www.example.co.uk/functions/database.php");
$q = "SELECT * FROM orders";
$result = queryDatabase($q);

?>

I'm trying to use the function "queryDatabase", but when I run this script I get the following error message:

Warning: require_once() [function.require-once]: URL file-access is disabled in the server configuration in C:\xampp\htdocs\test.php on line 6

Warning: require_once(http://www.example.co.uk/functions/database.php) [function.require-once]: failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\test.php on line 6

Fatal error: require_once() [function.require]: Failed opening required 'http://www.example.co.uk/functions/database.php' (include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\test.php on line 6

I really want to avoid having to duplicate this function! Could anyone help me out with this please?!

Thanks,

dai.hop

    Why dont you include it from the same server?

    If theres lots of sites which you want to access the same functions, copy the functions to some location where they can be included. Then just add the directory to include_path in your php.ini(for global) or vhost.:

    require_once('database.php');
    

    For example most hosts have added PEAR to include_path so their customers dont have to install it to their own webspace.

      Including files across an HTTP connection is quite different than including them via the local filesystem.

      If you use a URL in an include/require() path, the server makes an HTTP request to the address given to retrieve the contents of the file - just as your web browser does. This is obviously a problem, because PHP code is never transmitted across an HTTP connection to the end user - it's all executed on the server and only the output of that PHP script is transmitted.

      Thus, you need to use paths pointing to the local file system (e.g. "database.php" if it's in the same directory, "../database.php" if it's one directory up, etc.), assuming that this file is on the same server.

        Thanks for your replies.

        Its a shame this can't be done, now if I change the name of my database Ill have to update multiple copies of the function that queries it!

          No one said it can't be done... you just can't use a URL to include the file.

            Ok, is there any other way to access the function in my remote file on another domain then?

              Ok, is there any other way to access the function in my remote file on another domain then?

              No, unless your remote file is not parsed as PHP. However, note that unless you have control of the remote server, evaluating external code as PHP is dangerous.

                And also note that having code such as database connection details in a text-only format available to the web is dangerous and begging for trouble.

                Is this file truly on a separate physical server that is not within the same LAN? If so, then you may indeed have trouble accessing that PHP script... the simplest solution would probably be to copy the PHP file to all domains that you use it on... meaning yes, you will have to update them individually.

                If you wanted to have the code outputted as plain text (and thus could be [man]eval/man'ed), you would probably want to require a secret password to be passed before you output the PHP code, so you could instead require() http://otherdomain.com/database.php?pass=some_secret_password_here .

                  Write a Reply...