<?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>simon.net.nz &#187; php</title> <atom:link href="http://simon.net.nz/articles/category/php/feed/" rel="self" type="application/rss+xml" /><link>http://simon.net.nz</link> <description>Dr. Simon J. Greenhill&#039;s website</description> <lastBuildDate>Tue, 29 Nov 2011 09:49:47 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>benchmarking .htaccess performance</title><link>http://simon.net.nz/articles/benchmarking-htaccess-performance/</link> <comments>http://simon.net.nz/articles/benchmarking-htaccess-performance/#comments</comments> <pubDate>Sun, 27 Aug 2006 11:37:07 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[apache]]></category> <category><![CDATA[code]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/benchmarking-htaccess-performance/</guid> <description><![CDATA[One of the common tips to increasing Apache performance is to turn off the per-directory configuration files (aka .htaccess files) and merge them all into your main Apache server configuration file (httpd.conf).
Jeremy raised an interesting question about when the performance loss caused by using many htaccess files is offset by the ease of maintenance. He's arguing - and I agree - that it makes sense to keep the configuration locally inside .htaccess files, despite the performance loss as these are easier to maintain....]]></description> <content:encoded><![CDATA[<h2>Introduction:</h2><p>One of the common tips to increasing Apache performance is to turn off the per-directory configuration files (aka .htaccess files) and merge them all into your main Apache server configuration file (httpd.conf).</p><p>Jeremy raised <a
href="http://forums.devshed.com/apache-development-15/is-the-htaccess-performance-loss-acceptable-when-it-eases-maintenance-379487.html" title="is the htaccess performance loss acceptable when it eases maintenance">an interesting question</a> about when the performance loss caused by using many htaccess files is offset by the ease of maintenance. He's arguing - and I agree - that it makes sense to keep the configuration <em>locally</em> inside .htaccess files, despite the performance loss as these are easier to maintain.</p><p>It's fairly logical that the multiple .htaccess file route will be slower - for <strong>every</strong> node in the request URI, the webserver has to look for an .htaccess file and merge the rules found in every one. So, we're going to have to have a filesystem seek'n'read for every subdirectory.</p><p>However, is this a major issue? How much of a performance hit is there? Let's find out...</p><h2>Set-up:</h2><p>Ok. Let's make two docroots each with the same structure and files.</p><p>1) htdocs_access - the .htaccess version. This has one .htaccess file in the leaf directory.</p><p>2) htdocs_config - the httpd.conf version. This has the same rule as the above, but the rule is in the server-wide httpd.conf file and htaccess support is turned OFF (AllowOverride None).</p><p>Next, we need to get the .htaccess/httpd.conf files to do something ( mainly so we can see if Apache's merged them in ). So, we'll make a number of files in the last random directory (the leaf node), and give half of them the extension .foo, and the other half .bar. We'll then tell Apache to process the .bar's with PHP, and the .foo's as text. All files will have the same content:</p><div
class="igBar"><span
id="lphp-1"><a
href="#" onclick="javascript:showPlainTxt('php-1'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-1"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li></ol></div></div></div><p></p><p>Here's the (python) code I used to generate this structure:</p><div
class="igBar"><span
id="lpython-2"><a
href="#" onclick="javascript:showPlainTxt('python-2'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-2"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;">#!/usr/bin/env python</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># where we'll place the generated structure</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">staging = <span
style="color: #483d8b;">'/Users/simon/server'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">htdocs_access = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>staging, <span
style="color: #483d8b;">'htdocs_access'</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">htdocs_config = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>staging, <span
style="color: #483d8b;">'htdocs_config'</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># how deep to go!</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">dir_depth = <span
style="color: #ff4500;color:#800000;">10</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># how many files in the leaf node of the dir.</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">num_files = <span
style="color: #ff4500;color:#800000;">50</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># what content to put in the files</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">content = <span
style="color: #483d8b;">""</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># the actual htaccess file</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">htaccess = <span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">AddHandler application/x-httpd-php .bar</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">"</span><span
style="color: #483d8b;">""</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># make directory structure</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #008000;">dir</span> = <span
style="color: #483d8b;">''</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> dirnum <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">range</span><span
style="color: black;">&#40;</span> <span
style="color: #ff4500;color:#800000;">0</span>, dir_depth <span
style="color: black;">&#41;</span>:</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #008000;">dir</span> = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> <span
style="color: #008000;">dir</span>, <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span> dirnum <span
style="color: black;">&#41;</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hta = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> htdocs_access, <span
style="color: #008000;">dir</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">htc = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> htdocs_config, <span
style="color: #008000;">dir</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #dc143c;">os</span>.<span
style="color: black;">makedirs</span><span
style="color: black;">&#40;</span> hta <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #dc143c;">os</span>.<span
style="color: black;">makedirs</span><span
style="color: black;">&#40;</span> htc <span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># make the files...</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> filenum <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">range</span><span
style="color: black;">&#40;</span> <span
style="color: #ff4500;color:#800000;">0</span>, num_files <span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># assign the file types - half .foo, and half .bar</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">if</span> filenum % <span
style="color: #ff4500;color:#800000;">2</span> == <span
style="color: #ff4500;color:#800000;">0</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">filename = <span
style="color: #483d8b;">'%d.foo'</span> % filenum</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">else</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">filename = <span
style="color: #483d8b;">'%d.bar'</span> % filenum</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f = <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> hta, filename <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> content <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f = <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> htc, filename <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> content <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># now, add the .htaccess file inside the lead htdocs_access dir</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f = <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> hta, <span
style="color: #483d8b;">'.htaccess'</span> <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> htaccess <span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># and we'll place it in the root of the htdocs_config dir as</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># httpd.conf to remind ourselves to add it to the httpd.conf file</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f = <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> htdocs_config, <span
style="color: #483d8b;">'httpd.conf'</span> <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</span> <span
style="color: black;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> htaccess <span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><p>Here's what we end up with:</p><div
class="igBar"><span
id="lcode-3"><a
href="#" onclick="javascript:showPlainTxt('code-3'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-3"><div
class="code"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">0</span>/</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">1</span>/</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">2</span>/</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">3</span>/</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">4</span>/</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">5</span>/</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">6</span>/</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">7</span>/</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">8</span>/</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">9</span>/</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">0</span>.<span
style="">foo</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">1</span>.<span
style="">bar</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">10</span>.<span
style="">foo</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">11</span>.<span
style="">bar</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#40;</span>...<span
style="">etc</span>...<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">6</span>.<span
style="">foo</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">7</span>.<span
style="">bar</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">8</span>.<span
style="">foo</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">9</span>.<span
style="">bar</span></div></li></ol></div></div></div><p></p><p>Where htdocs_access has a .htaccess file in 9/ and htdocs_config doesn't.</p><h2>Server Configuration:</h2><p>Here are the two httpd.conf files for the configurations:</p><h3>htdocs_config httpd.conf:</h3><div
class="igBar"><span
id="lcode-4"><a
href="#" onclick="javascript:showPlainTxt('code-4'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-4"><div
class="code"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">### Section <span
style="color:#800000;color:#800000;">1</span>: Global Environment</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ServerRoot <span
style="color:#CC0000;">"/usr/local/apache2"</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">PidFile logs/httpd.<span
style="">pid</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Timeout <span
style="color:#800000;color:#800000;">300</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">KeepAlive On</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxKeepAliveRequests <span
style="color:#800000;color:#800000;">100</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">KeepAliveTimeout <span
style="color:#800000;color:#800000;">15</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DirectoryIndex index.<span
style="">html</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AccessFileName .<span
style="">htaccess</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">HostnameLookups Off</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"># fixes crashes on OSX??</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AcceptMutex fcntl</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">StartServers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MinSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxClients&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">100</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxRequestsPerChild&nbsp; <span
style="color:#800000;color:#800000;">10</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">### Section <span
style="color:#800000;color:#800000;">2</span>: <span
style="color:#CC0000;">'Main'</span> server configuration</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">User nobody</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Group #-<span
style="color:#800000;color:#800000;">1</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DocumentRoot <span
style="color:#CC0000;">"/Users/simon/server/htdocs_config"</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LoadModule php5_module modules/libphp5.<span
style="">so</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Listen <span
style="color:#800000;color:#800000;">8111</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;directory&gt;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Options Indexes FollowSymLinks</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AllowOverride None</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AddHandler application/x-httpd-php .<span
style="">bar</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/directory&gt;</div></li></ol></div></div></div><p></p><h3>htdocs_access httpd.conf:</h3><div
class="igBar"><span
id="lcode-5"><a
href="#" onclick="javascript:showPlainTxt('code-5'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-5"><div
class="code"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">### Section <span
style="color:#800000;color:#800000;">1</span>: Global Environment</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ServerRoot <span
style="color:#CC0000;">"/usr/local/apache2"</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">PidFile logs/httpd.<span
style="">pid</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Timeout <span
style="color:#800000;color:#800000;">300</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">KeepAlive On</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxKeepAliveRequests <span
style="color:#800000;color:#800000;">100</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">KeepAliveTimeout <span
style="color:#800000;color:#800000;">15</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DirectoryIndex index.<span
style="">html</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AccessFileName .<span
style="">htaccess</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">HostnameLookups Off</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"># fixes crashes on OSX??</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AcceptMutex fcntl</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">StartServers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MinSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxClients&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">100</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxRequestsPerChild&nbsp; <span
style="color:#800000;color:#800000;">10</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">### Section <span
style="color:#800000;color:#800000;">2</span>: <span
style="color:#CC0000;">'Main'</span> server configuration</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">User nobody</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Group #-<span
style="color:#800000;color:#800000;">1</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DocumentRoot <span
style="color:#CC0000;">"/Users/simon/server/htdocs_access"</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LoadModule php5_module modules/libphp5.<span
style="">so</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Listen <span
style="color:#800000;color:#800000;">8111</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;directory&gt;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Options Indexes FollowSymLinks</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AllowOverride All</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/directory&gt;</div></li></ol></div></div></div><p></p><h2>Results:</h2><p>Benchmarking was done with "ab" the Apache Benchmark program, which was set to access one page 1,000 times with 10 concurrencies. Each configuration was benchmarked five times in random order (to minimise the effect of any running background processes etc).</p><table><tr><th></th><th
colspan="2">htdocs_config</th><th
colspan="2">htdocs_access</th></tr><tr><th>Test:</th><th>Time Taken (s):</th><th>Requests per Second:</th><th>Time Taken (s):</th><th>Requests per Second:</th></tr><tr><td>1</td><td>12.683213</td><td>78.84</td><td>13.21618</td><td>75.66</td></tr><tr><td>2</td><td>12.854491</td><td>77.79</td><td>13.574916</td><td>73.67</td></tr><tr><td>3</td><td>11.777676</td><td>84.91</td><td>13.163296</td><td>75.97</td></tr><tr><td>4</td><td>13.668398</td><td>73.16</td><td>12.26475</td><td>81.53</td></tr><tr><td>5</td><td>13.76753</td><td>76.47</td><td>13.264527</td><td>75.39</td></tr><tr><th>AVERAGE:</th><th>12.9</th><th>78.23</th><th>13.1</th><th>76.4</th></tr></table><p>So - we're looking at a difference of around 2.3% extra requests per second when htaccess files are disabled. This is really quite trivial, and should only be worried about when you're <em>really</em> loaded.</p><h2>Issues:</h2><p>There are a number of areas where this could be improved:</p><ul><li>Try different directory depths i.e. the more nested the directory is, the slower it should be under the .htaccess scenario. In contrast, if there's only 2 or 3 levels then it should be faster.</li><li>Have multiple .htaccess files in the intermediate nodes to see how Apache handles the merging of these files. Here we've just used one .htaccess file, and we should probably see further slowdowns if Apache has to merge some complicated rule sets.</li><li>Access different files - I just requested one file repeatedly, so we might be getting a lot of interference from any caching systems (harddrive, ram, php caches etc) that I forgot about. Additionally, requesting multiple URI's is a more realistic test case for a webserver.</li></ul> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/benchmarking-htaccess-performance/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>process a url query string in PHP</title><link>http://simon.net.nz/articles/process-a-url-query-string-in-php/</link> <comments>http://simon.net.nz/articles/process-a-url-query-string-in-php/#comments</comments> <pubDate>Wed, 26 Jul 2006 13:33:07 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/process-a-url-query-string-in-php/</guid> <description><![CDATA[A PHP function to parse a url and extract its arguments.]]></description> <content:encoded><![CDATA[<div
class="igBar"><span
id="lphp-6"><a
href="#" onclick="javascript:showPlainTxt('php-6'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-6"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Function to parse a url and extract its arguments.</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> process_url<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$url</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$processed_url</span> = <a
href="http://www.php.net/parse_url"><span
style="color:#000066;">parse_url</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$url</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query_string</span> = <span
style="color:#0000FF;">$processed_url</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'query'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000; font-style:italic;"># split into arguments and values</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query_string</span> = <a
href="http://www.php.net/explode"><span
style="color:#000066;">explode</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">'&amp;amp;'</span>, <span
style="color:#0000FF;">$query_string</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$args</span> = <a
href="http://www.php.net/array"><span
style="color:#000066;">array</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>; <span
style="color:#FF9933; font-style:italic;">// return array</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">foreach</span><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$query_string</span> <span
style="color:#616100;">as</span> <span
style="color:#0000FF;">$chunk</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$chunk</span> = <a
href="http://www.php.net/explode"><span
style="color:#000066;">explode</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">'='</span>, <span
style="color:#0000FF;">$chunk</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// it's only really worth keeping if the parameter</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// has an argument.</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/count"><span
style="color:#000066;">count</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$chunk</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> == <span
style="color:#CC66CC;color:#800000;">2</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/list"><span
style="color:#000066;">list</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$key</span>, <span
style="color:#0000FF;">$val</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> = <span
style="color:#0000FF;">$chunk</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$args</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#0000FF;">$key</span> <span
style="color:#006600; font-weight:bold;">&#93;</span> = <a
href="http://www.php.net/urldecode"><span
style="color:#000066;">urldecode</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$val</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#0000FF;">$args</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$url</span> = <span
style="color:#FF0000;">'http://www.google.co.nz/search?q=simon+rocks!&amp;amp;start=0&amp;amp;ie=utf-8&amp;amp;oe=utf-8&amp;amp;client=firefox-a&amp;amp;rls=org.mozilla:en-US:official'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$result</span> = process_url<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$url</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/print_r"><span
style="color:#000066;">print_r</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$result</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Will result in something like this:</p><div
class="igBar"><span
id="lphp-7"><a
href="#" onclick="javascript:showPlainTxt('php-7'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-7"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/array"><span
style="color:#000066;">Array</span></a></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#40;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#91;</span>q<span
style="color:#006600; font-weight:bold;">&#93;</span> =&amp;gt; simon rocks!</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#91;</span>start<span
style="color:#006600; font-weight:bold;">&#93;</span> =&amp;gt; <span
style="color:#CC66CC;color:#800000;">0</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#91;</span>ie<span
style="color:#006600; font-weight:bold;">&#93;</span> =&amp;gt; utf-<span
style="color:#CC66CC;color:#800000;">8</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#91;</span>oe<span
style="color:#006600; font-weight:bold;">&#93;</span> =&amp;gt; utf-<span
style="color:#CC66CC;color:#800000;">8</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#91;</span>client<span
style="color:#006600; font-weight:bold;">&#93;</span> =&amp;gt; firefox-a</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#91;</span>rls<span
style="color:#006600; font-weight:bold;">&#93;</span> =&amp;gt; org.mozilla:en-US:official</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li></ol></div></div></div><p></p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/process-a-url-query-string-in-php/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Protecting MySQL from SQL Injection Attacks with PHP.</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/</link> <comments>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/#comments</comments> <pubDate>Sun, 04 Jun 2006 12:58:07 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/</guid> <description><![CDATA[This is intended as a brief guide to protecting your MySQL database from SQL injection attacks. Unfortunately, a large amount of the code that I've seen written by people on forums, and in countless crappy PHP tutorials lurking around on the net, and in the many websites that display the "magic breeding slashed-quote" show that many people just do not understand what's going on and how to protect themselves against SQL injection attacks.]]></description> <content:encoded><![CDATA[<p>This is intended as a brief guide to protecting your MySQL database from SQL injection attacks. Unfortunately, a large amount of the code that I've seen written by people on <a
href="http://forums.devshed.com/" title="Devshed Forums">forums</a>, and in countless crappy PHP tutorials lurking around on the net, and in the many websites that display the magic breeding slashed-quote (\' - <a
href="#mbsq" title="whats a Magic Breeding Slashed Quote?">see below</a>) show that many people just do not understand what's going on and how to protect themselves against SQL injection attacks.</p><p>In fact, the only reason that many websites are "protected" is due to magic quotes, and given that this is due to be disabled in the forthcoming PHP6, then there's going to be some major problems cropping up.</p><p>I'll talk about the problem of SQL injection, the half-hearted attempt to fix it with these "magic quotes", and what you should really be doing EVERY TIME you send user inputted data to your database.</p><h2> The Problem - What is SQL Injection:</h2><p>As the name suggests, SQL Injection is quite simply, when the user injects SQL into your application. How does this happen? Say we have a nice simple login form that takes a username and password, and checks if that's in the database. If it is, the user is logged into an admin section or something. The code for this could look something like this:</p><div
class="igBar"><span
id="lphp-8"><a
href="#" onclick="javascript:showPlainTxt('php-8'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-8"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// user and password come from a simple POST'ed form</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$user</span> = <span
style="color:#0000FF;">$_POST</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'user'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$password</span> = <span
style="color:#0000FF;">$_POST</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'password'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#FF0000;">"SELECT name, age, credit_card</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">FROM usertable</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">WHERE username = '$user' AND password = '$password' "</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$result</span> = <a
href="http://www.php.net/mysql_query"><span
style="color:#000066;">mysql_query</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$query</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// check if mysql found anything, and get the record if it did</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/mysql_num_rows"><span
style="color:#000066;">mysql_num_rows</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$result</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>&amp;gt; <span
style="color:#CC66CC;color:#800000;">0</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$data</span> = <a
href="http://www.php.net/mysql_fetch_assoc"><span
style="color:#000066;">mysql_fetch_assoc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$result</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">'</span></div></li><li
style="font-weight: bold;color:#26536A;"></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">Hello '</span>.<span
style="color:#0000FF;">$user</span>.<span
style="color:#FF0000;">'!</span></div></li><li
style="font-weight: bold;color:#26536A;"></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">Your credit card number is '</span>.<span
style="color:#0000FF;">$data</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'credit_card'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>.<span
style="color:#FF0000;">'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">else</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">Incorrect Username or Password! Go Away!</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">?&amp;gt;</div></li></ol></div></div></div><p></p><p>Ok. This works, BUT it's about as safe as juggling with scalpels. If I enter "simon" as my username, and "secret" as my password, then the query that goes to MySQL looks like this:</p><div
class="igBar"><span
id="lphp-9"><a
href="#" onclick="javascript:showPlainTxt('php-9'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-9"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'simon'</span> AND password = <span
style="color:#FF0000;">'secret'</span></div></li></ol></div></div></div><p></p><p>and I get logged in quite happily. Fantastic.</p><p>The problem comes when I start entering other characters. Let's say that the next user who trys to login is Peter O'Reilly. Naturally he'll want a username something like PeterO'Reilly. If we plug that into our query we get this:</p><div
class="igBar"><span
id="lphp-10"><a
href="#" onclick="javascript:showPlainTxt('php-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-10"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'PeterO'</span>Reilly<span
style="color:#FF0000;">' AND password = '</span>secret<span
style="color:#FF0000;">' </span></div></li></ol></div></div></div><p></p><p>MySQL blasts along quite happily and hits username='PeterO', and then it gets this "Reilly" thing which it doesn't know what to do with and this happens:</p><div
class="igBar"><span
id="lphp-11"><a
href="#" onclick="javascript:showPlainTxt('php-11'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-11"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ERROR <span
style="color:#CC66CC;color:#800000;">1064</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#CC66CC;color:#800000;">42000</span><span
style="color:#006600; font-weight:bold;">&#41;</span>: You have an error in your SQL syntax; check the manual that corresponds to your <a
href="http://www.php.net/mysql"><span
style="color:#000066;">MySQL</span></a> server version <span
style="color:#616100;">for</span> the right syntax to use near <span
style="color:#FF0000;">'Reilly'</span> and password=<span
style="color:#FF0000;">"secret"</span>; <span
style="color:#FF0000;">''</span> at line <span
style="color:#CC66CC;color:#800000;">1</span></div></li></ol></div></div></div><p></p><p>Nice. We have a broken website, Pete can't login, and he's left with misgivings about our web programming skills.</p><p>Even worse - what happens if I enter my password as this?</p><div
class="igBar"><span
id="lphp-12"><a
href="#" onclick="javascript:showPlainTxt('php-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-12"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">' or 1=1 ; -- </span></div></li></ol></div></div></div><p></p><p>The query that gets sent to MySQL will look like this:</p><div
class="igBar"><span
id="lphp-13"><a
href="#" onclick="javascript:showPlainTxt('php-13'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-13"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'simon'</span> AND password = <span
style="color:#FF0000;">''</span> or <span
style="color:#CC66CC;color:#800000;">1</span>=<span
style="color:#CC66CC;color:#800000;">1</span> ; -- <span
style="color:#FF0000;">' </span></div></li></ol></div></div></div><p></p><p>What does this mean? It tells MySQL to find all rows with a username equal to "simon" and a password equal to an empty string OR "1=1". To represent that a bit more logically:</p><p>( username = "simon" and password = "" ) || ( 1 = 1 )</p><p>Now, 1=1 is always going to be true, so this is equal to:</p><p>( false ) || ( true )</p><p>Which means that ALL the records in the table will get returned. Our login processer above is going to log me on with someone else's credentials - in fact, those of the first record returned.</p><p>Keep in mind, however, that we don't need to escape numbers, and we shouldn't put quote marks around them (it's not standard SQL)- if a variable is a number, then it'll be fine.</p><h2> The crap attempt to fix it ( magic quotes ):</h2><p>How to fix this? We need to be escape these quote characters ( both single and double quotes, as well as backslashes). This is done by putting a slash in front of them, e.g. so a ' becomes a \', and MySQL can work out that that quote mark is "protected" by the slash, and is part of the value and ignores it. So, Peter's attempt to login becomes:</p><div
class="igBar"><span
id="lphp-14"><a
href="#" onclick="javascript:showPlainTxt('php-14'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-14"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'PeterO<span
style="color:#000099; font-weight:bold;">\'</span>Reilly'</span> AND password = <span
style="color:#FF0000;">'secret'</span>;</div></li></ol></div></div></div><p></p><p>and my attempt to hack my way in becomes:</p><div
class="igBar"><span
id="lphp-15"><a
href="#" onclick="javascript:showPlainTxt('php-15'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-15"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'simon'</span> AND password = <span
style="color:#FF0000;">'<span
style="color:#000099; font-weight:bold;">\'</span> or 1=1 ; -- '</span>;</div></li></ol></div></div></div><p></p><p>MySQL now thinks my password is the string</p><div
class="igBar"><span
id="lphp-16"><a
href="#" onclick="javascript:showPlainTxt('php-16'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-16"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">' or 1=1 ; </span></div></li></ol></div></div></div><p></p><p>and I won't be able to login.</p><p>So where do we get these slashes? Since around PHP version 3.06, PHP tries to do this for you, with a setting called <a
href="http://www.php.net/magic_quotes" title="magic_quotes">"magic_quotes"</a> . What this does is to automatically add slashes to anything coming in via HTTP get or post requests and via cookies. You can also do this manually using the <em><a
href="http://www.php.net/addslashes" title="addslashes()">addslashes()</a></em> function.</p><h3>But do not rely on this!</h3><ul><li>It could be turned off or on, or not present in your version (and it won't be in PHP6). Therefore YOU CANNOT RELY ON IT, AND HAVE TO HANDLE THIS YOURSELF.</li><li> This means that if you rely on magic quotes, then your code is not portable. Unfortunately, escaping things with a slash is one of those irritating non-standard MySQL features. Most other databases which follow the SQL standards (like Postgres), escape things with another single quote ( O'Reilly =&gt; O''Reilly ).</li><li> It's crappy. It doesn't work well with extended characters, and these can be used to get around the slashes. <a
href="http://shiflett.org/archive/184" title="shiflett.org">See Chris Shiflett's discussion of this problem</a></li><li> It's irritating - it pollutes your data with stuff that the user didn't enter. This is the major cause of the magic breeding slashed-quote.</li></ul><h3> (Aside) The Magic Breeding Slashed-Quote:</h3><p><a
id="mbsq"></a></p><p>I'm sure you've all seen websites that have this really annoying habit of messing up their user\'s post\'s quote marks ( just like that ). I'm calling this the magic breeding slashed quote, because these things propagate like crazy. What's happening here is that the hard working web developer is adding slashes to the data they send to their database - great! BUT, they're not checking for magic quotes, so PHP is escaping the ' once to \', and then when the website runs addslashes() again, php sees a backslash AND a single quote which need escaping ( remember that the three characters that get escaped are \, ', and " ). This therefore becomes \\\'. MySQL comes along and sees an escaped backslash AND an escaped single quote.</p><p>Here's what's happening:</p><ol><li> user input: O'Reilly</li><li> magic quotes: O\'Reilly</li><li> addslashes: O\\\'Reilly</li><li> MySQL processes this to: O\'Reilly</li></ol><p>and we end up with O\'Reilly stored when we really want O'Reilly.</p><p>I've actually seen applications which quite happily store O\'Reilly, and <a
href="http://www.php.net/stripslashes" title="stripslashes()">stripslashes()</a> before they display the data - this is just blindingly stupid.</p><h2> Fixing it.</h2><p>So, we need a way of escaping data that isn't crappy, isn't as prone to character set issues, and isn't prone to magic breeding slashed quotes.</p><p>There are two ways to do this.</p><ul><li> Use better slashes ( PHP4, old mysql client library using the mysql_* functions )</li><li> Use a better technique - bound parameters ( PHP5 with the new mysqli_* client library)</li></ul><h2> Using better slashes - mysql_real_escape_string( ) (PHP4, mysql_* )</h2><p>If you're using the old mysql client library ( i.e. the <a
href="http://www.php.net/mysql" title="mysql_* functions">mysql_* functions</a> ), then you have to use the hideously named <a
href="http://www.php.net/mysql_real_escape_string" title="mysql_real_escape_string in the PHP manual">mysql_real_escape_string()</a> function. This takes into account the character set of the database connection and should handle things appropriately.</p><p><strong>Note:</strong> mysql_real_escape_string needs an active database connection, or anything sent to it will disappear ( WTF? ), or it will generate an error.</p><p>BUT we still need to check for the evil magic_quotes setting, which and remove it. We can do this with the <a
href="http://www.php.net/get_magic_quotes_gpc" title="get_magic_quotes_gpc on php.net">get_magic_quotes_gpc()</a> function ( "gpc" refers to Get, Post, and Cookies which magic quotes operates on ).</p><p>So - something like this:</p><div
class="igBar"><span
id="lphp-17"><a
href="#" onclick="javascript:showPlainTxt('php-17'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-17"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// remove the pesky slashes from magic quotes if it's turned on</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> clean_string<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span>, <span
style="color:#0000FF;">$DB</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/get_magic_quotes_gpc"><span
style="color:#000066;">get_magic_quotes_gpc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$value</span> = <a
href="http://www.php.net/stripslashes"><span
style="color:#000066;">stripslashes</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// escape things properly</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span>, <span
style="color:#0000FF;">$DB</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$string</span> = <span
style="color:#FF0000;">"O'Reilly"</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// where $db is your active database connection resource id.</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$safe_string</span> = clean_string<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$string</span>, <span
style="color:#0000FF;">$db</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>There's a function described in the <a
href="http://www.php.net/mysql_real_escape_string" title="quote_smart">PHP manual called quote_smart</a>, that handles this and handles both strings and integers:</p><div
class="igBar"><span
id="lphp-18"><a
href="#" onclick="javascript:showPlainTxt('php-18'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-18"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Quote variable to make safe</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> quote_smart<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$value</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Stripslashes</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/get_magic_quotes_gpc"><span
style="color:#000066;">get_magic_quotes_gpc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$value</span> = <a
href="http://www.php.net/stripslashes"><span
style="color:#000066;">stripslashes</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Quote if not a number or a numeric string</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> !<a
href="http://www.php.net/is_numeric"><span
style="color:#000066;">is_numeric</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$value</span> = <span
style="color:#FF0000;">"'"</span> . <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$value</span><span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">"'"</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#0000FF;">$value</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><p>Note that you'll need to implement this yourself, and you'll have to rewrite your queries to not have quotes in them eg:</p><div
class="igBar"><span
id="lphp-19"><a
href="#" onclick="javascript:showPlainTxt('php-19'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-19"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$variable</span> = <span
style="color:#FF0000;">"O'Reilly"</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$variable</span> = quote_smart<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$variable</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// note that we haven't surrounded $variable with quote marks in</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// the query below since quote_smart does that for us.</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#FF0000;">"SELECT x, y, z FROM tablename WHERE user = $variable"</span>;</div></li></ol></div></div></div><p></p><p>However, this leaving quote marks out of the query irritates me enough, that I generally just type-cast anything which should be a number to a number:</p><div
class="igBar"><span
id="lphp-20"><a
href="#" onclick="javascript:showPlainTxt('php-20'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-20"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> clean_int<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$i</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/is_numeric"><span
style="color:#000066;">is_numeric</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$i</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> int <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#0000FF;">$i</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// return False if we don't get a number</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">else</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#000000; font-weight:bold;">False</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><h3>Warning:</h3><p>This is NOT foolproof. In fact, if the attacker can change the character set on the fly, then this whole system can be avoided. <a
href="http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html" title="mysql_real_escape_string versus Prepared Statements">Ilia Alshanetsky has an excellent write up on this</a>.</p><h2> Fixing it with better technique - bound parameters ( PHP5, MySQLi ):</h2><p>So - the best solution? Use bound parameters. To use these you'll need to be using the <a
href="http://www.php.net/mysqli" title="PHP Documentation for MySQLi"> improved <strong>mysqli</strong> </a>library that comes with PHP5. This technique differs slightly in that you define a query "template" first with placeholders, and then "bind" the parameters to it, and the mysqli library takes care of  the appropriate escaping for us:</p><div
class="igBar"><span
id="lphp-21"><a
href="#" onclick="javascript:showPlainTxt('php-21'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-21"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$variable</span> = <span
style="color:#FF0000;">"O'Reilly"</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// prepare the query</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#0000FF;">$mysqli</span>-&amp;gt;prepare<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">"SELECT x, y, z FROM tablename WHERE user = ?"</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// bind a parameter - here the first parameter is a short string that specifies the type that the</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// subsequent arguments should be:</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 's' means a string</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 'd' means a double</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 'i' means an integer</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 'b' is a blob</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;bind_param<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">'s'</span>,&nbsp; <span
style="color:#0000FF;">$variable</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// execute query:</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;execute<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// so if we had a more complex query, which updated the user info with</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// &quot;favorite_color&quot; (a string), &quot;age&quot; ( an integer ) and &quot;description&quot;, a blob:</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#0000FF;">$mysqli</span>-&amp;gt;prepare<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">"UPDATE tablename SET favorite_color = ?, age = ?, description = ? WHERE user = ?"</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// we would have a bind looking like this:</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;bind_param<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">'sibs'</span>, <span
style="color:#FF0000;">'red'</span>, <span
style="color:#CC66CC;color:#800000;">27</span>, <span
style="color:#0000FF;">$some_blob</span>, <span
style="color:#0000FF;">$variable</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;execute<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Another benefit of this method is that it's faster to transfer data to the db server. <a
href="http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html" title="MySQLs Prepared Statements">Harrison Fisk has a good discussion of these here</a>.</p><h2> Another thing to keep in mind:</h2><p>Now, properly using mysql_real_escape_string or prepared statements should keep you pretty safe, but there are a few characters you might also want to watch out for:</p><h3> The Percentage Sign (%)</h3><p>The percentage symbol is commonly used by MySQL to perform LIKE queries - this WON'T get escaped. If your application is doing LIKE comparisons, and your database is large, then it's worth checking for this specifically to avoid a friendly user entering "%" and making your database grind to a halt - e.g.</p><div
class="igBar"><span
id="lphp-22"><a
href="#" onclick="javascript:showPlainTxt('php-22'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-22"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$user_input</span> = <span
style="color:#FF0000;">'%'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#FF0000;">"SELECT x,y,z FROM tablename WHERE user LIKE '%$user_input%';</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">// becomes LIKE %%% -&amp;gt; and returns all rows in tablename. </span></div></li></ol></div></div></div><p></p><h3>Edit: 15th August, 2006 -</h3><p>James Laver has written <a
href="http://www.sourcemill.org/2006-08-11/a-clean-php5-mysqli-wrapper/" title="A Clean PHP5 MySQLi Wrapper"> a nice lightweight database access class for MySQLi</a> which takes care of the binding of parameters for you.</p><h3>Edit: 9th November, 2006 -</h3><p>Fixing a link, <a
href="http://www.thecodergeek.com/blog/">thanks Peter <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </a></p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/feed/</wfw:commentRss> <slash:comments>23</slash:comments> </item> </channel> </rss>
