views:

201

answers:

4

While cleaning up some old php scripts I've noticed some weird behavior with require/include statements when I try to use variables.

On the live server, the following code works fine..

<?php
$test = "http://localhost/elearning/trunk/mypage.php";
require "$test";
?>

..but on my XAMPP installation ((basic package) version 1.6.7) I receive the following error:

Warning: require() [function.require]: URL file-access is disabled in the server configuration in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22

Warning: require(http://localhost/elearning/trunk/mypage.php) [function.require]: failed to open stream: no suitable wrapper could be found in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22

Fatal error: require() [function.require]: Failed opening required 'http://localhost/elearning/trunk/mypage.php' (include_path='.;C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\php\pear\') in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22

If I copy+paste http://localhost/elearning/trunk/mypage.php (directly from the error) into my browser, mypage.php loads. Is this an error in my configuration, or my approach?

+1  A: 

try require "./mypage.php" not the whole exact directory

lock
A: 

Don't use a path like that ... use a relative path or absolute path.

Example

require 'mypage.php'
alex
+3  A: 

You can't use paths that start with http:// on some servers because of security. What you want to use instead is a directory path.

You can get your current directory path by doing something like
echo $_SERVER['DOCUMENT_ROOT'];

that will give you your directory path from the root folder of the server. That is the path you want to use to include/require stuff.


If you didn't quite understand that, try this.

There are two types of paths:

World Wide Web Paths:
http://example.com/directory/file.php

Server Directory Paths:
/home/usr/www/site/html/

The Server directory path is where your files are located on the server's hard drive. Much like your computer's hard drive, it is never changes unless you move the files. On the other hand, The World Wide Web Path (the one everyone uses to access your website) can change based on what domain you are using, where your Document Root is pointing, Mod Rewrites, and more.

Note: The Document Root is the top most directory that your server serves files from. So, if you had index.php in your document root, it would show up like this on the web: http://example.com/index.php.

When PHP looks for a file, it uses the Server Directory Path, because it is running on the server. When Javascript, which runs from the user computer, wants to look for a file, it uses the World Wide Web path to access it, because it isn't accessing it from the server.

I really hope that somewhere in there, something made sense.

Chacha102
Ah, perfect sense (and answers what would have been my next question)! I've never got my head around the differences between www and directory paths, probably because this hokey server appears to treat them the same. Thanks!
Zoe
Some servers only allow you to put files after the DOCUMENT_ROOT directory, which means that the highest folder you can go to is also the same as example.com/
Chacha102
+1  A: 

PHP isn't smart enough to know that you're including from the same server when you use URL notation like that, so it thinks you're grabbing files from a seperate server (which can be dangerous).

Your server configuration is set to disallow loading these external files (evident by the error message "URL file-access is disabled in the server configuration"), so it's failing.

Since you're on the same server, you can simply use relative paths, like:

require_once 'mypage.php'
Mike Trpcic