<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Learn PHP Online &#187; PHP Errors</title>
	<atom:link href="http://www.learnphponline.com/category/errors/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learnphponline.com</link>
	<description>Learn PHP Online</description>
	<lastBuildDate>Wed, 06 May 2009 01:02:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>URL File-Access is Disabled in the Server Configuration</title>
		<link>http://www.learnphponline.com/errors/url-file-access-is-disabled-in-the-server-configuration</link>
		<comments>http://www.learnphponline.com/errors/url-file-access-is-disabled-in-the-server-configuration#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:59:46 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Errors]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php debug]]></category>
		<category><![CDATA[php errors]]></category>
		<category><![CDATA[php help]]></category>
		<category><![CDATA[url file access]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=86</guid>
		<description><![CDATA[How to fix the PHP error: URL File-Access is Disabled in the Server Configuration]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Warning: include()  [function.include]: URL file-access is disabled in the server configuration</strong> is an error obtained by using the include command. Lucky for webmasters, this error is easily fixed via several different methods.</p>
<h3>Why This Error Occurs</h3>
<p>If you&#8217;re seeing this error, we are willing to bet that you are using the include statement as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Common Include File Usage</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">include (</span><span style="color: #dd0000;">"http://www.YourDomain.com/includes/header.php"</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">?&gt;</span> </span></pre>
<p>And you are more than likely getting an error similar to the following:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Include File Error Message</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">/*Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/YourUsername/public_html/index.php on line xx */

/*Warning: include(http://www.YourDomain.com/index.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/YourUsername/public_html/index.php on line xx*/

/*Warning: include() [function.include]: Failed opening 'http://www.YourDomain.com/index.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/YourUsername/public_html/index.php on line xx*/
</span><span style="color: #0000bb;">?&gt;</span> </span></pre>
<p>What specifically causes this error is the fact that the server has upgraded from PHP 4 to a newer version. In the upgrade, the <strong>allow_url_fopen</strong> is set to <strong>OFF</strong>, which is responsible for disallowing include files to use absolute file paths.</p>
<p>Don&#8217;t be in a rush to turn this off in your system configuration just yet! Any upgrades past PHP 4 will turn <strong>allow_url_fopen</strong> to <strong>OFF</strong> as default due to security concerns. This is most prevalent in cross-site scripting attacks, or XSS attacks. In some cases, malicious users have even enslaved a server to become a spam-email-sending nightmare: all without the administrator noticing!</p>
<p>If there is any part of a website that allows a user to upload data of some sort, there are vulnerabilities that are present with poor coding that may allow malicious users to inject an include statement. By allowing <strong>allow_url_fopen</strong> to be set to <strong>ON</strong>, it allows them to include any file they wish from any website on the Internet. With it set to <strong>OFF</strong>, only documents on the server may be included. This is much safer considering you probably don&#8217;t have malicious code stored on the server. (And theoretically, if you did, hackers probably wouldn&#8217;t be able to find it)</p>
<h3>The First Solution: Use Relative File Paths</h3>
<p>A web server will automatically assume that the code below belongs on the server, and thus, is not a remote file:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Include File With Relative Paths</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">include (</span><span style="color: #0000bb;">header</span><span style="color: #007700;">.</span><span style="color: #0000bb;">php</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//This file is in the same directory as the PHP file

</span><span style="color: #007700;">include (</span><span style="color: #0000bb;">includes</span><span style="color: #007700;">/</span><span style="color: #0000bb;">header</span><span style="color: #007700;">.</span><span style="color: #0000bb;">php</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//This file is in a directory under the PHP file

</span><span style="color: #007700;">include (../</span><span style="color: #0000bb;">header</span><span style="color: #007700;">.</span><span style="color: #0000bb;">php</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//This file is in the directory above the current PHP file

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p>Relative file paths can be used in every legitimate situation an absolute path would be used, although it may take a little more work. As in the example above, you may have to work at determining where the file you wish to include exists in relation to the PHP file being run.</p>
<p>Not your idea of fun? We aren&#8217;t fond of it either, so on to the next solution!</p>
<h3>The Second Solution: Use Another PHP Function</h3>
<p>We may substitute the include statement with <strong>file_get_contents</strong>, which reads an entire file into a string.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Include With File_Get_Contents</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

$includeFile </span><span style="color: #007700;">= </span><span style="color: #0000bb;">file_get_contents</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"http://www.YourDomain.com/includes/header.php"</span><span style="color: #007700;">);

echo </span><span style="color: #0000bb;">$includeFile</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">?&gt;

</span>
</span></pre>
<p>This is a good alternative to keep the absolute path an option in including a certain file. There are some instances where the above code wouldn&#8217;t come out as planned, depending on the situation. In addition, it adds another line of code that we can relinquish with the best solution: using a server variable.</p>
<h3>The Best Solution: Using Server Variables</h3>
<p>If you don&#8217;t want to spend hours rearranging code, you can do it the easy way with $_SERVER['DOCUMENT_ROOT'].</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Include Server Variables</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php 

</span><span style="color: #007700;">include </span><span style="color: #0000bb;">$_SERVER</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'DOCUMENT_ROOT'</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">'/includes/header.php'</span><span style="color: #007700;">; 

</span><span style="color: #0000bb;">?&gt;

</span>
</span></pre>
<p>This allows you to keep the absolute path that you&#8217;ve come to be familiar with in using the include statement. Technically, the $_SERVER['DOCUMENT_ROOT'] command gives your path to the public_html directory, as seen below:</p>
<li style="margin-left: 30px;">/home/Your_Username/public_html</li>
<p>Essentially this is the root of your website, www.YourDomain.com, and therefore, you can use it just as you would with any other include statement. Just replace www.YourDomain.com with the server variable and you&#8217;re set!</p>
<h3>Should I Turn On allow_url_fopen?</h3>
<p>The short answer: <strong>no</strong>; allow_url_fopen was turned off for a reason. If you don&#8217;t own your own server, odds are your host won&#8217;t even allow the change in the first place. If you do own your own server, realize that the third solution presented in this tech tip takes almost no time to implement, and only requires that the base URL be replaced with a server variable.</p>
<h4>Closing Comments</h4>
<p>The bad news is that you&#8217;ll probably have to switch many pages in your website to conform to the new standard. The good news is that once that&#8217;s done you&#8217;ll have a more secure server, and by following the new syntax, you&#8217;ll never have to change the code again.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/errors/url-file-access-is-disabled-in-the-server-configuration/feed</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>
