<?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>worth a read &#187; PHP</title>
	<atom:link href="http://www.wortharead.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wortharead.com</link>
	<description>thoughts on PHP, Linux and other boring things</description>
	<lastBuildDate>Mon, 30 Mar 2009 09:45:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Relative links in PHP</title>
		<link>http://www.wortharead.com/2009/01/07/relative-links-in-php/</link>
		<comments>http://www.wortharead.com/2009/01/07/relative-links-in-php/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 15:39:07 +0000</pubDate>
		<dc:creator>Duncan</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.wortharead.com/?p=11</guid>
		<description><![CDATA[A neat trick to solve problems with relative links in PHP.]]></description>
			<content:encoded><![CDATA[<p>Whenever I create a web site, I use functions to output the header and footer of each page. This technique allows easy editing of page layouts as most of the HTML resides in these two functions.  Every page on the site will call the same two functions, resulting in a consistent look and feel across the whole web site.</p>
<p>The header function outputs the <code>&lt;head&gt;</code> section of the HTML, which will normally contain links to style sheets.  The question is: should these links be relative or absolute?</p>
<h3>Relative links</h3>
<p>The problem with relative links rears its ugly head when the header function is called from different directory depths.  Take, for example, the following directory structure:</p>
<blockquote><p><code>/css/main.css<br />
/members/admin/index.php [1]<br />
/members/index.php [2]</code></p></blockquote>
<p>It&#8217;s clear to see that file [1] requires a different relative link to access the CSS file compared with file [2] (i.e. <code>../../css/main.css</code> versus <code>../css/main.css</code>).  A static relative link won&#8217;t work for every file in a web site.</p>
<h3>Absolute links</h3>
<p>What about an absolute link?  This would certainly solve the above problem, however it presents its own irritating issue: what to do if the folder structure changes?</p>
<p>To give an example, my local web server is configured to serve pages from <code>/var/www</code> and I create a subdirectory for each site I&#8217;m constructing.  So my work on <code>http://www.foo.com</code> will probably be stored in <code>/var/www/foo</code> and accessed as <code>http://localhost/foo</code> on my test server.  Unfortunately, this means that absolute links on my test server (e.g. <code>/foo/css/main.css</code>) will break on my production server (which would require <code>/css/main.css</code>).</p>
<h3>Solution</h3>
<p>One solution is to define a base bath, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BASE_PATH&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span>1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s break the definition into pieces to see what this code is doing:</p>
<ul>
<li>The <code>__FILE__</code> constant will output the full path of the current file from the web server&#8217;s point of view, e.g. <code>/var/www/mysite/foo/bar.php</code>.</li>
<li>The <code>$_SERVER['DOCUMENT_ROOT']</code> variable will output the directory from which the web server is serving files, e.g. <code>/var/www</code>.</li>
<li>The <code>substr</code> command effectively subtracts one from the other, leaving <code>/mysite/foo/bar.php</code>.</li>
<li>The <code>dirname</code> command strips off the file name, leaving <code>/mysite/foo</code>.</li>
</ul>
<p>Now, suppose my <code>bar.php</code> file contains the header function.  It&#8217;s now able to construct an absolute link to anywhere in the web site, by prefixing the <code>BASE_PATH</code> constant to a relative link, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> BASE_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../css/main.css'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This forms an absolute link that is resilient to the folder structure changes discussed above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wortharead.com/2009/01/07/relative-links-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
