3

So, I am trying to insert a file with a php include, but for some reason it doesn't work with the full URL.

This works:

<?php include 'menu.html' ?>

But this does not:

<?php include 'http://domainname.com/menu.html' ?>

Any ideas?

1
  • 1
    Is the server running on Windows? If so, note the warning "Windows versions of PHP prior to PHP 4.3.0 do not support access of remote files via this function, even if allow_url_fopen is enabled." Commented Nov 13, 2012 at 22:02

6 Answers 6

9

include is meant to include and evaluate a specified PHP file. If you fetch it locally, it can be processed like PHP - if you fetch it through a full URL, you will get the resulting HTML (in theory ... you may get only an error).

Supporting Docs

include 'menu.php' //Internally process a PHP file to generate HTML

or

file_get_contents('http://domainname.com/menu.php'); //Fetch resutling HTML
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhhh, gotcha. Ok. Yeah,I wasn't sure if it was that or something else
7

Does your php.ini allow you to use remote files with include?

If it's on your own domain then it makes no sense to require the absolute URL, as this would cause the server to open a connection to itself to retrieve the file via HTTP when a direct file operation would be so much simpler.

1 Comment

Well, that's the thing. The dude who desinged the site before me thought it would be better to have the blog and the site (both wordpress) on two different installs and URL's. Then he made the entire menu static without using the wordpress menu functions. Blows my mind. Anyways, just looking for a simple solution to update the navigation in both places from one file.
7

check php.ini -

allow_url_include

but I have to say, if you don't have really really good reason, please don't use it. It's one of the worst php weaknesses, serious security threat.

2 Comments

Why is this a security threat?
Because potentially anything from who knows where could be executed
2

Don't use absolute urls. Instead, try linking from the root directory. Like this:

$path = $_SERVER['DOCUMENT_ROOT']."/layer1/layer2/layer3/";
include($path."file.php");

Comments

1

An include tag is meant to be used to reference other PHP script files. If it is not a script that you are going to be processing, you might want to look into using:

$FileContents = file_get_contents("http://www.domainname.com/menu.html");

Comments

0

Sounds like allow_url_fopen or allow_url_include or both have been set to 0 (disabled) in php.ini.

HTML should't be included in a PHP script, though. Other answers seem to address that issue as well.

1 Comment

I'm using it with and .htaccess file since the rest of the pages are .html files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.