<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Adam Patterson - Edmonton Web Design and Development &#187; Tutorials</title>
	<atom:link href="http://www.adampatterson.ca/blog/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adampatterson.ca</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 31 Jan 2012 02:53:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Regular Expression for URLs using ereg_replace</title>
		<link>http://www.adampatterson.ca/blog/2011/02/regular-expression-for-url-using-ereg_replace/</link>
		<comments>http://www.adampatterson.ca/blog/2011/02/regular-expression-for-url-using-ereg_replace/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 13:01:40 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ereg_replace]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Post]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/?p=1379</guid>
		<description><![CDATA[Ever needed a Regular Expression to parse URLs in a body of text. The beauty with this expression is its flexibility, it will handle a http or https url with or without www and it&#8217;s fine with tinyurls. $subject will be the body of text containing flat text urls. $text will now contain our information. [...]]]></description>
			<content:encoded><![CDATA[<p>Ever needed a <a href="http://en.wikipedia.org/wiki/Regular_expression">Regular Expression</a> to parse URLs in a body of text. The beauty with this expression is its flexibility, it will handle a http or https url with or without www and it&#8217;s fine with tinyurls.</p>
<pre class="brush: php; title: ; notranslate">
// $message contains a body of text with non html URLs
$text = ereg_replace(&quot;[[:alpha:]]+://[^&lt;&gt;[:space:]]+[[:alnum:]/]&quot;, &quot;&lt;a href=\&quot;\&#92;&#48;\&quot;&gt;\&#92;&#48;&lt;/a&gt;&quot;, $message);
</pre>
<p><strong>$subject</strong> will be the body of text containing flat text urls. <strong>$text</strong> will now contain our information.</p>
<pre class="brush: php; title: ; notranslate">
// Output New formatted content with links.
echo $text;
</pre>
<p>Now our content is parsed with URLs.</p>
<p>Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2011/02/regular-expression-for-url-using-ereg_replace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;s Memory Usage</title>
		<link>http://www.adampatterson.ca/blog/2010/12/phps-memory-usage/</link>
		<comments>http://www.adampatterson.ca/blog/2010/12/phps-memory-usage/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 12:23:04 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[Post]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/?p=1367</guid>
		<description><![CDATA[This is a handy little Snippet that goes well with PHP Microtime. Not only are we concerned with our scripts execution time but we also want to make sure we are not using an exorbitant amount of memory in the process. At the top of your script lets place the code that will start PHP&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>This is a handy little Snippet that goes well with <a href="http://www.adampatterson.ca/blog/2010/09/php-microtime/" target="_blank">PHP Microtime</a>. Not only are we concerned with our scripts execution time but we also want to make sure we are not using an exorbitant amount of memory in the process.</p>
<p>At the top of your script lets place the code that will start PHP&#8217;s Micro time.</p>
<pre class="brush: php; title: ; notranslate">
$time_start = microtime(true);
</pre>
<p>At the bottom of your page, just before the &#8220;</body>&#8221; tag we will put the Code that will calculate the execution time as well as how much memory was used to execute the script.</p>
<pre class="brush: php; title: ; notranslate">
// RENDER TIME
$time_end = microtime(true);
$time = $time_end - $time_start;

echo &amp;quot;Loaded in \n&amp;quot;;
echo round($time, 3).&amp;quot; Seconds &amp;lt;br /&amp;gt;&amp;quot;;
echo 'Peak: ' . number_format(memory_get_peak_usage(), 0, '.', ',') . &amp;quot; bytes\n &amp;lt;br /&amp;gt;&amp;quot;;
echo 'End: ' . number_format(memory_get_usage(), 0, '.', ',') . &amp;quot; bytes\n&amp;quot;;
</pre>
<p><a href="http://ca.php.net/number_format" target="_blank">number_format()</a> simply formats the number out put to look something like this &#8220;111,964&#8243; instead of &#8220;111964&#8243;.</p>
<p><a href="http://ca3.php.net/manual/en/function.memory-get-peak-usage.php" target="_blank">memory_get_peak_usage()</a> is going to return a value based on the peak memory used during the execution. This is more so were we want to see improvements made.</p>
<p><a href="http://ca3.php.net/memory_get_usage" target="_blank">memory_get_usage()</a> will return the amount of memory used once the script is done processing, basically the amount currently used once the crunching is finished.</p>
<p>It&#8217;s a good idea to think about memory usage when you are making web applications as this can impact your hosting.</p>
<p>Say you have a VPS server with 256mb of ram but the foot print of each user is 25mb you can can only have about 10 people accessing your site before you running server errors exceeding memory limits. Any work involving processing of images a lot of data manipulation can eat up memory in a hurry.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/12/phps-memory-usage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Work with RSS feeds using PHP and cURL</title>
		<link>http://www.adampatterson.ca/blog/2010/11/php-and-curl/</link>
		<comments>http://www.adampatterson.ca/blog/2010/11/php-and-curl/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 20:15:10 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[HelpSpot]]></category>
		<category><![CDATA[Post]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Unfuddle]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/?p=1351</guid>
		<description><![CDATA[cURL or Client URL Library is a very powerful tool, and its something that i recently had to use while working with two APIs one for Unfuddle and one for HelpSpot. PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many [...]]]></description>
			<content:encoded><![CDATA[<p>cURL or Client URL Library is a very powerful tool, and its something that i recently had to use while working with two APIs one for <a href="http://unfuddle.com/">Unfuddle</a> and one for <a href="http://www.userscape.com/products/helpspot/">HelpSpot</a>.</p>
<blockquote><p>PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP&#8217;s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.</p>
<p>These functions have been added in PHP 4.0.2. </p></blockquote>
<p>You can interact with most APIs using cURL, at first it seams intimidating but once you start to use it you become comfortable with the set up.</p>
<p>Lets get started by parsing an RSS feed.</p>
<p><span id="more-1351"></span></p>
<pre class="brush: php; title: ; notranslate">
	$ch = curl_init();
</pre>
<p><a href="http://ca3.php.net/curl_init">curl_init()</a> initializes a new session and return a cURL handle for use with the <a href="http://ca3.php.net/manual/en/function.curl-setopt.php">curl_setopt()</a>, <a href="http://ca3.php.net/manual/en/function.curl-exec.php">curl_exec()</a>, and <a href="http://ca3.php.net/manual/en/function.curl-close.php">curl_close()</a> functions. We set $ch with the curl_init() function to get things rolling.</p>
<pre class="brush: php; title: ; notranslate">
	curl_setopt($ch, CURLOPT_URL, $feed);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
</pre>
<p>Just like the name says, curl_setopt() will set an option on the given cURL session handle. There are far to many to list so you are bette roff reading the manual found <a href="http://ca3.php.net/manual/en/function.curl-setopt.php">here</a>. In our example we are going to need to access a URL and bring back the data.</p>
<p><strong>CURLOPT_URL</strong> &#8211; This will be the location of the RSS file.<br />
<strong>CURLOPT_HEADER</strong> &#8211; We set this to 0 or false as we do not want to see the header information.<br />
<strong>CURLOPT_RETURNTRANSFER</strong> &#8211; This will transfer the data back as a string instead of outputting directly to the browser.<br />
<strong>CURLOPT_USERAGENT</strong> &#8211; While this is not needed, The contents of the &#8220;User-Agent: &#8221; header to be used in a HTTP request.</p>
<pre class="brush: php; title: ; notranslate">
	$rss = curl_exec($ch);
	curl_close($ch);
</pre>
<p>Once we have our options set the next step is to execute them using curl_exec(), we will asign $rss to curl_exec() thus giving the variable the contents of our look up.</p>
<p>For good measure we close the connection using the curl_close() function.</p>
<p>Since we know RSS feeds are made of XML we can goa head and do some simple parsing of my XML.</p>
<pre class="brush: php; title: ; notranslate">
	// Manipulate string into object
	$rss = simplexml_load_string($rss);
</pre>
<p><a href="http://ca3.php.net/simplexml_load_string">simplexml_load_string()</a> is available in PHP 5.1.0 and on and will take a well-formed XML string and returns it as an object. This makes it easy for us to work with.</p>
<p>I have talked about simplexml_load_string() in <a href="http://www.adampatterson.ca/blog/2010/09/google-http-geocoding/">Google HTTP Geocoding</a>.</p>
<p>Since my goal was to introduce you to cURL i&#8217;m not going to go into much detail with the XML parsing. we have set an array on the variable $rss, i usually use Print_r($rss) to see the structure of the array before i work with it, this makes it easy to work with.</p>
<pre class="brush: php; title: ; notranslate">
	$cnt = count($rss-&gt;channel-&gt;item);

	for($i=0; $i&lt;$cnt; $i++)
	{
		$url = $rss-&gt;channel-&gt;item[$i]-&gt;link;
		$title = $rss-&gt;channel-&gt;item[$i]-&gt;title;
		$desc = $rss-&gt;channel-&gt;item[$i]-&gt;description;
		echo '&lt;h3&gt;&lt;a href=&quot;'.$url.'&quot;&gt;'.$title.'&lt;/a&gt;&lt;/h3&gt;'.$desc.'';
	}
</pre>
<p>The above code is counting the number of items and looping through them one at a time printing the data to the screen, not real magic here.</p>
<p>Below is the complete source code for your coding pleasure.</p>
<pre class="brush: php; title: ; notranslate">
Function feedMe($feed) {
	// Use cURL to fetch text
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $feed);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
	$rss = curl_exec($ch);
	curl_close($ch);

	// Manipulate string into object
	$rss = simplexml_load_string($rss);

	$siteTitle = $rss-&gt;channel-&gt;title;
	echo &quot;&lt;h1&gt;&quot;.$siteTitle.&quot;&lt;/h1&gt;&quot;;
	echo &quot;&lt;hr /&gt;&quot;;

	$cnt = count($rss-&gt;channel-&gt;item);

	for($i=0; $i&lt;$cnt; $i++)
	{
		$url = $rss-&gt;channel-&gt;item[$i]-&gt;link;
		$title = $rss-&gt;channel-&gt;item[$i]-&gt;title;
		$desc = $rss-&gt;channel-&gt;item[$i]-&gt;description;
		echo '&lt;h3&gt;&lt;a href=&quot;'.$url.'&quot;&gt;'.$title.'&lt;/a&gt;&lt;/h3&gt;'.$desc.'';
	}
}

feedMe(&quot;http://feeds.feedburner.com/adampatterson/&quot;);
</pre>
<p>cURL is a powerful tool, you can interact with APIs as well as sites or services that don&#8217;t have an AIL like Google Analytics. So play with it, make a twitter feed parser.</p>
<p><strong>Further Reading:</strong><br />
<a href="http://ca.php.net/curl">http://ca.php.net/curl</a><br />
<a href="http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/">http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/11/php-and-curl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sequel Pro on Dreamhost</title>
		<link>http://www.adampatterson.ca/blog/2010/11/aptana-or-mysql-query-browser-on-a-dreamhost-database/</link>
		<comments>http://www.adampatterson.ca/blog/2010/11/aptana-or-mysql-query-browser-on-a-dreamhost-database/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 22:07:59 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[External]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Post]]></category>
		<category><![CDATA[Sequel Pro]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/?p=1922</guid>
		<description><![CDATA[If you are doing any work with a database you probably have used phpMyAdmin. It is an excellent tool, and for a lot of tasks I prefer to use it. But when it comes down to QA or debugging of data on an established database I like to use an application. For the Mac I [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.adampatterson.ca/wp-content/uploads/2010/11/sequelpro-logo.jpg" alt="" title="sequelpro-logo" width="412" height="71" /></p>
<p>If you are doing any work with a database you probably have used <a href="http://www.phpmyadmin.net/home_page/index.php" target="_blank">phpMyAdmin</a>. It is an excellent tool, and for a lot of tasks I prefer to use it.</p>
<p>But when it comes down to QA or debugging of data on an established database I like to use an application. For the Mac I use <a href="http://www.sequelpro.com/" target="_blank">Sequel Pro</a>. Don&#8217;t let the Pro fool you though, it is a free application but rely on <a href="http://www.sequelpro.com/donate.html" target="_blank">Donations</a>.</p>
<p>I would offer the MySQL Query Browser as a PC alternative but it has since been redeveloped into a workbench. I have not spent much time getting to know it so for now I will focus on the Mac. </p>
<p>Since I use <a href="http://www.dreamhost.com/r.cgi?507558" target="_blank">Dreamhost</a> I will lay out the steps needed to connect to your Database.</p>
<p>Other hosts might allow something similar but unless they offer an external connection like mysql.domain.com or by IP then you wont be able to use <a href="http://en.wikipedia.org/wiki/Localhost" target="_blank">localhost</a>.</p>
<p><strong>Setup your User:</strong></p>
<p>To gain access You need to add your external IP to the MySQL user by going into the <a href="https://panel.dreamhost.com/index.cgi?tree=goodies.mysql" target="_blank">MySQL Goodies</a>, Click on your database user name to get to the property&#8217;s. Under &#8220;Allowable Hosts&#8221; enter you IP address. It will also be listed beside the text box. But you can use <a href="http://www.ipchicken.com/">IP Chicken</a> if you are unsure.</p>
<p><strong>Open up Sequel Pro and fill in the blanks.</strong></p>
<p>When you open up Sequel Pro make sure that you have a Standard connection selected. Give the connection a name. enter your Dreamhost mysql address, User-name, Password, leaving the port as default (3306).</p>
<p>Connect and everything should work, you can now select a database from the drop down on the left. If you get a connection error then check your log in credentials, make sure your External IP is correct, you may also need to wait a few min for Dreamhost to add your IP to their system.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/11/aptana-or-mysql-query-browser-on-a-dreamhost-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Php Microtime</title>
		<link>http://www.adampatterson.ca/blog/2010/09/php-microtime/</link>
		<comments>http://www.adampatterson.ca/blog/2010/09/php-microtime/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 14:09:42 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Microtime]]></category>
		<category><![CDATA[Post]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/2007/03/21/php-microtime/</guid>
		<description><![CDATA[This is a handy bit of code that will help when optimizing your script. Most MVC or NonMVC frameworks have a benchmark feature but if you are on your own this will do just fine. The PHP microtime() function will return the current Unix timestamp. There isn&#8217;t much to this script, we are going to [...]]]></description>
			<content:encoded><![CDATA[<p>This is a handy bit of code that will help when optimizing your script.  Most MVC or NonMVC frameworks have a benchmark feature but if you are on your own this will do just fine.</p>
<p>The PHP <code><a href="http://php.net/manual/en/function.microtime.php" target="_blank">microtime()</a></code> function will return the current <a href="http://en.wikipedia.org/wiki/Unix_time" target="_blank">Unix timestamp</a>.</p>
<p>There isn&#8217;t much to this script, we are going to assign <code>$time-start</code> the start time.  Then below your code to time place the <code>$time_end</code>.  </p>
<p>A little simple math will tell us the difference. <code>$time-start</code> minus <code>$time-end</code> will give us the execution time.</p>
<p>If we were to use the raw result we would see something like this: 0.0562338829041 Seconds.</p>
<p>Its a bit to detailed do be of any use. We can use the PHP Function <code>round()</code> to round the number to 3 decimal places.</p>
<pre class="brush: php; title: ; notranslate">
$time_start = microtime(true);

// Code Here
for ( $counter = 1; $counter &amp;lt;= 1000000; $counter += 1) {
	$counter;
}

$time_end = microtime(true);
$time = $time_end - $time_start;

echo &amp;quot;Loaded in &amp;quot;.round($time, 3).&amp;quot; seconds\n&amp;quot;;
</pre>
<p>That&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/09/php-microtime/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Php User Login With Sessions</title>
		<link>http://www.adampatterson.ca/blog/2010/09/php-user-login-with-sessions/</link>
		<comments>http://www.adampatterson.ca/blog/2010/09/php-user-login-with-sessions/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 18:13:57 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[log in]]></category>
		<category><![CDATA[Post]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Sessions]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/2007/03/07/php-user-login-with-sessions/</guid>
		<description><![CDATA[This is a tutorial more or less on principles for sessions with PHP. It is more about the big picture, and meant as a stepping stone for further exploration. The principal behind a user log in is simple, Don&#8217;t show private or secured information to non-members. By using sessions you can: Secure content from others. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.adampatterson.ca/wp-content/uploads/2010/09/4227370910_4cd8dfbea1_z-620x297.jpg" alt="Padlock"  width="620" height="297" class="full-image" /></p>
<p>This is a tutorial more or less on principles for sessions with PHP.  It is more about the big picture, and meant as a stepping stone for further exploration.</p>
<p>The principal behind a user log in is simple,  Don&#8217;t show private or secured information to non-members.</p>
<p>By using sessions you can:</p>
<ul>
<li>Secure content from others.</li>
<li>Show custom content to members.</li>
<li>Remember user settings</li>
<li>And much more!</li>
</ul>
<h2>What we will do</h2>
<ol>
<li>First we will need to log in to the site,</li>
<li>Check to see if the log in is correct,</li>
<li>If the log in was correct then we will allow access to our site, otherwise we will deny access,</li>
<li>If the user leaves the site after a successful log in then we will allow access again without prompting for log in again.</li>
</ol>
<h2>Lets get started</h2>
<p>I will cover the basic elements and supply my working source with this post so don&#8217;t worry if if you are missing some surrounding code that might make sense.</p>
<p>To start off we need to create a simple database. Something simple enough to hold a user name along with a password.</p>
<p><span id="more-230"></span></p>
<pre class="brush: sql; title: ; notranslate">
CREATE TABLE IF NOT EXISTS `members` (
  `id` int(4) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL default '',
  `password` varchar(65) NOT NULL default '',
  PRIMARY KEY  (`id`)
);
</pre>
<p>Now that the database has been created you need to have a user name and password to test against. Start off by inserting the user name and password &#8220;<strong>admin</strong>&#8220;.  Make sure the <strong>password</strong> is inserted as <strong>SHA1</strong>. If its easier use the following SQL in your PHPMyAdmin.</p>
<pre class="brush: sql; title: ; notranslate">
INSERT INTO `members` VALUES(1, 'admin', 'd033e22ae348aeb5660fc2140aec35850c4da997');
</pre>
<p>Now that we have some credentials in the database we need a way to login.</p>
<pre class="brush: php; title: ; notranslate">
&lt;form name=&quot;form&quot; method=&quot;post&quot; action=&quot;index.php?action=check&quot;&gt;
	&lt;h4&gt;Member Login:&lt;/h4&gt;

	&lt;p&gt;&lt;label for=&quot;username&quot;&gt;Username:&lt;/label&gt;
	&lt;input name=&quot;username&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;

	&lt;p&gt;&lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
	&lt;input name=&quot;password&quot; type=&quot;password&quot; /&gt;&lt;/p&gt;

	&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Login&quot;/&gt;
&lt;/form&gt;
</pre>
<p>Every page that will use sessions needs to have <a href="http://php.net/manual/en/function.session-start.php">session_start();</a> at the top of the page.</p>
<p>The form will post to another chunk of code that is in charge of checking weather or not the login was correct.</p>
<p>Because the password has been encrypted using SHA1 we have to encrypt the users input using he same method.</p>
<pre class="brush: php; title: ; notranslate">
// username and password sent from form
		$username=$_POST['username'];
		$password=$_POST['password'];

		$clean_username = strip_tags(stripslashes(mysql_real_escape_string($username)));
		$clean_password = sha1(strip_tags(stripslashes(mysql_real_escape_string($password))));

		$sql=&quot;SELECT * FROM members WHERE username='$clean_username' and password='$clean_password'&quot;;
		$rs = mysql_query($sql) or die (&quot;Query failed&quot;);

		$numofrows = mysql_num_rows($rs);

		if($numofrows==1){
			session_register(&quot;username&quot;);
			header(&quot;location:index.php?action=yes&quot;);
		}
		else {
			header(&quot;location:index.php?action=no&quot;);
		}
</pre>
<p>Using the <a href="http://php.net/manual/en/reserved.variables.post.php" target="_blank">$_post[]</a> array we will capture and assign our user name and password to matching variables. It is a good idea to do some cleaning to these variables. It is common for login scripts to bear the brunt of any exploits. Using <a href="http://php.net/manual/en/function.strip-tags.php" target="_blank">strip_tags()</a> <a href="http://php.net/manual/en/function.stripslashes.php" target="_blank">stripslashes()</a> <a href="http://php.net/manual/en/function.mysql-real-escape-string.php" target="_blank">mysql_real_escape_string()</a> will help protect against these threats.</p>
<p>Our password must be encoded using <a href="http://php.net/manual/en/function.sha1.php" target="_blank">sha1()</a>.  We will now compare the encrypted values with each other.</p>
<p>The SQL statement will return the results. Because the user name will be unique and the password encrypted we should be returned one row. This is important to know. If the user name existed more than once it is possible for the password to also exist more than once. This would return 2 and cause an error. In this case none of those users would gain access.</p>
<p>If we logged in correctly then we will set the session using <code>session_register("username");</code> and redirect to the secured content. If not we will deny access and send the user back to the login screen.</p>
<p>We could at this point set other session variables like expiry time, but that&#8217;s a little outside the scope of this basic example.</p>
<p>It is time to update the default login screen. Add the following code above the long in screen. We have set the session variable user name. so we ill see if it has been set, if it has then redirect to the secure content bypassing the login screen.</p>
<pre class="brush: php; title: ; notranslate">
if(isset($_SESSION['username'])){
	header(&quot;location:index.php?action=yes&quot;);
}
</pre>
<p>Every secure page should have the following code testing to see if the session variable has been set.</p>
<pre class="brush: php; title: ; notranslate">
if(!isset($_SESSION['username'])){
    header(&quot;location:index.php?action=no&quot;);
}
</pre>
<p>Using the <a href="http://php.net/manual/en/function.isset.php" target="_blank">!isset()</a> we will only act if the session variable has not been set.</p>
<p><strong>Whats next?</strong></p>
<p>We can now log in, check if the log in was successful acting accordingly. We have also secured our content in case that a non registered user or user who has logged out can not gain access to the secured information.</p>
<p>The last thing to do is allow the user to log out.&#8217;</p>
<pre class="brush: php; title: ; notranslate">
session_destroy();
header(&quot;location:index.php&quot;);
</pre>
<p>Now the session has been destroyed the user will be redirected to the main log in page.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?
session_start();
//DB Connection information
$dbname=&quot;php_sessions&quot;;
$dbuser=&quot;root&quot;;
$dbpwd=&quot;&quot;;
$host=&quot;localhost&quot;;

// Connect to the database
$cid = mysql_connect($host,$dbuser,$dbpwd);
if (!$cid) { print &quot;ERROR: &quot; . mysql_error() . &quot;\n&quot;;    }
mysql_select_db(&quot;$dbname&quot;) or die(mysql_error());

switch ( $_GET['action'] ) {
	case &quot;logout&quot;:
		session_destroy();
		header(&quot;location:index.php&quot;);
	break;
	case &quot;no&quot;:
        echo '&lt;h2&gt;You &lt;strong&gt;NOT&lt;/strong&gt; loged in.&lt;/h2&gt;';
	break;
	case &quot;yes&quot;:
		if(!isset($_SESSION['username'])){
			header(&quot;location:index.php&quot;);
		}
		echo '&lt;h2&gt;You &lt;strong&gt;ARE&lt;/strong&gt; loged in.&lt;/h2&gt;';
	break;
	case &quot;check&quot;: 

		$username=$_POST['username'];
		$password=$_POST['password'];

		$clean_username = strip_tags(stripslashes(mysql_real_escape_string($username)));
		$clean_password = sha1(strip_tags(stripslashes(mysql_real_escape_string($password))));

		$sql=&quot;SELECT * FROM members WHERE username='$clean_username' and password='$clean_password'&quot;;
		$rs = mysql_query($sql) or die (&quot;Query failed&quot;);

		$numofrows = mysql_num_rows($rs);

		if($numofrows==1){
			session_register(&quot;username&quot;);
			header(&quot;location:index.php?action=yes&quot;);
		}
		else {
			header(&quot;location:index.php?action=no&quot;);
		}
	default:
		if(isset($_SESSION['username'])){
			header(&quot;location:index.php?action=yes&quot;);
		}
		?&gt;
&lt;form name=&quot;form&quot; method=&quot;post&quot; action=&quot;index.php?action=check&quot;&gt;
	&lt;h4&gt;Member Login:&lt;/h4&gt;

	&lt;p&gt;&lt;label for=&quot;username&quot;&gt;Username:&lt;/label&gt;
	&lt;input name=&quot;username&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;

	&lt;p&gt;&lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
	&lt;input name=&quot;password&quot; type=&quot;password&quot; /&gt;&lt;/p&gt;

	&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Login&quot;/&gt;
&lt;/form&gt;
		&lt;?
	break;
    }
?&gt;
&lt;a href=&quot;index.php?action=login&quot;&gt;Log-in&lt;/a&gt; | &lt;a href=&quot;index.php?action=logout&quot;&gt;Log-out&lt;/a&gt;&lt;br /&gt;
 by &lt;a href=&quot;http://www.adampatterson.ca&quot; target=&quot;_blank&quot;&gt;adampatterson.ca&lt;/a&gt;
</pre>
<p><strong>Enjoy.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/09/php-user-login-with-sessions/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Php switch statement</title>
		<link>http://www.adampatterson.ca/blog/2010/09/php-switch-statement/</link>
		<comments>http://www.adampatterson.ca/blog/2010/09/php-switch-statement/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 15:03:04 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Post]]></category>

		<guid isPermaLink="false">http://studiolounge.net/news/2005/12/08/php-case-statement/</guid>
		<description><![CDATA[A switch statement is like a series of if statement. We are comparing a value with many other values. Pretend we have a script and want to greet our users. The above example is not the best use for a switch statement, but it gets the point across. In the past I have used them [...]]]></description>
			<content:encoded><![CDATA[<p>A switch statement is like a series of if statement.  We are comparing a value with many other values.</p>
<p>Pretend we have a script and want to greet our users.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

$user_name = 'adam';

switch( $user_name ) {
    case &quot;dave&quot;:
        echo 'Hello Dave.';
        break;
    case &quot;dan&quot;:
        echo 'Hello Dan.';
        break;
    case &quot;bill&quot;:
        echo 'Hello Bill.';
        break;
    case &quot;adam&quot;:
        echo 'Hello Adam.';
        break;
    default:
        echo 'Hello Guest';
        break;
    }
?&gt;
</pre>
<p>The above example is not the best use for a switch statement, but it gets the point across.</p>
<p>In the past I have used them to read a URL path for information or in functions for processing images based on the file extension.</p>
<p>Read more about the PHP <a href="http://php.net/manual/en/control-structures.switch.php" target="_blank">Switch statement</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/09/php-switch-statement/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP: Handy little Snippet for Multiline Text Box</title>
		<link>http://www.adampatterson.ca/blog/2010/09/php-handy-little-snippet-for-multiline-text-box/</link>
		<comments>http://www.adampatterson.ca/blog/2010/09/php-handy-little-snippet-for-multiline-text-box/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 12:56:33 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Explode]]></category>
		<category><![CDATA[Implode]]></category>
		<category><![CDATA[Post]]></category>
		<category><![CDATA[Slice]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/?p=1528</guid>
		<description><![CDATA[This is a quick and dirty little snippet. I had a case where I had to fill in a Subject Line and a message with only one multiline text box. It wasn&#8217;t practical to have a hard coded subject line as it would be the same for every message or in my case bug. Here [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick and dirty little snippet.  I had a case where I had to fill in a Subject Line and a message with only one multiline text box.  It wasn&#8217;t practical to have a hard coded subject line as it would be the same for every message or in my case bug.</p>
<p>Here is the full code.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?
// Get the message String
$rawString = $_POST['comments'];

// Split the string into pieces for processing
$pieces = explode(&quot;\n&quot;, $rawString);

// First element is the subject line
$subject = $pieces[0];

// Take the array, delete the first entry, So we can pass it to $message
$messagePieces = array_slice($pieces, 1);

// Replace the \n or add a &lt;br /&gt; if you like.
$message = implode(&quot;&lt;br /&gt;&quot;, $messagePieces);
echo &quot;Subject: &quot;. $subject;
echo &quot;&lt;br /&gt;&quot;;
echo &quot;Message: &quot;. $message;
?&gt;
&lt;form action=&quot;&lt;? echo $_SERVER['php_self'] ?&gt;&quot; method=&quot;post&quot;&gt;
	&lt;textarea id=&quot;comments&quot; name=&quot;comments&quot;&gt;Your message&lt;/textarea&gt;
	&lt;input name=&quot;send&quot; type=&quot;submit&quot; value=&quot;Send&quot; /&gt;
&lt;/form&gt;
</pre>
<p>Not a lot going on here.  I simply took the comments and assigned them to <code>$rawString</code>. The new line character is <code>\n</code> and are going to be on every new line (when you hit return) I can use the PHP Function <a href="http://ca2.php.net/explode">explode()</a> with our $rawString variable.</p>
<pre class="brush: php; title: ; notranslate">
$pieces = explode(&quot;\n&quot;, $rawString);
</pre>
<p>This will give us an array to work with. Our subject is now <strong>$subject = $pieces[0];</strong>.  Now we need to rebuild the message but we don&#8217;t want to loop through it printing each line separately.  We can use another PHP Function called <a href="http://ca.php.net/function.array-slice">array_slice()</a>, This will cut out the first row of the array, the Subject line.</p>
<pre class="brush: php; title: ; notranslate">
$messagePieces = array_slice($pieces, 1);
</pre>
<p>Now we cram it all back together using PHP <a href="http://ca3.php.net/implode">implode()</a>, this little guy will rebuild the array.  I decided to parse it with a <strong><br /></strong> tag instead of replacing the <strong>/n</strong>, you could set this to whatever you like. But in this context we are going to use the <strong><br /></strong> tag so the message will display correctly as HTML.</p>
<pre class="brush: php; title: ; notranslate">
$message = implode(&quot;&lt;br /&gt;&quot;, $messagePieces);
</pre>
<p>One Message box and we now have our Subject line and message. You can adapt this to do many things.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/09/php-handy-little-snippet-for-multiline-text-box/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to use Google HTTP Geocoding</title>
		<link>http://www.adampatterson.ca/blog/2010/09/google-http-geocoding/</link>
		<comments>http://www.adampatterson.ca/blog/2010/09/google-http-geocoding/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 19:42:56 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Addressbook]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Explode]]></category>
		<category><![CDATA[Full]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[PHP 5]]></category>
		<category><![CDATA[Post]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.studiolounge.net/2007/06/22/google-http-geocoding/</guid>
		<description><![CDATA[Getting the Latitude and Longitude of an address using the Google Maps API is actually quite easy. Google makes this process easy buy letting us interact with their API using a URI via a HTTP Request. By using a PHP function called urlencode I take the Address values and convert spaces to underscores so your [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.adampatterson.ca/wp-content/uploads/2007/06/goole-maps.jpg" alt="Google Maps - Edmonton" title="Goole Maps" width="620" height="395" class="full-image" /></p>
<p>Getting the Latitude and Longitude of an address using the <a href="http://code.google.com/apis/maps/index.html">Google Maps API</a> is actually quite easy.</p>
<p>Google makes this process easy buy letting us interact with their API using a <acronym title="Uniform Resource Identifier">URI</acronym> via a HTTP Request.</p>
<p>By using a PHP function called <code><a href="http://ca3.php.net/urlencode" target="_blank">urlencode</a></code> I take the Address values and convert spaces to underscores so your URI won’t explode and make Google angry.</p>
<p>Google is nice enough to give us formatted XML, KML, CSV, and even JSON. The basic URL looks something like this http://maps.google.com/maps/geo?q=address+city+state&#038;output=xml using a plus (+) signal the next field like City, Province, or Postal Code.</p>
<p><a href="http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway+Mountain+View+CA&#038;output=xml" target="_blank">Click to see output example</a>.</p>
<p>In my application all my contact information is stored in a Database, when I save or update a contact I generate the URL and bring back some XML to work with.</p>
<pre class="brush: php; title: ; notranslate">
   $googleAddress = &quot;http://maps.google.com/maps/geo?q=&quot;.urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($country).&quot;&amp;output=xml&quot;;
</pre>
<p>The next task was to get the contents of the file, for this I use the &#8220;<a href="http://ca.php.net/file_get_contents" target="_blank">file_get_contents</a>&#8221; function and pass it my URL.</p>
<pre class="brush: php; title: ; notranslate">
   $googlePage = file_get_contents($googleAddress);
</pre>
<p>PHP has a neat little function called <code><a href="http://ca.php.net/simplexml" target="_blank">SimpleXMLElement</a></code> that will convert XML to an object.</p>
<p><strong>Note:</strong> The <code><a href="http://ca.php.net/simplexml" target="_blank">SimpleXMLElement</a></code> will only work in PHP 5+.</p>
<pre class="brush: php; title: ; notranslate">
$xml = new SimpleXMLElement($googlePage);
</pre>
<p>Now that we have the values from the XML we can assign variables to them that will allow us to display the results.</p>
<p><a href="http://ca.php.net/xml">http://ca.php.net/xml</a></p>
<pre class="brush: php; title: ; notranslate">
list($longitude, $latitude, $altitude) = explode(&quot;,&quot;, $xml-&gt;Response-&gt;Placemark-&gt;Point-&gt;coordinates);
</pre>
<p>At this point you can use the information and store it in a database as I did, Or just echo it out.</p>
<pre class="brush: php; title: ; notranslate">
echo &quot;Longitude: $longitude, Latitude: $latitude&quot;;
</pre>
<h2>Here is the full code:</h2>
<pre class="brush: php; title: ; notranslate">
$address = '1 Sir Winston Churchill Square';
$city = 'edmonton';
$province = 'alberta';
$country = 'canada';
$postalcode = 'T5J 2R7';

// Google Geo Address
$googleAddress = &quot;http://maps.google.com/maps/geo?q=&quot;.urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($postalcode).'+'. urlencode($country).&quot;&amp;output=xml&quot;;

// Retrieve the URL contents
$googlePage = file_get_contents($googleAddress);

// Parse the returned XML file
$xml = new SimpleXMLElement($googlePage);

// Parse the coordinate string
list($longitude, $latitude, $altitude) = explode(&quot;,&quot;, $xml-&gt;Response-&gt;Placemark-&gt;Point-&gt;coordinates);

// Output the coordinates
echo &quot;Longitude: $longitude, Latitude: $latitude&quot;;
</pre>
<p>Links:<br />
<a href="http://www.google.com/apis/maps/documentation/#Geocoding_HTTP_Request" target="_blank">Google Geocoding HTTP Request</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/09/google-http-geocoding/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>WordPress: Dealing with three levels of navigation</title>
		<link>http://www.adampatterson.ca/blog/2010/09/wordpress-dealing-with-three-levels-of-navigation/</link>
		<comments>http://www.adampatterson.ca/blog/2010/09/wordpress-dealing-with-three-levels-of-navigation/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 20:49:13 +0000</pubDate>
		<dc:creator>Adam Patterson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Navigation]]></category>
		<category><![CDATA[Post]]></category>

		<guid isPermaLink="false">http://www.adampatterson.ca/?p=2518</guid>
		<description><![CDATA[As I mentioned in a previous post (WordPress is great but..) there are some issues that can arise when trying to use the WordPress navigation in a way that it was not intended. WordPress handles 2 levels with no issues at all, but three can be complicated. You have your root level navigation with Home, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.adampatterson.ca/wp-content/uploads/2010/09/path.jpg" alt="Path" title="path" width="620" height="319" class="full-image" /></p>
<p>As I mentioned in a previous post (<a href="http://www.adampatterson.ca/blog/2010/08/wordpress-is-great-but/">WordPress is great but..</a>) there are some issues that can arise when trying to use the WordPress navigation in a way that it was not intended. WordPress handles 2 levels with no issues at all, but three can be complicated.</p>
<p>You have your root level navigation with Home, Services, and About. Each of those pages containing sub pages for instance under Services we will have each service. Each Service has some Examples but we would like to list them in a side bar rather than horizontally in the main navigation.</p>
<p>WordPress would love to spit it out like this:</p>
<ul>
<li>Home</li>
<li> Service
<ul>
<li> Service 1
<ul>
<li>Example a</li>
<li>Example b</li>
<li>Example c</li>
</ul>
</li>
<li>Service 2
<ul>
<li>Example d</li>
<li>Example e</li>
</ul>
</li>
<li>Service 3</li>
</ul>
</li>
</ul>
<p>WordPress only carries a parent child relationship. It is unaware of anything greater or smaller.</p>
<p>Meaning <em>Example A</em> has no idea that it is located under Services, However WordPress will render the correct CSS attributes for <code>current_page_item</code>, <code>current_page_ancestor</code>, and <code>current_page_parent</code>. So WordPress on some level knows how to do this but has not extended that to its navigation functions.</p>
<p>Since the root navigation knows where it sits and the third level of navigation will only be shown id the current page ID has child pages the issues lie in the second level, more or less the link.</p>
<p>So how can this be resolved? Well it will take a bunch of work, not complicated work but something that should be embedded into WordPress.</p>
<pre class="brush: php; title: ; notranslate">
$parent_id = $post-&gt;post_parent;
$parent = get_post($parent_id);

  if($parent-&gt;post_parent){
  	$top_level_id = $parent-&gt;post_parent;
  }else if($post-&gt;post_parent){
  	$top_level_id = $post-&gt;post_parent;
  }else{
  	$top_level_id = $post-&gt;ID;
  }

  if($parent-&gt;post_parent){
  	$children = wp_list_pages(&quot;title_li=&amp;child_of=&quot;.$parent-&gt;post_parent.&quot;&amp;echo=0&amp;depth=1&quot;);
  	$my_id = $parent-&gt;post_parent;
  }else if($post-&gt;post_parent){
  	$children = wp_list_pages(&quot;title_li=&amp;child_of=&quot;.$post-&gt;post_parent.&quot;&amp;echo=0&amp;depth=1&quot;);
  	$my_id = $post-&gt;post_parent;
  }else{
  	$children = wp_list_pages(&quot;title_li=&amp;child_of=&quot;.$post-&gt;ID.&quot;&amp;echo=0&amp;depth=1&quot;);
  	$my_id = $post-&gt;post_parent;
  }
  if ($children) { ?&gt;
&lt;div id=&quot;subnavigation&quot;&gt;
  &lt;ul&gt;
  	&lt;?php echo $children; ?&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</pre>
<p>We basically traverse the page IDs setting when needs to be set in order to make the navigation act predictably.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adampatterson.ca/blog/2010/09/wordpress-dealing-with-three-levels-of-navigation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

