<?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>Ardamis &#187; php</title>
	<atom:link href="http://www.ardamis.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ardamis.com</link>
	<description>Ardamis is a blog about web development and technology in general.</description>
	<lastBuildDate>Thu, 02 Feb 2012 07:07:02 +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>My new Dell Precision 690 workstation</title>
		<link>http://www.ardamis.com/2011/10/20/my-new-dell-precision-690-workstation/</link>
		<comments>http://www.ardamis.com/2011/10/20/my-new-dell-precision-690-workstation/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 06:06:52 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux-Ubuntu]]></category>
		<category><![CDATA[Materialism]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[XAMPP]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1737</guid>
		<description><![CDATA[I'm not a hardware guy, but I am excited about what I'm going to do with my new-to-me Dell Precision WorkStation 690.]]></description>
			<content:encoded><![CDATA[<p>I just picked up an old <a href="http://www.dell.com/downloads/global/products/precn/en/spec_precn_690_en.pdf">Dell Precision 690 workstation</a>, which I intend to develop into a file server, a Windows IIS server, and an Ubuntu LAMP server.  This monster was built in 2006, but it still has some neat specs and tons of capacity (7 PCI slots, 4 hard drive bays, etc&#8230;), should I want to expand further.</p>
<h2>The main specs</h2>
<p><strong>CPU:</strong> Dual Core <a href="http://ark.intel.com/products/27211/Intel-Xeon-Processor-5060-(4M-Cache-3_20-GHz-1066-MHz-FSB)">Intel Xeon 5060</a> 3.2GHz, 4M Cache, 1066 MHz FSB<br />
<strong>RAM:</strong> 2GB DDR2 PC2-5300, CL=5, Fully Buffered, ECC, DDR2-667<br />
<strong>HD:</strong> SAS Fujitsu MAX3073RC 73GB, 15000 RPM, 16MB Cache<br />
<strong>Video:</strong> <a href="http://en.wikipedia.org/wiki/Nvidia_Quadro">Nvidia Quadro</a> NVS 285 PCI-Express, 128MB</p>
<h2>This is not a normal tower</h2>
<p>Right away, the size of this thing suggests it isn&#8217;t a normal tower.  It&#8217;s about up to my knee and weights 70 lbs.  It feels like it&#8217;s made with heavier gauge steel than the typical chassis, but that may be me projecting.</p>
<p>I immediately <a href="http://www.ebay.com/sch/i.html?_nkw=dell+workstation+690+memory&#038;_sacat=0&#038;LH_BIN=1&#038;_sop=15&#038;_odkw=dell+workstation+690+memory&#038;_osacat=0&#038;_trksid=p3286.c0.m270.l1313">shopped around for more RAM</a>, obviously.  2GB seems a little thin, even by 2006 standards, when considering the way everything else is high-end.  The mainboard has 8 slots and supports up to 32GB, but I figure 6GB is a safe place to start.</p>
<p>The workstation has three enormous fans, like, big-as-your-hand big.  Running it with the chassis open causes some sort of thermal protection system to kick in and it spins the fans up to the point that they were blowing stuff on the floor half-way across the room.</p>
<p>The CPU has a big, passive heat sink with six copper pipes and sits between two of those fans.  I&#8217;m tempted to buy a second CPU, but I&#8217;ll hold off.</p>
<p>I&#8217;m still on the fence about the SCSI drive.  It should be super fast, but I&#8217;m a little spoiled by the SSD in my machine at work, so it&#8217;s hard to get excited about a mechanical drive, even one running at 15k RPM.</p>
<p>The Nvidia Quadro card is also fanless, and has a bizarre <a href="http://en.wikipedia.org/wiki/DMS-59">DMS-59</a> connector.  An adapter converts the DMS-59 connector into two DVI outputs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/10/20/my-new-dell-precision-690-workstation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A PHP MySQL snippet for looking up names</title>
		<link>http://www.ardamis.com/2011/10/16/a-php-mysql-snippet-for-looking-up-names/</link>
		<comments>http://www.ardamis.com/2011/10/16/a-php-mysql-snippet-for-looking-up-names/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 00:26:35 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Nonsense]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1775</guid>
		<description><![CDATA[A PHP snippet and MySQL query for an Ajax autocompleter lookup of people names by either "Firstname Lastname" or "Lastname, Firstname" from a single input field.]]></description>
			<content:encoded><![CDATA[<p>For a recent project, I needed to create a form that would perform a look up of people names in a MySQL database, but I wanted to use a single input field.  To make it easy on the users of the form, I wanted the input field to accept names in either &#8220;Firstname Lastname&#8221; or &#8220;Lastname, Firstname&#8221; format, and I wanted it to autocomplete matches as the users typed, including when they typed both names separated by a space or a comma followed by a space.</p>
<p>The Ajax lookup was quick work with <a href="http://jqueryui.com/demos/autocomplete/">jQuery UI&#8217;s Autocomplete widget</a>.  The harder part was figuring out the most simple table structure and an appropriate SQL query.</p>
<h2>A flawed beginning</h2>
<p>My people table contains a &#8220;first_name&#8221; column and a &#8220;last_name&#8221; column, nothing uncommon there.  To get the project out the door, I wrote a PHP function that ran two ALTER TABLE queries on the people table to create two additional columns for pre-formatted strings (column &#8220;firstlast&#8221;, to be formatted as &#8220;Firstname Lastname&#8221;, and column &#8220;lastfirst&#8221;, to be formatted as &#8220;Lastname, Firstname&#8221;), added indexes on these columns, and then walked through each record in the table, populating these new fields.  I then wrote a very straight forward SQL query to perform a lookup on both fields.  The PHP and query looked something like this:</p>
<pre class="brush: php; title: ; notranslate">
// The jQuery UI Autocomplete widget passes the user input as a value for the parameter &quot;name&quot;
$name= $_GET['name'];

// This SQL query uses argument swapping
$query = sprintf(&quot;SELECT * FROM people WHERE (`firstlast` LIKE '%1\$s' OR `lastfirst` LIKE '%1\$s') ORDER BY `lastfirst` ASC&quot;,
mysql_real_escape_string($name. &quot;%&quot;, $link));
</pre>
<p>This was effective, accurate, and pretty fast, but the addition of columns bothered me and I didn&#8217;t like that I needed to run a process to generate those pre-formatted fields each time a record was added to the table (or if a change was made to an existing record).  One possible alternative was to watch the input and match either lastname or firstname until the user entered a comma or a space, then explode the string on the comma or space and search more precisely.  Once a comma or a space was encountered, I felt pretty sure that I would be able to accurately determine which part of the input was the first name and which was the last name.  But this had that same inefficient, clunky bad-code-smell as the extra columns.  (Explode is one of those functions that I try to avoid using.)  Writing lots of extra PHP didn&#8217;t seem necessary or right.</p>
<p>I&#8217;m much more comfortable with PHP than with MySQL queries, but I realize that one can do some amazing things within the SQL query, and that it&#8217;s probably faster to use SQL to perform some functions.  So, I decided that I&#8217;d try to work up a query that solved my problem, rather than write more lines of PHP.</p>
<h2>CONCAT_WS to the rescue</h2>
<p>I Googled around for a bit and settled on using <code><a href="http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_concat-ws">CONCAT_WS</a></code> to concatenate the first names and last names into a single string be matched, but found it a bit confusing to work with.  I kept trying to use it to create an alias, &#8220;lastfirst&#8221;, and then use the alias in the WHERE clause, which doesn&#8217;t work, or I was getting the literal column names back instead of the values.  Eventually, I hit upon the correct usage.</p>
<p>The PHP and query now looks like this:</p>
<pre class="brush: php; title: ; notranslate">
// The jQuery UI Autocomplete widget passes the user input as a value for the parameter &quot;name&quot;
$name= $_GET['name'];

// This SQL query uses argument swapping
$query = sprintf(&quot;SELECT *, CONCAT_WS(  ', ',  `last_name`,  `first_name` ) as lastfirst FROM people WHERE (CONCAT_WS(  ', ',  `last_name`,  `first_name` ) LIKE '%1\$s' OR CONCAT_WS(  ' ',  `first_name`,  `last_name` ) LIKE '%1\$s') ORDER BY lastfirst ASC&quot;,
mysql_real_escape_string($name. &quot;%&quot;, $link));
</pre>
<p>The first instance of CONCAT_WS isn&#8217;t needed for the lookup.  The first instance allows me to order the results alphabetically and provides me an array key of &#8220;lastfirst&#8221; with a value of the person&#8217;s name already formatted as &#8220;Lastname, Firstname&#8221;, so I don&#8217;t have to do it later with PHP.  The lookup comes from the two instances of CONCAT_WS in the WHERE clause.  I haven&#8217;t done any performance measuring here, but the results of the lookup get back to the user plenty fast enough, if not just as quickly as the method using dedicated columns.</p>
<p>The result of the query is output back to the page as JSON-formatted data for use in the jQuery Autocomplete.</p>
<p>The end result works exactly as I had hoped.  A user of the form is able to type a person&#8217;s name in whatever way is comfortable to them, as &#8220;Bob Smith&#8221; or &#8220;Smith, Bob&#8221;, and the matches are found either way.  The only thing it doesn&#8217;t do is output the matches back to the autocompleter in the same format that the user is using.  But I can live with that for now. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/10/16/a-php-mysql-snippet-for-looking-up-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GoDaddy phpMyAdmin error #1045 &#8211; Access denied for user</title>
		<link>http://www.ardamis.com/2011/10/11/godaddy-phpmyadmin-error-1045-access-denied-for-user/</link>
		<comments>http://www.ardamis.com/2011/10/11/godaddy-phpmyadmin-error-1045-access-denied-for-user/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 02:45:33 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[godaddy]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1768</guid>
		<description><![CDATA[How GoDaddy's new MySQL database form accepts mixed case characters as a username, then converts the username to lowercase without alerting you, potentially causing login problems down the road.]]></description>
			<content:encoded><![CDATA[<p>While setting up a new MySQL account at a GoDaddy hosted web site, I kept getting an error when logging in to phpMyAdmin.</p>
<blockquote><p>
<strong>Error</strong><br />
#1045 &#8211; Access denied for user
</p></blockquote>
<p>For things like database usernames/passwords and other things that I&#8217;ll never have to remember or type, I like to use a long string of random characters.  One excellent source of such strings is <a href="https://www.grc.com/passwords.htm">GRC&#8217;s Ultra High Security Password Generator</a>.  I typically use a subset of the 63 random alpha-numeric characters (a-z, A-Z, 0-9) in the bottom box.  This gives me a good mix of uppercase, lowercase, and numbers, which satisfies the requirements of most password systems that require even minimum complexity.</p>
<p>So, I picked a string of characters for the database name and a different string for the password (making sure the password contained at least 1 uppercase character and 1 number), pasted them into the config.php file I was going to use on the project and then pasted them into the database setup form and created my database.  No problem.</p>
<p>I gave it 10 or 15 minutes to get all set up and then launched phpMyAdmin.  I copied and pasted the username and password from my config file into the log in fields and wham, I got the #1045.</p>
<p>After much second guessing and more copying and pasting, all with no luck, I tried resetting the password back in the Hosting Control Center.  I waited a few more minutes for good measure and tried again.  Still, #1045 &#8211; Access denied for user.</p>
<p>Then it was time to Google, which turned up a thread full of people with the same experience at <a href="http://community.godaddy.com/groups/web-hosting/forum/topic/mysql-login-error-1045-access-denied-for-user/?sid&#038;sp=1&#038;topic_page=1&#038;num=15">http://community.godaddy.com/groups/web-hosting/forum/topic/mysql-login-error-1045-access-denied-for-user/?sid&#038;sp=1&#038;topic_page=1&#038;num=15</a>.</p>
<p>Back in the Control Center, I noticed that the mixed case characters I&#8217;d used for the database/username had been converted to lowercase.  So I tried using the lowercase version at phpMyAdmin and still no luck.</p>
<p>I submitted a support ticket, as recommended in the thread, and then called Customer Support for good measure.</p>
<p>The guy confirmed that the database was in good shape and that the last password reset took effect, then had me reset it again.  And of course, when I tried to log into phpMyAdmin a moment later with the lowercase username, it went right in.</p>
<h2>The fix (or a plausible explanation, at least)</h2>
<p>The lesson learned here, is that even though the new MySQL database setup form will accept mixed case characters as the database name/username, it will <em>silently convert them to lowercase on you</em>.  The phpMyAdmin login, then, is case sensitive, so you may want to copy and paste from the Control Center into phpMyAdmin to be sure you&#8217;re feeding it the correct username.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/10/11/godaddy-phpmyadmin-error-1045-access-denied-for-user/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dreamweaver open files in code view</title>
		<link>http://www.ardamis.com/2011/10/01/dreamweaver-open-files-in-code-view/</link>
		<comments>http://www.ardamis.com/2011/10/01/dreamweaver-open-files-in-code-view/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 04:47:00 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1745</guid>
		<description><![CDATA[A simple registry merge can fix Dreamweaver's default setting of opening .php and .asp files in Design view, instead of Code view.]]></description>
			<content:encoded><![CDATA[<p>If there is one thing that bothers me about Dreamweaver&#8217;s default settings (other than its annoying habit of <a href="http://www.ardamis.com/2009/09/30/dreamweaver-rewriting-valid-code-as-camelcase/">rewriting valid code as camelCase</a>), it is that double-clicking .php and .asp files launches Dreamweaver in Design view.  Who on earth thinks that people generally want to open these kinds of files in anything other than code view?</p>
<p>Well, thankfully, this ridiculous behavior can be changed with a quick registry tweak.  Just add an extension to the &#8220;Open As Text&#8221; value to cause Dreamweaver to always open that file type in code view.  The .reg file below assumes you&#8217;re running Dreamweaver CS5.</p>
<pre class="brush: plain; title: ; notranslate">
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Adobe\Dreamweaver CS5\Helper Applications Preferences]
&quot;Open As Text&quot;=&quot;.js .asa .css .cs .config .inc .txt .as .asc .asr .vb .htaccess .htpasswd .php .asp .html&quot;
</pre>
<p>It would be nice if Dreamweaver were smart enough to realize that, if your preferred layout is Coder, any file should be opened in Code view.</p>
<p>The link below explains how to do the same thing via Edit | Preferences.</p>
<p>Source: <a href="http://help.adobe.com/en_US/dreamweaver/cs/using/WSc78c5058ca073340dcda9110b1f693f21-7be2a.html">Open files in Code view by default</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/10/01/dreamweaver-open-files-in-code-view/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A cache-friendly method for reducing WordPress comment spam</title>
		<link>http://www.ardamis.com/2011/08/27/a-cache-proof-method-for-reducing-comment-spam/</link>
		<comments>http://www.ardamis.com/2011/08/27/a-cache-proof-method-for-reducing-comment-spam/#comments</comments>
		<pubDate>Sat, 27 Aug 2011 08:34:53 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Nonsense]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[comment spam]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[spam]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1673</guid>
		<description><![CDATA[A safe-for-cached-pages method of filtering out spam comments by requiring at least some time to have passed between the time the page is loaded and the form is submitted.]]></description>
			<content:encoded><![CDATA[<p>In the endless battle against WordPress comment spam, I&#8217;ve developed and then refined a few different methods for preventing spam from getting to the database to begin with.  My philosophy has always been that a human visitor and a spam bot behave differently (after all, we&#8217;re not dealing with <a href="http://en.wikipedia.org/wiki/Do_Androids_Dream_of_Electric_Sheep%3F#Androids">Nexus-6 model androids</a> here), and an effective spam-prevention method should be able to recognize the differences.  I also have a dislike for CAPTCHA methods that require a human visitor to <em>prove</em>, via an intentionally difficult test, that they aren&#8217;t a bot.  The ideal method, I feel, would be invisible to a human visitor, but still accurately identify comments submitted by bots.</p>
<h2>A history of spam fighting</h2>
<p>The most successful and simple method I found was a server-side system for <a href="http://www.ardamis.com/2007/12/15/using-timestamps-to-reduce-wordpress-comment-spam/">reducing comment spam by using a handshake method involving timestamps</a> on hidden form fields.  The general idea was that a bot would submit a comment more quickly than a human visitor, so if the comment was submitted too soon after the page was loaded, it was rejected.  A human caught in this trap would be able to click the Back button on the browser to resubmit.  This had proven to be very effective on ardamis.com, cutting the number of <a href="http://www.ardamis.com/2010/08/09/reducing-wordpress-spam-comments/">spam comments intercepted by Akismet per day to nearly zero</a>.  For a long time, the only problem was that it required modifying a core WordPress file, <strong>wp-comments-post.php</strong>.  Each time WordPress was updated, the core file was replaced.  If I didn&#8217;t then go back and make my modifications again, <a href="http://www.ardamis.com/2011/01/18/a-chart-illustrating-the-reduction-in-comment-spam-at-ardamis-com/">I would lose the spam protection</a> until I made the changes.  As it became easier to update WordPress (via the admin panel) and I updated it more frequently, editing the core file became more of a nuisance.</p>
<h2>A huge facepalm</h2>
<p>When Google began weighting page load times as part of its ranking algorithm, I implemented the <a href="http://wordpress.org/extend/plugins/wp-super-cache/" title="WP Super Cache">WP Super Cache</a> caching plugin on ardamis.com and configured it to use .htaccess and mod_rewrite to serve cache files.  Page load times certainly decreased, but the amount of spam detected by Akismet increased.  After a while, I realized that this was because the spam bots were submitting comments from static, cached pages, and the timestamps on those pages, which had been generated server-side with PHP, were already minutes old when the page was requested.  The form processing script, which normally rejects comments that are submitted too quickly to be written by a human visitor, happily accepted the timestamps.  Even worse, a second function of my anti-spam method also rejected comments that were submitted 10 minutes or more after the page was loaded.  Of course, most of the visitors were being served cached pages that were already more than 10 minutes old, so even legitimate comments were being rejected.  Using PHP to generate my timestamps obviously was not going to work if I wanted to keep serving cached pages.</p>
<h2>JavaScript to the rescue</h2>
<p>Generating real-time timestamps on cached pages requires JavaScript.  But instead of a reliable server clock setting the timestamp, the time is coming from the visitor&#8217;s system, which can&#8217;t be trusted to be accurate.  Merely changing the comment form to use JavaScript to generate the first timestamp wouldn&#8217;t work, because verifying a timestamp generated on the client-side against one generated with a server-side language would be disastrous.</p>
<p>Replacing the PHP-generated timestamps with JavaScript-generated timestamps would require substantial changes to the system.</p>
<p>Traditional client-side form validation using JavaScript happens when the form is submitted.  If the validation fails, the form is not submitted, and the visitor typically gets an alert with suggestions on how to make the form acceptable.  If the validation passes, the form submission continues without bothering the visitor.  To get our two timestamps, we can generate a first timestamp when the page loads and compare it to a second timestamp generated when the form is submitted.  If the visitor submits the form too quickly, we can display an alert showing the number of seconds remaining until the form can be successfully submitted.  This should hopefully be invisible to most visitors who choose to leave comments, but at the very least, far less irritating than a CAPTCHA system.</p>
<p>It took me two tries to get it right, but I&#8217;m going to discuss the less successful method first to point out its flaws.</p>
<h3>Method One (not good enough)</h3>
<p>Here&#8217;s how the original system flowed.</p>
<ol>
<li>Generate a first JS timestamp when the page is loaded.</li>
<li>Generate a second JS timestamp when the form is submitted.</li>
<li>Before the form is submitted, compare the two, and if enough time has passed, write a pre-determined passcode to a hidden INPUT element, then submit the form.</li>
<li>On the form processing page, use server-side logic to verify that the passcode is present and valid.</li>
</ol>
<p>The problem was that it seemed that certain bots could parse JavaScript enough to drop the pre-determined passcode into the hidden form field before submitting the form, circumventing the timestamps completely and defeating the system.  </p>
<p>It also failed to adhere to one of the basic tenants of form validation &#8211; that the input must be checked on both the client-side and the server-side.</p>
<h3>Method Two (better)</h3>
<p>Rather than having the server-side validation be merely a check to confirm that the passcode is present, method two goes back to comparing the timestamps a second time on the server side. Instead of a single hidden input, we now have two &#8211; one for each timestamp.  This is intended to prevent a bot from figuring out the ultimate validation mechanism by simply parsing the JavaScript.  Finally, the hidden fields are added to the form via jQuery, which makes it easier to implement and may act as another layer of obfuscation. </p>
<ol>
<li>Generate a first JS timestamp when the page is loaded and write it to a hidden form field.</li>
<li>Generate a second JS timestamp when the form is submitted and write it to a hidden form field.</li>
<li>Before the form is submitted, compare the two, and if enough time has passed, submit the form (client-side validation).</li>
<li>On the form processing page, use server-side logic to compare the timestamps a second time (server-side validation).</li>
</ol>
<p>The timestamp handshake works more like it did in the server-side-only method.  We still have to pass something from the comment form to the processing script, but it&#8217;s not too obvious from the HTML what is being done with it.</p>
<h2>The same downside plagues me</h2>
<p>Unfortunately, if we want to have any server-side validation at all, and we do, the core file <strong>wp-comments-post.php</strong> will still have to be modified.  In my experience, the system is not sufficiently effective using just client-side validation.  </p>
<h2>The code</h2>
<p>Two files must be modified to implement the validation.</p>
<p><strong>File 1: The theme&#8217;s comments.php file (older themes) or wp-includes\comment-template.php (newer themes)</strong></p>
<p>Your comment form lives somewhere.  My theme is based on Kubrick, the old default WordPress theme, and my comment form is in my theme folder, in a file named <strong>comments.php</strong>.  If your theme is newer and based on the current default theme, twentyeleven, the form is in <strong>wp-includes\comment-template.php</strong>.  If your theme isn&#8217;t based on either of these, all bets are off.  I know it&#8217;s confusing.  Sorry.</p>
<p>Add the JavaScript that creates and populates the timestamp fields.  Be sure to confirm that your comment form has an ID of <strong>commentform</strong>.  I&#8217;m using jQuery to help fire functions when the page loads.</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){
	ardGenTS1();
});

function ardGenTS1() {
	// prepare the form
	$('#commentform').append('&lt;input type=&quot;hidden&quot; name=&quot;ardTS1&quot; id=&quot;ardTS1&quot; value=&quot;1&quot; /&gt;');
	$('#commentform').append('&lt;input type=&quot;hidden&quot; name=&quot;ardTS2&quot; id=&quot;ardTS2&quot; value=&quot;1&quot; /&gt;');
	$('#commentform').attr('onsubmit', 'return validate()');
	// set a first timestamp when the page loads
	var ardTS1 = (new Date).getTime();
	document.getElementById(&quot;ardTS1&quot;).value = ardTS1;
}

function validate() {
	// read the first timestamp
	var ardTS1 = document.getElementById(&quot;ardTS1&quot;).value;
//	alert ('ardTS1: ' + ardTS1);
	// generate the second timestamp
	var ardTS2 = (new Date).getTime();
	document.getElementById(&quot;ardTS2&quot;).value = ardTS2;
//	alert ('ardTS2: ' + document.getElementById(&quot;ardTS2&quot;).value);
	// find the difference
	var diff = ardTS2 - ardTS1;
	var elapsed = Math.round(diff / 1000);
	var remaining = 10 - elapsed;
//	alert ('diff: ' + diff + '\n\nelapsed:' + elapsed);
	// check whether enough time has elapsed
	if (diff &gt; 10000) {
		// submit the form
		return true;
	}else{
		// display an alert if the form is submitted within 10 seconds
		alert(&quot;This site is protected by an anti-spam feature that requires 10 seconds to have elapsed between the page load and the form submission.\n\nPlease close this alert window.  The form may be resubmitted successfully in &quot; + remaining + &quot; seconds.&quot;);
		// prevent the form from being submitted
		return false;
	}
}
&lt;/script&gt;
</pre>
<p><strong>File 2: The wp-comments-post.php file</strong></p>
<p>The wp-comments-post.php file lives in the root of WordPress and handles the form processing.  We need to add a few lines that check the contents of our new validation input field.</p>
<p>Somewhere after line 53 or so (where <em>$comment_content</em> is defined), insert the following code.</p>
<pre class="brush: php; title: ; notranslate">
$ardTS1 = ( isset($_POST['ardTS1']) ) ? trim($_POST['ardTS1']) : 1;
$ardTS2 = ( isset($_POST['ardTS2']) ) ? trim($_POST['ardTS2']) : 2;
$ardTS = $ardTS2 - $ardTS1;

if ( $ardTS &lt; 10000 ) {
// If the difference of the timestamps is not more than 10 seconds, exit
    wp_die( __('&lt;strong&gt;ERROR&lt;/strong&gt;:  This site uses JavaScript validation to reduce comment spam.  Either your browser has JavaScript disabled, or the comment was not legitimately submitted.') );
}
</pre>
<p>That&#8217;s it.  Not so bad, right?</p>
<h2>Final thoughts</h2>
<p>One advantage to this method over the old PHP-only method is that even if the core file is replaced and the server-side validation is lost, the client-side validation continues to work, perhaps providing some measure of protection.</p>
<p>The method is safe for use with caching systems that create purely static, HTML pages.  I&#8217;ll follow up later and report on how effective it seems to be at stopping spam comments before they get to Akismet (and into the WordPress database).</p>
<p>Now, for a little extra protection, you can rename the <strong>wp-comments-post.php</strong> file and change the path in the comment form&#8217;s action attribute.  I&#8217;ve <a href="http://www.ardamis.com/2010/08/09/reducing-wordpress-spam-comments/">posted logs</a> showing that some bots just try to post spam directly to the <strong>wp-comments-post.php</strong> file, so renaming that file is an easy way to cut down on spam.  Just remember to come back and delete the <strong>wp-comments-post.php</strong> file each time you update WordPress.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/08/27/a-cache-proof-method-for-reducing-comment-spam/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OOP PHP Notes</title>
		<link>http://www.ardamis.com/2011/07/30/oop-php-notes/</link>
		<comments>http://www.ardamis.com/2011/07/30/oop-php-notes/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 18:56:43 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Nonsense]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1565</guid>
		<description><![CDATA[My notes on learning Object Oriented Programming in PHP.]]></description>
			<content:encoded><![CDATA[<p><strong>Overview of classes and objects</strong></p>
<p>Objects are the building blocks of the application (ie: the workers in a factory)<br />
Classes can be thought of as blueprints for the objects.  Classes describe the objects, which are created in memory.<br />
So, the programmer writes the classes and the PHP interpreter creates the objects from the classes.</p>
<p>A class may contain both variables and functions.<br />
A variable inside a class is called a <em>property</em>.<br />
A function inside a class is called a <em>method</em>.</p>
<p><strong>Instantiation</strong></p>
<p>To create an object, you instantiate a class (you create an instance of the class as an object).<br />
For example, if we have a class named &#8216;person&#8217; and want to instantiate it as the variable $oliver:</p>
<pre class="brush: php; title: ; notranslate">$oliver = new person();</pre>
<p>The variable $oliver is referred to as the &#8216;handle&#8217;.</p>
<p><strong>Accessing properties and methods</strong></p>
<p>To access the properties and methods of a class, we use the object&#8217;s handle, followed by the arrow operator &#8220;-&gt;&#8221;.<br />
For example, if our class has a method &#8216;get_name&#8217;, we can echo that to the page with:</p>
<pre class="brush: php; title: ; notranslate">echo $oliver-&gt;get_name();</pre>
<p>Note that there are no single or double quotes used in instantiating a class or accessing properties and methods of a class.</p>
<p><strong>Constructors</strong></p>
<p>A class may have a special method called a constructor.  The constructor method is called automatically when the object is instantiated.<br />
The constructor method begins with two underscores and the word &#8216;construct&#8217;:</p>
<pre class="brush: php; title: ; notranslate">function __construct($variable) { }</pre>
<p>One can pass values to the constructor method by providing arguments after the class name.<br />
For example, to pass the name &#8220;John Doe&#8221; to the constructor method in the &#8216;person&#8217; class:</p>
<pre class="brush: php; title: ; notranslate">$john = new person(&quot;John Doe&quot;);</pre>
<p><span style="color:#F00">!</span> If a constructor exists and expects arguments, you must instantiate the class with the arguments expected by the constructor.</p>
<p><strong>Access modifiers and visibility declarations</strong></p>
<p>Properties must, and methods may, have one of three access modifiers (visibility declarations): public, protected, and private.<br />
Public: can be accessed from outside the class, eg: $myclass->secret_variable;<br />
Protected: can be accessed within the class and by classes derived from the class<br />
Private: can be accessed only within the class</p>
<p>Declaring a property with var makes the property public.</p>
<p>Methods declared without an explicit access modifier are considered public.</p>
<p><span style="color:#F00">!</span> If you call a protected method from outside the class, any PHP output before the call is still processed, but you get an error message when the interpreter gets to that call:</p>
<pre class="brush: plain; title: ; notranslate">Fatal error: Call to protected method...</pre>
<p><strong>Inheritance</strong></p>
<p>Inheritance allows a child class to be created from a parent class, whereby the child has all of the public and protected properties and methods of the parent.</p>
<p>A child class <em>extends</em> a parent class:</p>
<pre class="brush: php; title: ; notranslate">class employee extends person {
}</pre>
<p>A child class can redefine/override/replace a method in the parent class by reusing the method name.</p>
<p><span style="color:#F00">!</span>  A child class&#8217;s method&#8217;s access modifier can not be more restrictive than that of the parent class.  For example, if the parent class has a public set_name() method and the child class&#8217;s set_name() method is protected, the class itself will generate a fatal error, and no prior PHP output will be rendered. (In the error below, <em>employee</em> is the child class to <em>person</em>):</p>
<pre class="brush: plain; title: ; notranslate">Fatal error: Access level to employee::set_name() must be public (as in class person) in E:\xampp\htdocs\tester\oop\class_lib.php on line 38</pre>
<p>To differentiate between a method in a parent class vs the method as redefined in a child class, one must specifically name the class that contains the method you want to call using the scope resolution operator (::):</p>
<pre class="brush: php; title: ; notranslate">person::set_name($new_name);</pre>
<p>The scope resolution operator allows access to static, constant, and overridden properties or methods of a class, generally, a parent class. This would be done inside the child class, after redefining a parent&#8217;s method of the same name.</p>
<p>It&#8217;s also possible to use &#8216;parent&#8217; to refer to the child&#8217;s parent class:</p>
<pre class="brush: php; title: ; notranslate">parent::set_name($new_name);</pre>
<p>(I&#8217;m still a bit vague on this and am looking for examples of situations in which this would be used.)</p>
<p><strong>Classes inside classes</strong></p>
<p>Just as it&#8217;s possible to instantiate a class and use the object in a view file, it&#8217;s possible to instantiate an object and call its methods from inside another class.</p>
<p><strong>Static properties and methods</strong></p>
<p>Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).</p>
<p><strong>Resources</strong><br />
<a href="http://us2.php.net/manual/en/language.oop5.php" title="http://us2.php.net/manual/en/language.oop5.php">http://us2.php.net/manual/en/language.oop5.php</a><br />
<a href="http://net.tutsplus.com/tutorials/php/oop-in-php/" title="http://net.tutsplus.com/tutorials/php/oop-in-php/">http://net.tutsplus.com/tutorials/php/oop-in-php/</a><br />
<a href="http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect" title="http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect">http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect</a><br />
<a href="http://www.killerphp.com/tutorials/object-oriented-php/" title="http://www.killerphp.com/tutorials/object-oriented-php/">http://www.killerphp.com/tutorials/object-oriented-php/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/07/30/oop-php-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixed: XAMPP Component Status Check failure [3].</title>
		<link>http://www.ardamis.com/2011/07/10/xampp-component-status-check-failure-3/</link>
		<comments>http://www.ardamis.com/2011/07/10/xampp-component-status-check-failure-3/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 18:35:21 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[XAMPP]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1603</guid>
		<description><![CDATA[A simple fix for the XAMPP Component Status Check failure [3]. error when launching the XAMPP 1.7.4 Control Panel.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m running XAMPP 1.7.4 [PHP: 5.3.5] (not as a service) on 64-bit Windows 7 Professional.  </p>
<p>I installed XAMPP to E:\xampp, and I have pinned the XAMPP Control Panel (xampp-control.exe) to the taskbar for easier access, but starting up xampp-control.exe from that shortcut throws an error:</p>
<blockquote><p><strong>XAMPP Control</strong></p>
<p>XAMPP Component Status Check failure [3].</p>
<p>Current directory: E:\xampp</p>
<p>Run this program only from your XAMPP root directory.</p>
<p>[OK] [Cancel]</p></blockquote>
<p>Strangely enough, I even get this error even when running xampp-control.exe from my XAMPP root directory, which really is E:\xampp.</p>
<p>The last post in the thread at <a href="http://www.apachefriends.org/f/viewtopic.php?f=16&#038;t=44320&#038;sid=a41029c6a36bbf5b3bb5817f37842340&#038;start=60">http://www.apachefriends.org/f/viewtopic.php?f=16&#038;t=44320&#038;sid=a41029c6a36bbf5b3bb5817f37842340&#038;start=60</a> offers a simple solution:  change the Install_Dir value under HKEY_LOCAL_MACHINE to point to C:\xampp.  According to the thread, the error message is due to a bug where the Install_Dir is checked against a hard-coded path on C:\.  That may or may not be the case, but the suggested work-around seems to be effective.</p>
<p>Here&#8217;s a registry merge for Windows 7 64-bit that will make the change for you.</p>
<pre class="brush: plain; title: ; notranslate">
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\xampp]
&quot;Install_Dir&quot;=&quot;C:\\xampp&quot;
</pre>
<p>Now xampp-control.exe launches without the error, and I haven&#8217;t noticed anything (PHP, MySQL, etc.) not working because of the bogus path.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/07/10/xampp-component-status-check-failure-3/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Blogspot.com and Googlebot</title>
		<link>http://www.ardamis.com/2011/06/29/blogspot-googlebot/</link>
		<comments>http://www.ardamis.com/2011/06/29/blogspot-googlebot/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 02:44:51 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Nonsense]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1514</guid>
		<description><![CDATA[Just how quickly does Googlebot visit a page after it is linked to from a post on blogspot.com?]]></description>
			<content:encoded><![CDATA[<p><strong>Q:</strong> Just how quickly does Googlebot visit a page after it is linked to from a post on blogspot.com?<br />
<strong>A:</strong> Pretty darn quickly.</p>
<p>I am curious about just what data is being passed to pages by user-agents.  To try to gather some of this data, I created a test page at <a href="http://alephstudios.com/">Aleph Studios</a>.  This page records the keys and values of the $_GET, $_POST, and $_COOKIE arrays, along with the values that belong to the keys starting with HTTP_ from the $_SERVER array, and the value of $_SERVER['REQUEST_TIME'].  It bundles all this up into an email and sends it to me each time the page is accessed.</p>
<p>In order to get this page into Google, I <a href="http://aleph-studios.blogspot.com/2011/06/more-http-get-post-and-cookie-testing.html">posted a link to it</a> on my throwaway blog at <a href="http://aleph-studios.blogspot.com/">Aleph Studios on Blogger</a>.  The timestamp of that post is 9:13 pm.  The email from the page as triggered by <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=182072">Googlebot</a> is timestamped 9:14 pm.  That&#8217;s pretty slick.</p>
<p>The HTTP headers sent by Googlebot and recorded by the page are:</p>
<pre class="brush: plain; title: ; notranslate">
HTTP_ACCEPT = */*
HTTP_ACCEPT_ENCODING = gzip,deflate
HTTP_CONNECTION = Keep-alive
HTTP_FROM = googlebot(at)googlebot.com
HTTP_HOST = alephstudios.com
HTTP_USER_AGENT = Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
</pre>
<p>The page&#8217;s filename, title tag, and H1 tag are all &#8220;WvVdWfwgMcmypDqv7t&#8221;, so it&#8217;ll be easy to find in Google later.  As <a href="http://aleph-studios.blogspot.com/2010/07/time-for-some-testing.html">I&#8217;ve observed in the past</a>, within 20 minutes, the page on blogspot.com containing the string shows up in Google for a search on the string.</p>
<p>New pages don&#8217;t really take twenty minutes to show up in Google, but I don&#8217;t check Google very quickly after hitting the Publish button.  The fastest I&#8217;ve personally witnessed a new page on ardamis.com appearing in Google was under 4 minutes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/06/29/blogspot-googlebot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jealous of The Social Network</title>
		<link>http://www.ardamis.com/2011/06/12/jealous-of-the-social-network/</link>
		<comments>http://www.ardamis.com/2011/06/12/jealous-of-the-social-network/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 02:46:50 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Nonsense]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[downloads]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1467</guid>
		<description><![CDATA[A short list of my programming accomplishments.]]></description>
			<content:encoded><![CDATA[<p>So I finally watched <a href="http://en.wikipedia.org/wiki/The_Social_Network"><em>The Social Network</em></a> over the weekend, and it&#8217;s made me feel jealous and a bit guilty.</p>
<p>In a meager effort to console myself for so far failing to be a billionaire, I&#8217;m assembling the short list of web-application type things I&#8217;ve built here.</p>
<ol>
<li>A dice roller: <a href="http://alephstudios.com/rollforit/">rollforit</a>. Enter a name, create a room, invite your friends, and start rolling dice.  For people who want to play pen and paper, table-top RPG dice games with their distant friends.</li>
<li>A URL shortener: <a href="http://minifi.de/">Minifi.de</a>.  Minifi.de comes with an API and a bookmarklet.  It really works, too!  The <a href="http://minifi.de/technical.php">technical explanation</a> has more details.</li>
<li>A social networking site: <a href="http://alephstudios.com/snapbase/">Snapbase</a>.  Snapbase is a social site that shows you what&#8217;s going on in your city or anywhere in the world as pictures are uploaded by your friends and neighbors.  The application extracts location information from the EXIF data embedded in images and displays recent images taken near your present location.</li>
<li>A <a href="http://alephstudios.com/helpdesk/">trouble-ticketing system</a> for an IT help desk or technical support center.  It&#8217;s really pretty extensive, with asset management, user accounts, salted encrypted passwords, and all sorts of nifty things.  I really must write a full description of it at some point, but until then, the <a href="http://alephstudios.com/helpdesk/documentation/">documentation</a> is the next best thing.</li>
<li>An <a href="http://alephstudios.com/ias/">account-based invoice tracking and access system</a> for grouping invoices according to clients, then sharing invoice history with those clients and allowing them to easily pay outstanding invoices via Paypal.</li>
<li>An <a href="http://alephstudios.com/ias/">account-based invoice access system</a> where clients can view paid and unpaid invoices, and even easily pay an outstanding invoice via Paypal. I actually use this almost every day.</li>
<li>A simple method for <a href="http://www.ardamis.com/2008/06/11/protecting-a-download-using-a-unique-url/">protecting a download using a unique URL</a> that can be emailed to authorized users. The URL can be set to expire after a certain amount of time or any number of downloads.</li>
<li>An update to the above download protection script to <a href="http://www.ardamis.com/2009/06/26/protecting-multiple-downloads-using-unique-urls/">protect multiple downloads</a>, generate batches of keys, leave notes about who received the key, the ability to specify per-key the allowable number of downloads and age, and some basic reporting.</li>
<li>An HTML auction template generator called <a href="http://simpleauctionwizard.com/">Simple Auction Wizard</a>.  It helps you create HTML auction templates for eBay, and uses SWFUpload and tinyMCE.</li>
</ol>
<p>I have another project in the works that promises to be more financially viable, but the most clever thing on that list is Snapbase.  It&#8217;s in something akin to alpha right now; barely usable.  I really wish I had the time to pursue it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/06/12/jealous-of-the-social-network/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to fix the &#8220;PHP Fatal error:  Call to undefined function  get_header()&#8221; error in WordPress</title>
		<link>http://www.ardamis.com/2011/06/02/fix-for-php-fatal-error-get_header-in-wordpress/</link>
		<comments>http://www.ardamis.com/2011/06/02/fix-for-php-fatal-error-get_header-in-wordpress/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 17:07:13 +0000</pubDate>
		<dc:creator>ardamis</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Site Dev]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[500 error]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[troubleshooting]]></category>

		<guid isPermaLink="false">http://www.ardamis.com/?p=1430</guid>
		<description><![CDATA[Fix the "PHP Fatal error: Call to undefined function get_header()" error in WordPress.]]></description>
			<content:encoded><![CDATA[<p>While making changes to my WordPress theme, I noticed that the error_log file in my theme folder contained dozens of PHP Fatal error lines:</p>
<pre class="brush: plain; title: ; notranslate">
...
[01-Jun-2011 14:25:15] PHP Fatal error:  Call to undefined function  get_header() in /home/accountname/public_html/ardamis.com/wp-content/themes/ars/index.php on line 7
[01-Jun-2011 20:58:23] PHP Fatal error:  Call to undefined function  get_header() in /home/accountname/public_html/ardamis.com/wp-content/themes/ars/index.php on line 7
...
</pre>
<p>The first seven lines of my theme&#8217;s index.php file:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php ini_set('display_errors', 0); ?&gt;
&lt;?php
/**
 * @package WordPress
 * @subpackage Ars_Theme
*/
get_header(); ?&gt;
</pre>
<p>I realized that the error was being generated each time that my theme&#8217;s index.php file was called directly, and that the error was caused by the theme&#8217;s inability to locate the WordPress <strong>get_header</strong> function (which is completely normal).  Thankfully, the descriptive error wasn&#8217;t being output to the browser, but was only being logged to the error_log file, due to the inclusion of the <strong>ini_set(&#8216;display_errors&#8217;, 0);</strong> line.  I had learned this the hard way a few months ago when I found that calling the theme&#8217;s index.php file directly would generate an error message, output to the browser, that would reveal my hosting account username as part of the absolute path to the file throwing the error.</p>
<p>I decided the best way to handle this would be to check to see if the file could find the <strong>get_header</strong> function, and if it could not, simply redirect the visitor to the site&#8217;s home page.  The code I used to do this:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php ini_set('display_errors', 0); ?&gt;
&lt;?php
/**
* @package WordPress
* @subpackage Ars_Theme
*/
if (function_exists('get_header')) {
	get_header();
}else{
    /* Redirect browser */
    header(&quot;Location: http://&quot; . $_SERVER['HTTP_HOST'] . &quot;&quot;);
    /* Make sure that code below does not get executed when we redirect. */
    exit;
}; ?&gt;
</pre>
<p>So there you have it.  No more fatal errors due to <strong>get_header</strong> when loading the WordPress theme&#8217;s index.php file directly.  And if something else in the file should throw an error, <strong>ini_set(&#8216;display_errors&#8217;, 0);</strong> means it still won&#8217;t be sent to the browser.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ardamis.com/2011/06/02/fix-for-php-fatal-error-get_header-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

