<?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>logon2 Blog &#187; PHP</title>
	<atom:link href="http://www.logon2.com.au/blog/archive/category/coding/coding-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.logon2.com.au/blog</link>
	<description>Better use of the web for everybody</description>
	<lastBuildDate>Tue, 13 Jul 2010 12:44:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)</title>
		<link>http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/</link>
		<comments>http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 08:49:59 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[colour]]></category>
		<category><![CDATA[compatibility]]></category>
		<category><![CDATA[cross-browser]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[execute]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://www.logon2.com.au/blog/?p=167</guid>
		<description><![CDATA[It&#8217;s possible that you&#8217;re thinking, &#8220;Processing PHP in CSS? That&#8217;s plain stinks.,&#8221; just from reading the heading. It does stink, but so does maintaining two CSS files, updating your colour scheme manually (or even by sed), changing common dimensions, and all the other crap that goes with static files. Think about it this way &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s possible that you&#8217;re thinking, &#8220;Processing PHP in CSS? That&#8217;s plain stinks.,&#8221; just from reading the heading. It does stink, but so does maintaining two CSS files, updating your colour scheme manually (or even by sed), changing common dimensions, and all the other crap that goes with static files. Think about it this way &#8211; would you build a site out of many HTML files or would you use some kind of template system (like <a href="http://www.logon2.com.au/blog/coding/pretty-smarties/">Pretty Smarties</a>)? By now I hope you&#8217;re less sceptical. If you&#8217;re interested, please read on.</p>
<h3>Executing PHP in CSS Files</h3>
<p>In order to parse and execute PHP in a CSS file, all you have to do is tell Apache to treat CSS files as PHP, and just make sure you tell the browser that it&#8217;s NOT text/html (default mime type) but CSS.Very easy.</p>
<h4>.htaccess File</h4>
<p>Place a .htaccess file in the directory of the CSS file (if there isn&#8217;t one already). Simply add a directive to process the PHP in CSS files. Depending on your configuration, you might have to use a different directive. Here are the two most common ones I&#8217;ve been able to use:<br />
Mod_PHP (Apache PHP Module)</p>
<pre>
#MOD PHP
<FilesMatch "\.(css)$">
	SetHandler application/x-httpd-php
</FilesMatch>
</pre>
<p>Others:</p>
<pre>
#OTHERS
AddType application/x-httpd-php5 .css
</pre>
<p><em>(If you&#8217;re using php4, use x-httpd-php4)</em></p>
<h4>CSS (and PHP) File</h4>
<p>I recommend using this code just to test that your Apache directive is working. Hopefully this example shows the potential use for  (server-side) executable code in CSS files.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">&lt;?php
	//make sure the browser treats this as CSS
	header<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;Content-Type: text/css&quot;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	//browser detection
	if <span style="color: #00AA00;">&#40;</span>strpos<span style="color: #00AA00;">&#40;</span>$_SERVER<span style="color: #00AA00;">&#91;</span><span style="color: #ff0000;">'HTTP_USER_AGENT'</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">,</span><span style="color: #ff0000;">&quot;Internet Explorer&quot;</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#123;</span>
		$internet_explorer <span style="color: #00AA00;">=</span> true<span style="color: #00AA00;">;</span>
	<span style="color: #00AA00;">&#125;</span>
&nbsp;
	$baseColour <span style="color: #00AA00;">=</span> <span style="color: #ff0000;">&quot;#F3AA9C&quot;</span><span style="color: #00AA00;">;</span>
	$secondaryColour <span style="color: #00AA00;">=</span> <span style="color: #ff0000;">&quot;#CCAF1F&quot;</span><span style="color: #00AA00;">;</span>
	$foregroundColour <span style="color: #00AA00;">=</span> <span style="color: #ff0000;">&quot;#FFFFFF&quot;</span><span style="color: #00AA00;">;</span>
	$moduleWidth <span style="color: #00AA00;">=</span> <span style="color: #ff0000;">&quot;125px&quot;</span><span style="color: #00AA00;">;</span>
?<span style="color: #00AA00;">&gt;</span>
&nbsp;
<span style="color: #cc00cc;">#content</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span>&lt;?php echo $baseColour<span style="color: #00AA00;">;</span> ?<span style="color: #00AA00;">&gt;;</span>
	colour<span style="color: #00AA00;">:</span>&lt;?php echo $foregroundColour ?<span style="color: #00AA00;">&gt;;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#sideBar</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> &lt;?php echo $secondaryColour<span style="color: #00AA00;">;</span> ?<span style="color: #00AA00;">&gt;;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span>&lt;?php echo $baseColour<span style="color: #00AA00;">;</span> ?<span style="color: #00AA00;">&gt;;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span>&lt;?php echo $moduleWidth<span style="color: #00AA00;">;</span> ?<span style="color: #00AA00;">&gt;;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> &lt;?php echo $secondaryColour<span style="color: #00AA00;">;</span> ?<span style="color: #00AA00;">&gt;;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span>&lt;?php echo $foregroundColour<span style="color: #00AA00;">;</span> ?<span style="color: #00AA00;">&gt;;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><em>Obviously mixing PHP and CSS stuffs up syntax highlighting sorry.</em></p>
<h4>Caveat</h4>
<p>Most browsers will download the CSS file on every pageload &#8211; it&#8217;s not too difficult to implement cache-control, but I haven&#8217;t done it for this code.</p>
<hr />
Code hard or &#8230; er&#8230; code home.</p>
<h3>Related Posts:</h3><ul class="related_post"><li><a href="http://www.logon2.com.au/blog/archive/coding/pretty-smarties/pretty-smarties-05-pretty-urls-smarty-templates-and-faster-web-design/" title="Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design">Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design</a></li><li><a href="http://www.logon2.com.au/blog/archive/myspace/choose-myspace-comment-colors-fonts-and-sizes/" title="Choose MySpace Comment Colors, Fonts and Sizes">Choose MySpace Comment Colors, Fonts and Sizes</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/pngs-have-different-colours-in-different-browsers/" title="PNGs Have Different Colours in Different Browsers?">PNGs Have Different Colours in Different Browsers?</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/introducing-the-lovely-caption-maker/" title="Introducing the Lovely Caption Maker">Introducing the Lovely Caption Maker</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/accessible-valid-dropdown-menu-or-popup-menu-using-css-and-semantic-html/" title="Accessible, Valid Dropdown Menu or Popup Menu using CSS and Semantic HTML">Accessible, Valid Dropdown Menu or Popup Menu using CSS and Semantic HTML</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/" title="Extract GET Variables from URL String to Array &#8211; PHP Function">Extract GET Variables from URL String to Array &#8211; PHP Function</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; No such file or directory in Unknown on line 0 Error</title>
		<link>http://www.logon2.com.au/blog/archive/troubleshooting/php-no-such-file-or-directory-in-unknown-on-line-0-error/</link>
		<comments>http://www.logon2.com.au/blog/archive/troubleshooting/php-no-such-file-or-directory-in-unknown-on-line-0-error/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 02:31:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[fatal]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[unknown]]></category>
		<category><![CDATA[warning]]></category>

		<guid isPermaLink="false">http://www.logon2.com.au/blog/?p=138</guid>
		<description><![CDATA[Ran XAMPP, tried to access the development virtual host for Kids On The Rise ( http://localhost:99 ) and got this: Warning: Unknown([PATH HERE]): failed to open stream: No such file or directory in Unknown on line 0 Some other Fatal error came up but I forgot to copy it, sorry. The error message is unhelpful [...]]]></description>
			<content:encoded><![CDATA[<p>Ran XAMPP, tried to access the development virtual host for Kids On The Rise ( http://localhost:99 ) and got this:</p>
<pre>Warning: Unknown([PATH HERE]): failed to open stream: No such file or directory in Unknown on line 0</pre>
<p>Some other <b>Fatal</b> error came up but I forgot to copy it, sorry.</p>
<p>The error message is unhelpful but it was pretty easy to fix. I just deleted the .htaccess file from the root and tested. It worked! After restoring a backup of the .htaccess file everything worked. Hooray!</p>
<h3>Related Posts:</h3><ul class="related_post"><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/" title="Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)">Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/check-if-headers-have-already-been-sent-in-php/" title="Check if Headers Have Already Been Sent in PHP">Check if Headers Have Already Been Sent in PHP</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/pretty-smarties/pretty-smarties-05-pretty-urls-smarty-templates-and-faster-web-design/" title="Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design">Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/" title="Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial">Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/" title="Extract GET Variables from URL String to Array &#8211; PHP Function">Extract GET Variables from URL String to Array &#8211; PHP Function</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.logon2.com.au/blog/archive/troubleshooting/php-no-such-file-or-directory-in-unknown-on-line-0-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check if Headers Have Already Been Sent in PHP</title>
		<link>http://www.logon2.com.au/blog/archive/troubleshooting/check-if-headers-have-already-been-sent-in-php/</link>
		<comments>http://www.logon2.com.au/blog/archive/troubleshooting/check-if-headers-have-already-been-sent-in-php/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 05:39:51 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[headers]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.logon2.com.au/blog/?p=119</guid>
		<description><![CDATA[Most PHP programmers would have come across this old chestnut before: Cannot modify header information – headers already sent by page.php line 42 This can happen when you try doing a HTTP redirect or send HTTP 404, as it can happen whenever you try to send headers once they&#8217;ve already been sent. When a user [...]]]></description>
			<content:encoded><![CDATA[<p>Most PHP programmers would have come across this old chestnut before:<br />
<code>Cannot modify header information – headers already sent by page.php line 42</code></p>
<p>This can happen when you try doing a HTTP redirect or send HTTP 404, as it can happen whenever you try to send headers once they&#8217;ve already been sent. When a user sees this, they are basically left high and dry, often on a blank page. The solution is simple:</p>
<h3>Detect if Headers Have Already Been Sent</h3>
<p>Whenever you call <strong>header();</strong> , call <strong>headers_sent();</strong> first!</p>
<p>Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">headers_sent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$absolute</span> ? HOME_URL <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&lt;p&gt;Couldn't redirect to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$absolute</span> ? HOME_URL <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, headers already sent.&lt;/p&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//do something here...</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Of course you should probably do thing useful </p>
<h3>Related Posts:</h3><ul class="related_post"><li><a href="http://www.logon2.com.au/blog/archive/coding/mysql-coding/groupwise-maximum-mysql/" title="Returning the entire row of maximum value for each group (Group-wise Maximum in SQL / MySQL )">Returning the entire row of maximum value for each group (Group-wise Maximum in SQL / MySQL )</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/" title="Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)">Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/php-no-such-file-or-directory-in-unknown-on-line-0-error/" title="PHP &#8211; No such file or directory in Unknown on line 0 Error">PHP &#8211; No such file or directory in Unknown on line 0 Error</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/pretty-smarties/pretty-smarties-05-pretty-urls-smarty-templates-and-faster-web-design/" title="Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design">Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/" title="Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial">Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/" title="Extract GET Variables from URL String to Array &#8211; PHP Function">Extract GET Variables from URL String to Array &#8211; PHP Function</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.logon2.com.au/blog/archive/troubleshooting/check-if-headers-have-already-been-sent-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial</title>
		<link>http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/</link>
		<comments>http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 05:39:20 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.logon2.com.au/blog/archive/tips/91/</guid>
		<description><![CDATA[In technology, there&#8217;s always the goal of making complex and difficult things more simple &#8211; this goal has brought about &#8216;clean URLs&#8217;. Clean URLs are website addresses that are easy for people (and also search engines) to read and understand. For example: An interesting article about the Bermuda Triangle might be at www.articles.com/article.php?id=23452 &#8211; That [...]]]></description>
			<content:encoded><![CDATA[<p>In technology, there&#8217;s always the goal of making complex and difficult things more simple &#8211; this goal has brought about &#8216;clean URLs&#8217;. Clean URLs are website addresses that are easy for people (and also search engines) to read and understand. For example: An interesting article about the Bermuda Triangle might be at <strong>www.articles.com/article.php?id=23452</strong> &#8211; That address tells us (and search engines) nothing about the article. Using a clean URL, the same article might be at <strong>www.articles.com/articles/bermuda-triangle </strong>- much easier,  right? Well, here is how you can implement this on <em>your </em>website&#8230;</p>
<h3>Setting Up Apache&#8217;s &#8220;mod_rewrite&#8221; Modul</h3>
<p>To implement these URLs with Apache, you will need to have the mod_rewrite module enabled. Here&#8217;s how to enable mod_rewrite:</p>
<ol>
<li>Open the httpd.conf file from the &#8230;apache/conf/ directory</li>
<li>Uncomment the line &#8220;LoadModule rewrite_module modules/mod_rewrite.so&#8221; (by removing preceding #)</li>
</ol>
<p>If you have more than one virtual server or directory set up, you might have to add something like this as well:</p>
<pre><code></code>&lt;Directory "PATHOFDIRECTORY"&gt;
    AllowOverride all
    Order allow,deny
    Allow from all
&lt;/Directory&gt;</pre>
<p>Next, you need to create a file named &#8220;<em>.htaccess</em>&#8221; at the website&#8217;s root directory (if you haven&#8217;t got one already)</p>
<p>In your <em>.htaccess</em> file, add the following rules:</p>
<pre>&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
&lt;/IfModule&gt;</pre>
<p>This will redirect all requests that don&#8217;t point to an existing file or directory to &#8216;index.php&#8217;.</p>
<p>You might (or might not) be thinking &#8220;How does that help implement clean URLs?&#8221; The next part will answer that question, but before proceeding &#8211; test your new Apache config and  <em>.htaccess </em>file &#8211; restart Apache if necessary and try loading a directory that doesn&#8217;t exist (ie. http://yourwebsitehost/page/5/your-information/). If it loads your index.php page &#8211; we&#8217;re ready to continue.</p>
<h3>Getting PHP to Interpret The URLs</h3>
<p>Ok, the hard part is done. Now I&#8217;ll explain what we want when a user requests a page:</p>
<ol>
<li> User requests &#8220;/page/5/your-information/&#8221;</li>
<li>Apache redirects the request to &#8220;index.php&#8221; without changing the user&#8217;s URL</li>
<li>index.php breaks up the requet URI and loads page.php instructing it to load page &#8217;5&#8242;</li>
</ol>
<p>This might seem a bit difficult (or, if you&#8217;re experienced, very easy) &#8211; it honestly is rather easy. Once you understand how it works, you might decide to do it differently, but here is a <em>really simple </em>example of a website that contains information &#8216;pages&#8217; and information about the &#8216;authors&#8217; of those pages.</p>
<p>In our example, we have three php files:</p>
<ul>
<li>index.php</li>
<li>page.php</li>
<li>author.php</li>
</ul>
<h4>&#8220;index.php&#8221;</h4>
<pre><code>&lt;?php
//$urlVariables is an array that contains the different bits of information in the request.
$urlVariables = explode("/",$_SERVER['REQUEST_URI']);
//If the user requested '/page/6/about-elephans/' then $urlVariables[1] would be 'page', $urlVariables[2] would be '6' and so on.</code>

if ($urlVariables[1] == "page") {
//the user is requesting an information page
    include "page.php";
} elseif $urlVariables[2] == "author" {
    //the user is requesting author information
    include "author.php";
} else {
    //load the home page
    include "default.php";
}?&gt;</pre>
<h4>&#8220;page.php&#8221;</h4>
<pre><code>&lt;?php</code>//load an object that deals with pages (to keep the tutorial simple)
include "classes/page.php";
$page = new page;
<pre>//$urlVariables will contain the page id as the 2nd array element (ie. /page/pageid/page-name-here/)$page_id = $urlVariables[2];
//the page name part of the URL is ignored when loading because we only use ID to load pages - it's just good for search engines and people.

//get the page class to load the page
if ($page-&gt;load_page($page_id)) {?&gt;

&lt;h1&gt;&lt;?php echo $page-&gt;page_name(); ?&gt;&lt;/h1&gt;
&lt;p&gt;&lt;?php echo $page-&gt;page_content(); ?&gt;&lt;/p&gt;
&lt;p&gt;Written by &lt;a href="&lt;?php echo $page-&gt;author_url(); ?&gt;"&gt; &lt;?php echo $page-&gt;author_name(); ?&gt; &lt;/a&gt;&lt;/p&gt;&lt;?php

} else {

    //error!
    include "error.php";</pre>
<p>}&gt;</pre>
<h4>&#8220;about.php&#8221;</h4>
<pre><code>&lt;?php

</code>//load an object that deals with authors (again, to keep the tutorial simple)
include "classes/author.php";
$author = new author;
//$urlVariables will contain the page id as the 2nd array element (ie. /author/authorid/author-name-here/)
$author_id = $urlVariables[2];
//again, the author name part of the URL is ignored when loading because we also only use ID to load authors - it is just good for search engines and people.

//get the author class to load the page
if ($author-&gt;load_author($author_id)) {?&gt;
&lt;h1&gt;&lt;?php echo $author-&gt;author_name(); ?&gt;&lt;/h1&gt;
&lt;h2&gt;Bio&lt;/h2&gt;&lt;p&gt;&lt;?php echo $author-&gt;author_information(); ?&gt;&lt;/p&gt;
&lt;h2&gt;All pages by &lt;?php echo $author-&gt;author_name; ?&gt;&lt;/h2&gt;
&lt;$php echo $author-&gt;page_list; 

} else {
    //error!
    include "error.php";
}
?&gt;</pre>
<p>Now that example was probably a bit excessive. Here&#8217;s a basic overview of how it works:</p>
<ol>
<li>The <em>index.php </em>page is loaded, and &#8216;explodes&#8217; the request part of the URL into an array ($urlVariables in the example)</li>
<li>You then interpret the array elements to decide what to show to the user. ($urlVariables[1] determined whether to show a page or an author profile and $urlVariables[2] was an ID for either the page or the author in our example)</li>
</ol>
<p>It&#8217;s that simple. This method is SUPER flexible if you build a strong framework around it.</p>
<p>Good luck!</p>
<h3>Related Posts:</h3><ul class="related_post"><li><a href="http://www.logon2.com.au/blog/archive/coding/pretty-smarties/pretty-smarties-05-pretty-urls-smarty-templates-and-faster-web-design/" title="Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design">Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/" title="Extract GET Variables from URL String to Array &#8211; PHP Function">Extract GET Variables from URL String to Array &#8211; PHP Function</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/" title="Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)">Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/nicer-footer-navigation-with-pseudo-selectors/" title="Nicer Footer Navigation with pseudo-selectors">Nicer Footer Navigation with pseudo-selectors</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/php-no-such-file-or-directory-in-unknown-on-line-0-error/" title="PHP &#8211; No such file or directory in Unknown on line 0 Error">PHP &#8211; No such file or directory in Unknown on line 0 Error</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/check-if-headers-have-already-been-sent-in-php/" title="Check if Headers Have Already Been Sent in PHP">Check if Headers Have Already Been Sent in PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Extract GET Variables from URL String to Array &#8211; PHP Function</title>
		<link>http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/</link>
		<comments>http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 15:25:48 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scraping]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.logon2.com.au/blog/archive/coding/codin-php/extract-get-variables-from-url-string-to-array-php-function/</guid>
		<description><![CDATA[While making the YouTube Music in MySpace tool, I coudln&#8217;t find a function that reads an input URL then makes an array with the GET variable names and values. I needed this to get the video id from the YouTube video URL &#8211; so I made it, and now I&#8217;m sharing it. So in my [...]]]></description>
			<content:encoded><![CDATA[<p>While making the<a href="http://www.logon2.com.au/blog/archive/tools/new-tool-youtube-music-on-myspace/"> YouTube Music in MySpace </a>tool, I coudln&#8217;t find a function that reads an input URL then makes an array with the GET variable names and values.  I needed this to get the video id from the YouTube video URL &#8211; so I made it, and now I&#8217;m sharing it.</p>
<p>So in my case, the function is fed a YouTube URL and returns the video id variable (v) &#8211; So an example:<br />
Input:  http://au.youtube.com/watch?v=le860Jd-FqI&amp;feature=related<br />
Output:<br />
$output['v'] = &#8220;le860Jd-FqI&#8221;<br />
$output['feature'] = &#8220;related&#8221;</p>
<p>And so witout further ado, here&#8217;s the actual code:</p>
<pre>
function getVariableFromUrl($url) {		//we need to see if this URL is passing any GET variables

 	$variablesStart = strpos($url, "?") + 1;

if (!$variablesStart) {

 		// no variables!

 		return(false);

 	}

//before we extract the variables, we need to remove any anchors

$variablesEnd = strpos($url,"#",$variablesStart);

if ($variablesEnd) {

 		$getVariables = substr($url, $variablesStart, $variablesEnd - $variablesStart);

 	} else {

 		$getVariables = substr($url, $variablesStart);

 	}

//next, we split the URL into an arrays containing variable name and value pairs (ie. "variable=value")

 	$variableArray = explode("&amp;", $getVariables);

//we will iterate through each of the array pairs (ie. "variable=value")

 	foreach ($variableArray as $arraySet) {

$nameAndValue = explode("=", $arraySet);

//using the above examples, $nameAndValue[0] would be "variable" and $nameAndValue[1] would be "value"

 		$output[$nameAndValue[0]] = $nameAndValue[1];

}

return($output);

}</pre>
<h3>Related Posts:</h3><ul class="related_post"><li><a href="http://www.logon2.com.au/blog/archive/coding/pretty-smarties/pretty-smarties-05-pretty-urls-smarty-templates-and-faster-web-design/" title="Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design">Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/" title="Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)">Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/" title="Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial">Clean URLs Using Apache&#8217;s mod_rewrite and PHP &#8211; Beginner&#8217;s Tutorial</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/php-no-such-file-or-directory-in-unknown-on-line-0-error/" title="PHP &#8211; No such file or directory in Unknown on line 0 Error">PHP &#8211; No such file or directory in Unknown on line 0 Error</a></li><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/check-if-headers-have-already-been-sent-in-php/" title="Check if Headers Have Already Been Sent in PHP">Check if Headers Have Already Been Sent in PHP</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/introducing-the-lovely-caption-maker/" title="Introducing the Lovely Caption Maker">Introducing the Lovely Caption Maker</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

