<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Lateral Code</title>
	
	<link>http://www.lateralcode.com</link>
	<description>A Web Development Blog Focused on Code and Technology</description>
	<lastBuildDate>Thu, 26 Aug 2010 22:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/lateralcode" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="lateralcode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">lateralcode</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2Flateralcode" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Flateralcode" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2Flateralcode" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bloglines.com/sub/http://feeds.feedburner.com/lateralcode" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Flateralcode" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Flateralcode" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Flateralcode" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><item>
		<title>Automated Uptime Verification</title>
		<link>http://www.lateralcode.com/automated-uptime-verification/</link>
		<comments>http://www.lateralcode.com/automated-uptime-verification/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 22:58:17 +0000</pubDate>
		<dc:creator>Patrick Lin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1618</guid>
		<description><![CDATA[If you have ever administered a website, you know that downtime is unavoidable. Creating a system to automatically check if your website is up can help deal with downtime and provide a better user experience. Doing so is quite simple if you have the following tools available: A web server other that that on which [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever administered a website, you know that downtime is unavoidable. Creating a system to automatically check if your website is up can help deal with downtime and provide a better user experience. Doing so is quite simple if you have the following tools available:</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/automated-uptime-verification/1.jpg" alt="Automated Uptime Verification" class="list-post-img" /></p>
<ul>
<li>A web server other that that on which the website is hosted</li>
<li>Cron (if you do not have this, you can use an online service)</li>
<li>PHP with libcurl support</li>
<li>PHP with mail support (optional)</li>
</ul>
<p><span id="more-1618"></span></p>
<h2>Setting up PHP and libcurl</h2>
<p>In case you don&#8217;t know, libcurl (and it&#8217;s cousin cURL) are tools for working with URLs and web pages. Provided that you have libcurl support and PHP, you can check if a site is active by using the following function:</p>
<pre><code>function checkURL($url) {
	$return = array();
	$curl = curl_init(); // Initialize libcurl
	// set options:
	curl_setopt ($curl, CURLOPT_URL, $url ); // URL to visit
	curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE); // returns a string instead of echoing to screen
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // follows redirects (recursive)
	curl_setopt($curl, CURLOPT_NOBODY, TRUE); // Only get headers, not content (saves on time)
	$result = curl_exec($curl);
	$errno = curl_errno($curl);
	if ( $errno != 0 ) { // curl_errno returns 0 if no error, otherwise returns the error code
		$return['message'] = "An error occurred while trying to $url! ".curl_err($curl); // If there was an error, return the error message
		$return['success'] = false;
	} else {
		$http = curl_getinfo($curl, CURLINFO_HTTP_CODE); // Get the HTTP return code
		$return['code'] = $http;
		if ( $http &gt;= 200 &#038;&#038; $http &lt; 300 ) { // An HTTP code greater than 200 and less than 300 means a successful load
			$return['message'] = "$url is up! ($http)";
			$return['success'] = true;
		} else {
			$return['message'] = "$url is down! ($http)";
			$return['success'] = false;
		}
	}
	curl_close($curl);
        return $return;
}
</code></pre>
<p>This function returns an array containing a success boolean (true, your site is up or false, your site is down) and a corresponding message string. Now that our checking works as specified, let&#8217;s move on to the notifications.</p>
<h2>Notifications</h2>
<p>In this section, we will present two ways to receive notifications.</p>
<h3>Notification by E-mail/SMS</h3>
<p>After checking the URL, you can use the PHP mail function to send the status to your e-mail account. On the other hand, you can find out the address for your SMS by sending a text to your own e-mail. For example, Verizon phones have the e-mail address &lt;phone number&gt;@vtext.com</p>
<p>The code:</p>
<pre><code>$result = checkURL($your_url);
if ( $result['success'] == true; )
	$subject = "Your site is still up!";
else
	$subject = "Oh no! Your site is down."
mail($your_email, $subject, $result['message']);
</code></pre>
<p>As you can see, this is a fairly easy method with minimal code.</p>
<h3>Push notifications for your iDevice</h3>
<p>If you have an iDevice, such as an iPhone or iPod Touch, you can use <a href="http://en.wikipedia.org/wiki/Apple_Push_Notification_Service">push notifications</a> as an alternative to e-mail or SMS.</p>
<p>First, download the app <a href="http://itunes.apple.com/us/app/pushme-to-free-instant-messages/id343341970?mt=8">pushme.to</a> and make and account. Take note of your username. This app will allow you to send pushed messages to your iDevice.</p>
<p>Then, you will need the following function:</p>
<pre><code>function pushmeto($username, $message) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "http://pushme.to/$username/");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($curl, CURLOPT_POST, TRUE);
	curl_setopt($curl, CURLOPT_POSTFIELDS, "message=".urlencode($message);
	curl_exec($ch);
	curl_close($ch);
}
</code></pre>
<p>Usage is simple:</p>
<pre><code>$result = checkURL($your_url);
pushmeto($your_username, $result['message']);
</code></pre>
<h2>Automation</h2>
<p>Cron can be used to automate the checking uptime process. If you don&#8217;t have cron on your server (which is rare), you can use an online service such as <a href="http://www.onlinecronjobs.com/">Online Cron Jobs</a>.</p>
<p>Cron jobs are set using the following format:</p>
<pre>
[min] [hour] [date] [month] [day_of_week] [command]
</pre>
<p>A cron job set to run at 7am every Tuesday would look like this:</p>
<pre>
0 7 * * 2 [command]
</pre>
<p>For more information on how to use cron, see this <a href="http://adminschoice.com/crontab-quick-reference">quick reference</a>.</p>
<p>As for the command, you can use the following (which assumes you saved the uptime verification code to a file called uptime.php):</p>
<pre>
php /path/to/uptime.php > /dev/null 2>&#038;1
</pre>
<p>Remember, of course, to change the path.</p>
<p>Once activated by the automated command, the code in the PHP file will check if your site is up and notify you. You can set this to occur as often as you want.</p>
<h2>Conclusion</h2>
<p>Do you have any other ways to monitor uptime? If so, please join the discussion below!</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/qUbiPPMKh-k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/automated-uptime-verification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Addresses? Use the new Google Maps API! Part 2</title>
		<link>http://www.lateralcode.com/new-google-maps-api-2/</link>
		<comments>http://www.lateralcode.com/new-google-maps-api-2/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 19:33:30 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1600</guid>
		<description><![CDATA[A few days ago, our experiments with the Google Maps API resulted in static images of various locations. Today, these same experiments will continue in a new medium: JavaScript. As we expand our knowledge of the API, we&#8217;ll cover the creation of a dynamic map, fully equipped with both movement and zooming. View the Demo [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, <a href="http://www.lateralcode.com/new-google-maps-api/">our experiments with the Google Maps API</a> resulted in static images of various locations. Today, these same experiments will continue in a new medium: JavaScript. As we expand our knowledge of the API, we&#8217;ll cover the creation of a dynamic map, fully equipped with both movement and zooming.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/new-google-maps-api-2/1.jpg" alt="Maps" class="list-post-img"/></p>
<p><a class="view" href="http://demo.lateralcode.com/new-google-maps-api-2/">View the Demo</a> <a class="dl" href="http://www.lateralcode.com/wp-content/uploads2/new-google-maps-api-2/new-google-maps-api-2.zip">Download the Files</a></p>
<p><span id="more-1600"></span></p>
<p>To begin our mad science, we&#8217;ll first need to include the API itself (note the HTML 5 that omits the <code>type</code> attribute of the <code>script</code> tag):</p>
<pre><code>&lt;!-- sensor=false tells google images we aren't trying to sense the user's location --&gt;
&lt;script src="http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;</code></pre>
<p>Moving forward, we&#8217;ll create a function which, when given an address, will display a dynamic map centered on it. Dutifully called <code>setMapAddress</code>, the function will have the following container:</p>
<pre><code>function setMapAddress( address )
{
	// code goes here
}</code></pre>
<p>To specify a location, Google Maps requires a latitude and longitude. As a result, our first task will be to convert the given <code>address</code> into the necessary format. Of course, this process has it&#8217;s own scientific term: geocoding. Being both scientifically and technologically-savvy, Google does the work for us in their <code>Geocoder</code> object. All we need to do is instantiate one and subsequently call its <code>geocode</code> method:</p>
<pre><code>var geocoder = new google.maps.Geocoder();

geocoder.geocode( { address : address }, function( results, status ) {

} );</code></pre>
<p>The geocode method requires, as arguments, an object as well as a callback function. Looking at the first parameter, <code>{ address : address }</code>, may strike initial confusion. The first use of the word <code>address</code> refers to a property of the object. On the contrary, the second use refers to the variable passed into the <code>setMapAddress</code> function, which we are trying to geocode.</p>
<p>Once the geocoding finishes, the callback function is invoked with a <code>results</code> array and <code>status</code> that is used to determine success. If the <code>status</code> notes a successful geocode, we&#8217;ll have to get the location (latitude/longitude) from the <code>results</code> array, setup options (similar to the options for the static images in the previous article), and finally construct the map. In its completed form, the callback function is:</p>
<pre><code>if( status == google.maps.GeocoderStatus.OK ) {
	var latlng = results[0].geometry.location;
	var options = {
		zoom: 8,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	new google.maps.Map( document.getElementById( 'map' ), options );
}</code></pre>
<p>The <code>options</code> object specifies the zoom, center, and type of the resultant map. In this case, these have been set to 8, the given address converted to a latitude/longitude, and road map respectively.</p>
<p>In addition, note that the constructor requires a DOM element and the aforementioned options. This means we&#8217;ll have to create the element, which has an <code>id</code> of <code>map</code>.</p>
<pre><code>&lt;div id="map"&gt;&lt;/div&gt;</code></pre>
<p><code>#map</code> also needs a defined width and height:</p>
<pre><code>#map {
	width: 512px;
	height: 512px;
}</code></pre>
<p>Last, but certainly not least, we have to call the <code>setMapAddress</code> function we recently finished:</p>
<pre>setMapAddress( "Chicago, IL" );</pre>
<p>Voila! That&#8217;s all we need for a functioning dynamic map. You can <a href="http://demo.lateralcode.com/new-google-maps-api-2/">view the demo</a> or <a href="http://www.lateralcode.com/wp-content/uploads2/new-google-maps-api-2/new-google-maps-api-2.zip">download the files</a>.</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/BMATER6IVJs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/new-google-maps-api-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Addresses? Use the new Google Maps API!</title>
		<link>http://www.lateralcode.com/new-google-maps-api/</link>
		<comments>http://www.lateralcode.com/new-google-maps-api/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 23:10:25 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1575</guid>
		<description><![CDATA[Would you like to travel to Paris? Maybe Singapore? How about Hawaii? Many people love to travel to discover new places, meet family, or just have a great time. In order to get from place to place, they often use maps. Indeed, maps are a vital part of traveling, providing directions to new, exciting locations. [...]]]></description>
			<content:encoded><![CDATA[<p>Would you like to travel to Paris? Maybe Singapore? How about Hawaii? Many people love to travel to discover new places, meet family, or just have a great time. In order to get from place to place, they often use maps. Indeed, maps are a vital part of traveling, providing directions to new, exciting locations.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/new-google-maps-api/1.jpg" alt="Maps" class="list-post-img"/></p>
<p>With the new technology age, maps have moved from tangible paper to the virtual world, allowing anyone to access them with a simple visit to <a href="http://maps.google.com">Google Maps</a>. More importantly, with the new <a href="http://code.google.com/apis/maps/index.html">Google Maps API Version 3</a>, it&#8217;s simple to integrate these maps into any website.</p>
<p>In the following article, I&#8217;ll be explaining how to get started with the new API and embrace the technology from the folks at Google.</p>
<p><span id="more-1575"></span></p>
<h2>Focus</h2>
<p>We&#8217;re going to focus on creating a static map, which is just an image of a location. This is in contrast to a dynamic map, which enables the user to move and zoom. In a follow-up article, which will be posted later this week, we&#8217;ll tackle a dynamic map using JavaScript.</p>
<h2>Static Map</h2>
<p>The static map only requires an image tag and a few configuration options:</p>
<pre><code>&lt;img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false" alt="Static Map of Chicago, Illinois"&gt;&lt;/img&gt;</code></pre>
<p>It looks a bit daunting at first, but, when broken down, it&#8217;s much easier to understand. Note that all the magic happens in the <code>src</code> attribute:</p>
<ul>
<li><code>http://maps.google.com/maps/api/staticmap?</code> &#8211; the base URL which we pass configuration options to.</li>
<li><code>center=Chicago,IL</code> &#8211; the center of the map, which, in this case, is Chicago, Illinois. Note that this can be any address.</li>
<li><code>zoom=8</code> &#8211; the amount to zoom in. This value was determined purely by experimentation.</li>
<li><code>size=512x512</code> &#8211; the size of the map in pixels. In this case, it&#8217;s 512px by 512px.</li>
<li><code>maptype=roadmap</code> &#8211; the type of the map. Possible values are roadmap, terrain, satellite, and hybrid.</li>
<li><code>sensor=false</code> &#8211; tells Google Maps whether we are trying to sense the user&#8217;s location. In this case, we aren&#8217;t.</li>
</ul>
<p>The above code produces the following image:<br />
<img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false" alt="Static Map of Chicago, Illinois" class="list-post-img"/></p>
<p>We can play around with the options a bit to produce a much different map:</p>
<pre>&lt;img alt="Static Map of New York City, New York" src="http://maps.google.com/maps/api/staticmap?center=New+York+City,NY&#038;zoom=4&#038;size=512x512&#038;maptype=hybrid&#038;sensor=false"&gt;&lt;/img&gt;</pre>
<p>These new configuration options yield the following map:<br />
<img src="http://maps.google.com/maps/api/staticmap?center=New+York+City,NY&#038;zoom=4&#038;size=512x512&#038;maptype=hybrid&#038;sensor=false" alt="Static Map of New York City, New York" class="list-post-img"/></p>
<p>When creating maps, it&#8217;s also necessary to mark important locations, whether they be destinations or areas to avoid. To do so, we can use the <code>markers</code> parameter:</p>
<pre>&lt;img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=Chicago,IL" alt="Static, Marked Map of Chicago, Illinois"&gt;&lt;/img&gt;</pre>
<p>Notice the new parameter:</p>
<pre>markers=Chicago,IL</pre>
<p>This will create the following marked map:<br />
<img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=Chicago,IL" alt="Static, Marked Map of Chicago, Illinois" class="list-post-img"/></p>
<p>We can add in more configuration options to the marker parameter, including a color and a label. For example:</p>
<pre>markers=color:blue|Chicago,IL</pre>
<p>Notice the pipe operator (<code>|</code>) which separates the color from the location. This change will edit the marker color, yielding the following map:<br />
<img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=color:blue|Chicago,IL" alt="Static, Marked (Blue) Map of Chicago, Illinois" class="list-post-img"/></p>
<p>As you can see, the blue marker still has a dot representing the location. When working with multiple locations, you might want to change this dot to a number or letter in order to differentiate between locations:</p>
<pre>markers=color:blue|label:A|Chicago,IL</pre>
<p>Instead of the dot, the image now has the letter <em>A</em>:<br />
<img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=color:blue|label:A|Chicago,IL" alt="Static, Marked (Labeled A) Map of Chicago, Illinois" class="list-post-img"/></p>
<p>To add multiple markers, just add in another <code>markers</code> parameter:</p>
<pre>&lt;img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=color:blue|label:A|Chicago,IL&#038;markers=color:purple|label:B|Hammond,IN" alt="Static, Marked Map of Chicago, Illinois and Hammond, Indiana"&gt;&lt;/img&gt;</pre>
<p>Now there are two markers: one for Chicago, Illinois and another for Hammond, Indiana:<br />
<img src="http://maps.google.com/maps/api/staticmap?center=Chicago,IL&#038;zoom=8&#038;size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=color:blue|label:A|Chicago,IL&#038;markers=color:purple|label:B|Hammond,IN" alt="Static, Marked Map of Chicago, Illinois and Hammond, Indiana" class="list-post-img"/></p>
<p>This is accomplished using the following marker code:</p>
<pre>markers=color:blue|label:A|Chicago,IL&#038;markers=color:purple|label:B|Hammond,IN</pre>
<p>Finally, when specifying the markers attribute, note that the <code>center</code> and <code>zoom</code> parameters are no longer required. If we omit these, we get the following code:</p>
<pre>&lt;img src="http://maps.google.com/maps/api/staticmap?size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=color:blue|label:A|Chicago,IL&#038;markers=color:purple|label:B|Hammond,IN" alt="Static, Marked Map of Chicago, Illinois and Hammond, Indiana with no Center and Zoom"&gt;&lt;/img&gt;</pre>
<p>And the corresponding map:<br />
<img src="http://maps.google.com/maps/api/staticmap?size=512x512&#038;maptype=roadmap&#038;sensor=false&#038;markers=color:blue|label:A|Chicago,IL&#038;markers=color:purple|label:B|Hammond,IN" alt="Static, Marked Map of Chicago, Illinois and Hammond, Indiana with no Center and Zoom" class="list-post-img"/></p>
<p>As shown in the image, Google Maps will automatically use a fitting <code>center</code> and <code>zoom</code> in order to display the two locations.</p>
<h2>Conclusion</h2>
<p>As you can see, we&#8217;ve created multiple static maps all through the new Google Maps API. With no needed API key (which was previously required) and some simple URL manipulation, Google makes it near effortless to integrate a map into any website.</p>
<p>Are you interested in the <a href="http://code.google.com/apis/maps/index.html">Google Maps API</a>? Do you have any questions or comments? Did you stumble upon more useful features for static maps? Leave a comment below and a share your thoughts!</p>
<p>Don&#8217;t forget that we&#8217;ll be creating dynamic maps with the JavaScript API later this week!</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/1UCd6mi_wdI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/new-google-maps-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Elegant jQuery Slideshow Plugin: Lateral Slider</title>
		<link>http://www.lateralcode.com/lateral-slider/</link>
		<comments>http://www.lateralcode.com/lateral-slider/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 03:44:57 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1541</guid>
		<description><![CDATA[Let&#8217;s start with the defintion: Slider &#8211; The easiest known way to showcase your images and photos on the web in style. Who can deny it? This is the generation of web 2.0 sliders. A few JavaScript files, some CSS, and a bit of markup is all you need to generate an elegant showcase of [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s start with the defintion:</p>
<p>Slider &#8211; The easiest known way to showcase your images and photos on the web in style.</p>
<p>Who can deny it? This is the generation of web 2.0 sliders. A few JavaScript files, some CSS, and a bit of markup is all you need to generate an elegant showcase of your images. Best part? 99% of it is <strong>done for you</strong>.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/lateral-slider/1.jpg" alt="Lateral Slider" class="list-post-img"/></p>
<p>Well, that&#8217;s the ideal situation. Unfortunately, it&#8217;s often times much more complicated. That&#8217;s why I created <a href="http://codecanyon.net/item/lateral-slider-jquery-plugin-with-13-transitions/109908?ref=nviswan10">Lateral Slider</a>.</p>
<p><a class="view" href="http://codecanyon.net/item/lateral-slider-jquery-plugin-with-13-transitions/full_screen_preview/109908?ref=nviswan10">View the Demo</a><a class="dl" href="http://codecanyon.net/item/lateral-slider-jquery-plugin-with-13-transitions/109908?ref=nviswan10">Purchase it from CodeCanyon</a></p>
<p><span id="more-1541"></span></p>
<p>Lateral Slider is a jQuery slideshow plugin that I recently released on <a href="http://codecanyon.net/?ref=nviswan10">CodeCanyon</a>. How does it work? Simple.</p>
<h2>1. Include the CSS/JavaScript</h2>
<p>CSS:</p>
<pre>&lt;link rel="stylesheet" href="slider.css" type="text/css" media="screen" /&gt;</pre>
<p>JavaScript:</p>
<pre>&lt;!-- jQuery --&gt;
&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;!-- Lateral Slider --&gt;
&lt;script src=&quot;slider.jquery.js&quot;&gt;&lt;/script&gt;</pre>
<h2>2. Write the Markup</h2>
<p>All you&#8217;ll need is a div and a few images:</p>
<pre>&lt;div id=&quot;slider&quot;&gt;
	&lt;img src=&quot;image1.jpg&quot; alt=&quot;Image 1&quot; /&gt;
	&lt;img src=&quot;image2.jpg&quot; alt=&quot;Image 2&quot; /&gt;
	&lt;img src=&quot;image3.jpg&quot; alt=&quot;Image 3&quot; /&gt;
&lt;/div&gt;</pre>
<h2>3. Activate Lateral Slider</h2>
<pre>&lt;!-- no need for type="text/javascript" in HTML 5 --&gt;
&lt;script&gt;$( '#slider' ).lateralSlider();&lt;/script&gt;</pre>
<h2>4. Celebrate</h2>
<p>Lateral Slider is ready to be displayed.</p>
<h2>5. Oops, was there supposed to be a fifth step?</h2>
<p>Nope. You&#8217;re done.</p>
<h2>Features</h2>
<p>Simple setup is great, but I&#8217;m sure you&#8217;re also looking for features. Fear not:</p>
<ul>
<li>13 different transitions</li>
<li>Support for all major browsers (including IE6)</li>
<li>Image linking</li>
<li>Many configurable options</li>
<li>Controls (forward, back, pick a slide)</li>
<li>Comprehensive documentation</li>
<li>Much more&#8230;</li>
</ul>
<p>Want to know more? Check out the <a href="http://codecanyon.net/item/lateral-slider-jquery-plugin-with-13-transitions/109908?ref=nviswan10">description at CodeCanyon</a>. Better yet, why don&#8217;t you take a look at the <a href="http://codecanyon.net/item/lateral-slider-jquery-plugin-with-13-transitions/full_screen_preview/109908?ref=nviswan10">live preview</a> and see Lateral Slider for yourself.</p>
<h2>Purchase It!</h2>
<p>Interested? It&#8217;s only $10. Why not <a href="http://codecanyon.net/item/lateral-slider-jquery-plugin-with-13-transitions/109908?ref=nviswan10">purchase it from CodeCanyon</a>? <img src='http://www.lateralcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/0_75q3FPDDs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/lateral-slider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Snippet: Generate the DZone Button with PHP (Save HTTP Requests!)</title>
		<link>http://www.lateralcode.com/dzone-button-php/</link>
		<comments>http://www.lateralcode.com/dzone-button-php/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 06:20:54 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1530</guid>
		<description><![CDATA[The folks at DZone provide a great way for developers to share links in one, concentrated area. URLs which rank highly in their system (those that receive many &#8220;up&#8221; votes) often get quite a bit of traffic. As a result, many developers use the DZone button to provide their visitors a way to rate articles [...]]]></description>
			<content:encoded><![CDATA[<p>The folks at <a href="http://www.dzone.com/links/index.html">DZone</a> provide a great way for developers to share links in one, concentrated area. URLs which rank highly in their system (those that receive many &#8220;up&#8221; votes) often get quite a bit of traffic. As a result, many developers use the <a href="http://www.dzone.com/links/buttons.jsp">DZone button</a> to provide their visitors a way to rate articles directly from their own website.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/dzone-button-php/1.jpg" alt="DZone" class="list-post-img"/></p>
<p>Do you use the DZone button on your website? If not, you should definitely consider it. Unfortunately, each one of these buttons requires the inclusion of a JavaScript file, which adds an HTTP request (and thus slows down the page). With some quick studying of the <a href="http://widgets.dzone.com/links/widgets/zoneit.js">included JavaScript</a>, it&#8217;s possible (not to mention quite easy) to generate this same button with only PHP, thus saving a request.</p>
<p><span id="more-1530"></span></p>
<p>Here&#8217;s the function to do so:</p>
<pre><code>function dzone_button( $style, $url, $title, $description = false )
{
	$html = '&lt;iframe src=&quot;http://widgets.dzone.com/links/widgets/zoneit.html?t=' . $style;
	$html .= '&amp;url=' . urlencode( $url ) . '&amp;title=' . urlencode( $title );

	if( $description !== false )
		$html .= '&amp;description=' . $description;

	if( $style == 1 )
		$html .= '&quot; height=&quot;70&quot; width=&quot;50&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;';
	else
		$html .= '&quot; height=&quot;25&quot; width=&quot;155&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;';

	return $html;
}</code></pre>
<p>It requires the same parameters are the JavaScript file: the type of button (1 for tall, 2 for wide), URL, title, and description (optional).</p>
<p>To use it, just plug, chug, and echo:</p>
<pre><code>echo dzone_button( 1, 'http://example.com', 'Example', 'Example description' );</code></pre>
<p>If you&#8217;re using WordPress, consider the following snippet:</p>
<pre><code>echo dzone_button( 1, get_permalink(), get_the_title() );</code></pre>
<p>If you want a description, use <code>get_the_excerpt</code>:</p>
<pre><code>echo dzone_button( 1, get_permalink(), get_the_title(), get_the_excerpt() );</code></pre>
<p>That&#8217;s all there is to it; you&#8217;ve generated the same button without the JavaScript. The only real difference is that you&#8217;ll have an <code>iframe</code> directly in the HTML page rather than one generated by JavaScript. Happy DZoning!</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/-Y1flp4a1Fg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/dzone-button-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Snippet: Backup your Files through SSH</title>
		<link>http://www.lateralcode.com/backup-files-ssh/</link>
		<comments>http://www.lateralcode.com/backup-files-ssh/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 23:38:11 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1517</guid>
		<description><![CDATA[Today we&#8217;re introducing a new type of post at Lateral Code: the simple snippet. These articles are meant to provide you with concise, meaningful snippets of code for use on your website. To kickoff off the simple snippet, we begin with an important topic for any site: backups. Have you ever lost important files? If [...]]]></description>
			<content:encoded><![CDATA[<p><em>Today we&#8217;re introducing a new type of post at Lateral Code: the simple snippet. These articles are meant to provide you with concise, meaningful snippets of code for use on your website. To kickoff off the simple snippet, we begin with an important topic for any site: backups.</em></p>
<p>Have you ever lost important files? If you have, you know the importance of keeping backups.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/backup-files-ssh/1.jpg" alt="Files" class="list-post-img"/></p>
<p>Backups could mean the difference between life and death for your site. It&#8217;s vital to constantly generate backups and even store them on a remote machine in order to ensure full protection. All it takes is a determined attacker to possibly corrupt all of your files. As a result, this simple snippet is focused on getting you started with the backup process.</p>
<p><span id="more-1517"></span></p>
<p>Generating backups through secure-shell (SSH) can be quite a simple task. The <code>tar</code> command is all you&#8217;ll need:</p>
<pre><code>$ tar czvf backup.tar.gz path/to/directory</code></pre>
<p>Let&#8217;s break down this snippet into it&#8217;s constituents:</p>
<pre><code>tar - the command to create a <a href="http://en.wikipedia.org/wiki/Tar_(file_format)">tar file</a>

c - the option that creates a tar ball
z - add gzip compression
v - verbose option to display files that are added to the tar ball
f - used to specify the archive file (in this case backup.tar.gz)

backup.tar.gz - the name of the archive file for the f option
path/to/directory - recursively search this directory for files to place in the tar ball</code></pre>
<p>After running this command on the directory of your choice (generally the public or public_html directory for a full backup), you should have an archive of files which you can easily download to your own computer (or keep on your web server).</p>
<p>You can read more about the <code>tar</code> command <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?tar">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/KIfMyEAVcdk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/backup-files-ssh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>5 Must-Reads on JavaScript Inheritance</title>
		<link>http://www.lateralcode.com/javascript-inerhitance/</link>
		<comments>http://www.lateralcode.com/javascript-inerhitance/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 00:01:06 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1507</guid>
		<description><![CDATA[Do you know inheritance in JavaScript? Do you truly understand how it works? Are you aware that JavaScript uses a prototypal inheritance scheme that is often disfavored or disliked? Have you used a script on the web to adapt this scheme to classical inheritance? In these past few days, I&#8217;ve been writing a jQuery slider [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know inheritance in JavaScript? Do you <em>truly understand</em> how it works? Are you aware that JavaScript uses a prototypal inheritance scheme that is often disfavored or disliked? Have you used a script on the web to adapt this scheme to classical inheritance?</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/javascript-inheritance/1.jpg" alt="Read" class="list-post-img"/></p>
<p>In these past few days, I&#8217;ve been writing a jQuery slider plugin with various transition effects. To make the code succinct, I decided to use a base Transition object that other objects, which define a specific type of transition, inherit from. This led me to the question of JavaScript inheritance. </p>
<p>Inheritance in JavaScript has been a controversial subject. Should a standard be set to use classical inheritance or should the base prototypal system be favored? Both paths have their own advantages and disadvantages. The readings listed below will hopefully give you enough information to choose which method you want to use.</p>
<p><span id="more-1507"></span></p>
<h2>1. <a href="http://javascript.crockford.com/private.html">Private Members in JavaScript</a></h2>
<p>This reading isn&#8217;t <em>really</em> about inheritance, but it&#8217;s an important primer on object-oriented programming in JavaScript which will help give you a deeper understand of what is to come.</p>
<p><a href="http://javascript.crockford.com/private.html"><img src="http://www.lateralcode.com/wp-content/uploads2/javascript-inheritance/inheritance1.png" alt="Private Members in JavaScript" class="list-post-img"/></a></p>
<h2>2. <a href="http://ejohn.org/blog/simple-javascript-inheritance/">Simple JavaScript Inheritance</a></h2>
<p>John Resig, the creator of the jQuery JavaScript library, presents his own take on inheritance. Using his utility, it&#8217;s possible to change JavaScript into more of a classical inheritance scheme.</p>
<p><a href="http://ejohn.org/blog/simple-javascript-inheritance/"><img src="http://www.lateralcode.com/wp-content/uploads2/javascript-inheritance/inheritance2.png" alt="Simple JavaScript Inheritance" class="list-post-img"/></a></p>
<h2>3. <a href="http://www.ruzee.com/blog/2008/12/javascript-inheritance-via-prototypes-and-closures">JavaScript Inheritance via Prototypes and Closures</a></h2>
<p>Steffen Rusitschka explains the advantages and disadvantages of prototype-based and closure-based (more classical) inheritance. He also presents his own derivative of Resig&#8217;s script.</p>
<p><a href="http://www.ruzee.com/blog/2008/12/javascript-inheritance-via-prototypes-and-closures"><img src="http://www.lateralcode.com/wp-content/uploads2/javascript-inheritance/inheritance3.png" alt="JavaScript Inheritance via Prototypes and Closures" class="list-post-img"/></a></p>
<h2>4. <a href="http://www.crockford.com/javascript/inheritance.html">Classical Inheritance in JavaScript</a></h2>
<p>This reading, by Douglas Crockford, a well-known JavaScript developer, is yet another way to achieve the classical inheritance scheme in JavaScript. In addition to presenting his extension, Crockford also explains the added functionality that JavaScript can provide.</p>
<p><a href="http://www.crockford.com/javascript/inheritance.html"><img src="http://www.lateralcode.com/wp-content/uploads2/javascript-inheritance/inheritance4.png" alt="Classical Inheritance in JavaScript" class="list-post-img"/></a></p>
<h2>5. <a href="http://javascript.crockford.com/prototypal.html">Prototypal Inheritance in JavaScript</a></h2>
<p>In this reading, Crockford analyzes his old classical inheritance and realizes the benefits of sticking with the prototypal structure. This last article expresses the flip side of the controversy, explaining why there is no need for a classical inheritance scheme. </p>
<p><a href="http://javascript.crockford.com/prototypal.html"><img src="http://www.lateralcode.com/wp-content/uploads2/javascript-inheritance/inheritance5.png" alt="Prototypal Inheritance in JavaScript" class="list-post-img"/></a></p>
<p>Hopefully, these links have shed light on the debate between prototypal inheritance and classical inheritance. Although I used Resig&#8217;s script in my slider plugin, I recently came across Crockford&#8217;s prototypal inheritance article (#5) and am now rethinking my ways for future projects.</p>
<p>So, what&#8217;s your take on JavaScript inheritance? Leave a comment below. I would be glad to hear more arguments for either side!</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/4sFb9W4yYDU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/javascript-inerhitance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coding a Color Manager with Object-Oriented PHP</title>
		<link>http://www.lateralcode.com/color-manager/</link>
		<comments>http://www.lateralcode.com/color-manager/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 23:57:17 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1483</guid>
		<description><![CDATA[Colors are a vital part of web design. They can easily make or break a design. Often times, designers are interested in modifying colors by mixing, fading, or brightening them. This will be the basis for today&#8217;s article. We&#8217;re going to go through the process of creating a simple color manager with object-oriented PHP. Rather [...]]]></description>
			<content:encoded><![CDATA[<p>Colors are a vital part of web design. They can easily make or break a design. Often times, designers are interested in modifying colors by mixing, fading, or brightening them. This will be the basis for today&#8217;s article.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/color-manager/1.jpg" alt="Color Wheel" class="list-post-img"/></p>
<p>We&#8217;re going to go through the process of creating a simple color manager with object-oriented PHP. Rather than focusing on multiple forms of modification, our color manager will perform one basic function: fading. Given a base color (hexadecimal) and a fade percentage, we will calculate a new, faded color.</p>
<p><a href="http://demo.lateralcode.com/color-manager/" class="view button"><span>View the color manager</span></a> <a href="http://www.lateralcode.com/wp-content/uploads2/color-manager/color-manager.zip" class="dl button"><span>Download the files</span></a></p>
<p><span id="more-1483"></span></p>
<p>Fading, when taken from a red-green-blue (RGB) color perspective, only involves linear interpolation. In other words, to fade a color from blue <code>rgb( 0, 0, 255 )</code> to white <code>rgb( 255, 255, 255 )</code>, each color component must be increased by a percentage of the difference to the target component.</p>
<p>To elucidate this statement, consider the following example: if you wanted to fade 10% from blue to white, you would calculate the components as such:</p>
<pre><code>original color (blue) = rgb( 0, 0, 255 )
target color (white)  = rgb( 255, 255, 255 )
fade percent          = 10%

original color's red component = 0
target color's red component   = 255
difference in components       = 255 - 0
                               = 255

new red component = original red component + difference in components * fade percent
new red component = 0 + 255 * .10
                  = 25.5</code></pre>
<p>This process would be repeated with the two other components. The new color (after all calculations) would be:</p>
<pre><code>rgb( 25.5, 25.5, 255 );</code></pre>
<p>If you were to look at this color, it would be a faded blue.</p>
<p>Since we are working with a hexadecimal color, it would be beneficial to convert it to RGB, fade it, and then convert back. Although there are ways to fade directly in hexadecimal format, this path begs the use of object-oriented PHP and does not require a knowledge of bit manipulation.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/color-manager/2.jpg" alt="Hexadecimal" class="list-post-img"/></p>
<p>Before starting to code, you must first understand how to convert from hexadecimal to RGB and back. The first two letters/digits in a hex color represent the red component in an RGB color. In like manner, the third and fourth letters/digits represent the green component. Finally, the last two digits represent the blue component. After splitting the hex color into these three sets, each one must be converted from base 16 to base 10 to get the final RGB color:</p>
<pre><code>hex color (white) = FFFFFF

red component = FF (base 16)
              = 255 (base 10)

green component = FF (base 16)
                = 255 (base 10)

blue component = FF (base 16)
               = 255 (base 10)

hex color (white) = rgb( 255, 255, 255 )</code></pre>
<p>To go from RGB to hexadecimal, convert each component to base 16 and concatenate them:</p>
<pre><code>rgb color (white) = ( 255, 255, 255 )

red component = 255 (base 10)
              = FF (base 16)

green component = 255 (base 10)
                = FF (base 16)

blue component = 255 (base 10)
               = FF (base 16)

rgb color (white) = hex FFFFFF</code></pre>
<p>Now we can move on to the implementation. To begin, we will first write a <code>HexColor</code> class, which will hold a <code>$hex</code> variable representing the hex string. This class will provide a function to convert to an RGB color:</p>
<pre><code>class HexColor {
	private $hex;

	public function HexColor( $hex )
	{
		if( strpos( $hex, '#' ) === 0 )
			$this->hex = substr( $hex, 1 );
		else
			$this->hex = $hex;
	}

	public function getHexString()
	{
		return $this->hex;
	}

	public function convertToRGB()
	{
		// first two digits represent red, next two blue, and the last two green
		$red = substr( $this->hex, 0, 2 );
		$green = substr( $this->hex, 2, 2 );
		$blue = substr( $this->hex, 4, 2 );

		// convert from hexadecimal to base 10
		$red = (int) base_convert( $red, 16, 10 );
		$green = (int) base_convert( $green, 16, 10 );
		$blue = (int) base_convert( $blue, 16, 10 );

		return new RGBColor( $red, $green, $blue );
	}

	public static function isValid( $hex )
	{
		return (bool) preg_match( '/^(#)?[a-zA-Z0-9]{6}$/', $hex );
	}

	public function __toString()
	{
		return $this->hex;
	}
}</code></pre>
<p>Note that the <code>isValid</code> function is used to check whether a given string is a valid hex color.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/color-manager/3.jpg" alt="RGB" class="list-post-img"/></p>
<p>Next is the <code>RGBColor</code> class, which contains similar functionality and a <code>fadeTo</code> function based off of the calculations explained at the beginning of this article:</p>
<pre><code>class RGBColor {
	private $red, $green, $blue;

	public function RGBColor( $red, $green, $blue )
	{
		$this->red = $this->clamp( $red );
		$this->green = $this->clamp( $green );
		$this->blue = $this->clamp( $blue );
	}

	public function getRed()
	{
		return $this->red;
	}

	public function getGreen()
	{
		return $this->green;
	}

	public function getBlue()
	{
		return $this->blue;
	}

	public function fadeTo( $rgbColor, $percent )
	{
		$newRed = ( 1 - $percent ) * $this->red + $percent * $rgbColor->getRed();
		$newGreen = ( 1 - $percent ) * $this->green + $percent * $rgbColor->getGreen();
		$newBlue = ( 1 - $percent ) * $this->blue + $percent * $rgbColor->getBlue();

		return new RGBColor( (int) $newRed, (int) $newGreen, (int) $newBlue );
	}

	public function convertToHex()
	{
		$newRed = base_convert( $this->red, 10, 16 );
		$newGreen = base_convert( $this->green, 10, 16 );
		$newBlue = base_convert( $this->blue, 10, 16 );

		$newRed = $this->addZero( $newRed );
		$newGreen = $this->addZero( $newGreen );
		$newBlue = $this->addZero( $newBlue );

		return new HexColor( $newRed . $newGreen . $newBlue );
	}

	private function addZero( $colorValue )
	{
		if( strlen( $colorValue ) == 1 )
			$colorValue = '0' . $colorValue;
		return $colorValue;
	}

	private function clamp( $colorValue )
	{
		// clamp colorValue in interval [0, 255]
		return max( 0, min( 255, $colorValue ) );
	}

	public function __toString()
	{
		return '(' . $this->red . ', ' . $this->green . ', ' . $this->blue . ')';
	}
}</code></pre>
<p>Since RGB color values can only be in the 0-255 range, the <code>clamp</code> function is used to ensure this is true. In addition, the <code>addZero</code> function makes sure the hex color is six digits long by prepending the necessary 0s.</p>
<p>Given these classes, a <code>$hex</code> string, and a <code>$fade</code> percentage, the process to fade a hex color is as follows:</p>
<ol>
<li>Instantiate a <code>HexColor</code> object with the <code>$hex</code> string</li>
<li>Convert it to an <code>RGBColor</code></li>
<li>Call the <code>fadeTo</code> function with the given <code>$fade</code> percentage and the color white</li>
<li>Convert the <code>RGBColor</code> back to a <code>HexColor</code> and display it</li>
</ol>
<p>In addition, to make this more flexible, we can change the third step to darken the color (fade to black) if the fade percentage is negative. These instructions lead to the following code:</p>
<pre><code>$hexColor = new HexColor( $hex );

// target color is white by default
$targetColor = new RGBColor( 255, 255, 255 );

// want to darken the color--target is black
if( $fade < 0 ) {
	$targetColor = new RGBColor( 0, 0, 0 );
	$fade = -$fade; // make fade positive
}

$rgbColor = $hexColor->convertToRGB();

// fade / 100 is the percentage
$fadedColor = $rgbColor->fadeTo( $targetColor, $fade / 100 );
echo $fadedColor->convertToHex();</code></pre>
<p>Combined with some AJAX, form handling, and CSS (which I won&#8217;t be going over, as this was a lesson in object-oriented PHP), this code ultimately results in a simple color manager, which you may view/download using the buttons below:</p>
<p><a href="http://demo.lateralcode.com/color-manager/" class="view">View the color manager</a> <a href="http://www.lateralcode.com/wp-content/uploads2/color-manager/color-manager.zip" class="dl">Download the files</a></p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/iDYTUj_8MJk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/color-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Store Arrays in a Database</title>
		<link>http://www.lateralcode.com/store-array-database/</link>
		<comments>http://www.lateralcode.com/store-array-database/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 07:01:39 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1475</guid>
		<description><![CDATA[When working with databases, it is sometimes necessary to store an array in a MySQL field. Unfortunately, there is no way to directly pass in an array as a parameter. As a result, storing these data structures is a bit more complex, but by no means hard or impossible. To convert any array (or any [...]]]></description>
			<content:encoded><![CDATA[<p>When working with databases, it is sometimes necessary to store an array in a MySQL field. Unfortunately, there is no way to directly pass in an array as a parameter. As a result, storing these data structures is a bit more complex, but by no means hard or impossible.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/store-array-database/1.jpg" alt="Array" class="list-post-img"/></p>
<p><span id="more-1475"></span></p>
<p>To convert any array (or any object) into a string using PHP, call the <code>serialize</code> function:</p>
<pre><code>$array = array( 1, 2, 3 );
$string = serialize( $array );
echo $string;</code></pre>
<p><code>$string</code> will now hold a string version of the array. The output of the above code is as follows:</p>
<pre><code>a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}</code><code></code></pre>
<p>To convert back from the string to the array, use <code>unserialize</code>:</p>
<pre><code>// $array will contain ( 1, 2, 3 )
$array = unserialize( $string );</code></pre>
<p>Now let&#8217;s try serializing an array of 200 randomly generated integers from 1 to 1000:</p>
<pre>$array = array();
for( $i = 0; $i < 200; $i++ )
	$array[] = mt_rand( 1, 1000 );

$string = serialize( $array );
echo $string;</pre>
<p>This outputs something like:
</pre>
<pre>a:200:{i:0;i:465;i:1;i:202;i:2;i:9;i:3;i:448;i:4;i:887;i:5;i:844;i:6;i:230;i:7;i:785;i:8;i:892;i:9;i:949;i:10;i:864;i:11;i:29;i:12;i:239;i:13;i:521;i:14;i:632;i:15;i:115;i:16;i:903;i:17;i:331;i:18;i:732;i:19;i:192;i:20;i:487;i:21;i:297;i:22;i:1000;i:23;i:674;i:24;i:301;i:25;i:208;i:26;i:819;i:27;i:690;i:28;i:906;i:29;i:544;i:30;i:316;i:31;i:932;i:32;i:458;i:33;i:64;i:34;i:268;i:35;i:590;i:36;i:80;i:37;i:375;i:38;i:837;i:39;i:928;i:40;i:209;i:41;i:880;i:42;i:60;i:43;i:98;i:44;i:395;i:45;i:880;i:46;i:336;i:47;i:183;i:48;i:321;i:49;i:167;i:50;i:917;i:51;i:423;i:52;i:882;i:53;i:768;i:54;i:415;i:55;i:728;i:56;i:431;i:57;i:540;i:58;i:72;i:59;i:338;i:60;i:431;i:61;i:669;i:62;i:234;i:63;i:699;i:64;i:983;i:65;i:602;i:66;i:348;i:67;i:995;i:68;i:772;i:69;i:337;i:70;i:113;i:71;i:644;i:72;i:209;i:73;i:587;i:74;i:822;i:75;i:135;i:76;i:269;i:77;i:111;i:78;i:406;i:79;i:364;i:80;i:613;i:81;i:522;i:82;i:621;i:83;i:789;i:84;i:195;i:85;i:15;i:86;i:674;i:87;i:916;i:88;i:186;i:89;i:70;i:90;i:59;i:91;i:911;i:92;i:242;i:93;i:270;i:94;i:903;i:95;i:553;i:96;i:166;i:97;i:201;i:98;i:250;i:99;i:683;i:100;i:801;i:101;i:691;i:102;i:602;i:103;i:862;i:104;i:357;i:105;i:872;i:106;i:105;i:107;i:86;i:108;i:496;i:109;i:208;i:110;i:349;i:111;i:69;i:112;i:938;i:113;i:500;i:114;i:961;i:115;i:437;i:116;i:446;i:117;i:16;i:118;i:782;i:119;i:268;i:120;i:296;i:121;i:341;i:122;i:343;i:123;i:160;i:124;i:247;i:125;i:610;i:126;i:600;i:127;i:962;i:128;i:224;i:129;i:659;i:130;i:951;i:131;i:124;i:132;i:937;i:133;i:819;i:134;i:684;i:135;i:930;i:136;i:104;i:137;i:493;i:138;i:568;i:139;i:290;i:140;i:333;i:141;i:626;i:142;i:160;i:143;i:80;i:144;i:278;i:145;i:840;i:146;i:942;i:147;i:141;i:148;i:28;i:149;i:69;i:150;i:241;i:151;i:724;i:152;i:386;i:153;i:209;i:154;i:933;i:155;i:281;i:156;i:410;i:157;i:397;i:158;i:360;i:159;i:337;i:160;i:29;i:161;i:321;i:162;i:543;i:163;i:642;i:164;i:943;i:165;i:273;i:166;i:505;i:167;i:856;i:168;i:860;i:169;i:67;i:170;i:879;i:171;i:735;i:172;i:964;i:173;i:858;i:174;i:965;i:175;i:984;i:176;i:821;i:177;i:540;i:178;i:857;i:179;i:363;i:180;i:588;i:181;i:707;i:182;i:588;i:183;i:540;i:184;i:380;i:185;i:35;i:186;i:52;i:187;i:926;i:188;i:686;i:189;i:833;i:190;i:941;i:191;i:385;i:192;i:730;i:193;i:743;i:194;i:815;i:195;i:497;i:196;i:567;i:197;i:811;i:198;i:339;i:199;i:144;}</pre>
<p>These strings can easily be stored in a database and unserialized when the data is accessed. Often times, <code>base64_encode</code> is used in conjunction with <code>serialize</code> when storing arrays:</p>
<pre><code>$string = base64_encode( serialize( $array ) );</code></pre>
<p>The encrypted string can then be restored to an array by using <code>base64_decode</code>:</p>
<pre><code>$array = unserialize( base64_decode( $string ) );</code></pre>
<p>Unfortunately, these strings can grow to be quite large. To counter the size, you may want to use <code>gzcompress</code> to apply gzip compression and significantly reduce the size:</p>
<pre><code>$smallString = gzcompress( $string );</code></pre>
<p>Note that gzip compression can be undone with <code>gzuncompress</code>.</p>
<p>That&#8217;s really all there is to it. Now you can easily store an array of information in a database!</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/VnlCNXIeV2w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/store-array-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prevent Form Attacks with Basic Math Security</title>
		<link>http://www.lateralcode.com/basic-math-security/</link>
		<comments>http://www.lateralcode.com/basic-math-security/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 23:43:33 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1470</guid>
		<description><![CDATA[Form security is a top priority these days due to the risks of losing sensitive information, getting spammed by bots, being exposed to viruses, and more. As a result, it is important to take steps to secure your forms in order to counter these risks. In this article, we&#8217;re going to create a PHP class [...]]]></description>
			<content:encoded><![CDATA[<p>Form security is a top priority these days due to the risks of losing sensitive information, getting spammed by bots, being exposed to viruses, and more. As a result, it is important to take steps to secure your forms in order to counter these risks.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/basic-math-security/1.jpg" alt="Security" class="list-post-img"/></p>
<p>In this article, we&#8217;re going to create a PHP class to help secure forms with basic math questions. This class will generate two random numbers that must be added by the user in order to ensure a human is submitting the form. Taken as a whole, this class only takes about 10 minutes to write. This begs the question: are you willing to secure your forms for a few minutes of work?</p>
<p><a href="http://demo.lateralcode.com/basic-math-security/" class="view">View a Demo</a> <a href="http://www.lateralcode.com/wp-content/uploads2/basic-math-security/basic-math-security.zip" class="dl">Download the Files</a></p>
<p><span id="more-1470"></span></p>
<p>This PHP class, which will be called BasicMathSecurity, will have three main functions:</p>
<ul>
<li><code>generateNumbers</code> &#8211; generates two random numbers from 1-9 that the user must add</li>
<li><code>getField</code> &#8211; returns the label and input tags that will be displayed</li>
<li><code>isCorrect</code> &#8211; checks whether the user input is correct</li>
</ul>
<p>Before we begin with these functions, let&#8217;s write a simple constructor:</p>
<pre><code>public function BasicMathSecurity( $name = 'math' )
{
	$this->name = $name;
	$this->generateNumbers();
}</code></pre>
<p><code>$name</code> will be the name of the user input field. Note that generate numbers is called during instantiation to eliminate the need for an extra call.</p>
<p><code>generateNumbers</code> is trivial through the use of PHP&#8217;s <a href="http://www.php.net/manual/en/function.mt-rand.php"><code>mt_rand</code></a> function:</p>
<pre><code>public function generateNumbers()
{
	$this->operand1 = mt_rand( 1, 9 );
	$this->operand2 = mt_rand( 1, 9 );
}</code></pre>
<p><code>getField</code> will create three HTML tags:</p>
<ul>
<li>A label for the math field with the question (ex. &#8220;1 + 1 =&#8221;)</li>
<li>The math field, which is a text input</li>
<li>A hidden input field for the answer to the math question</li>
</ul>
<p>Using the <code>$name</code> paramater asked for in the constructor, the following function can be produced:</p>
<pre><code>public function getField()
{
	$label = '&lt;label for=&quot;' . $this-&gt;name . '&quot;&gt;' . $this-&gt;operand1 . ' + ' . $this-&gt;operand2 . ' = &lt;/label&gt;';
	$math = '&lt;input type=&quot;text&quot; name=&quot;' . $this-&gt;name . '&quot; value=&quot;&quot; id=&quot;' . $this-&gt;name . '&quot;&gt;&lt;/input&gt;';

	$answer = '&lt;input type=&quot;hidden&quot; name=&quot;' . $this-&gt;name . '-answer&quot; value=&quot;' . ( $this-&gt;operand1 + $this-&gt;operand2 ) . '&quot;&gt;&lt;/input&gt;';
	$string = $label . &quot;\n&quot; . $math . &quot;\n&quot; . $answer;

	return $string;
}</code></pre>
<p>An id is added to the math field for styling purposes. In addition, note that the answer field uses the same name as the math field with a &#8220;-answer&#8221; appended to it. This is important for the next function.</p>
<p>Finally, the <code>isCorrect</code> function will use the global <code>$_REQUEST</code> array to check whether the user input matches the answer:</p>
<pre><code>public function isCorrect()
{
	$answer = $this->name . '-answer';
	if( !isset( $_REQUEST[ $this->name ] ) || !isset( $_REQUEST[ $answer ] ) )
		return false;

	return (int) $_REQUEST[ $this->name ] == (int) $_REQUEST[ $answer ];
}</code></pre>
<p>The <code>$answer</code> index is based off the code in the <code>getField</code> function.</p>
<p>As a whole, the class is as follows:</p>
<pre><code>class BasicMathSecurity {

	private $name;
	private $operand1, $operand2;

	public function BasicMathSecurity( $name = 'math' )
	{
		$this->name = $name;
		$this->generateNumbers();
	}

	public function generateNumbers()
	{
		$this->operand1 = mt_rand( 1, 9 );
		$this->operand2 = mt_rand( 1, 9 );
	}

	public function getField()
	{
		$label = '&lt;label for=&quot;' . $this-&gt;name . '&quot;&gt;' . $this-&gt;operand1 . ' + ' . $this-&gt;operand2 . ' = &lt;/label&gt;';
		$math = '&lt;input type=&quot;text&quot; name=&quot;' . $this-&gt;name . '&quot; value=&quot;&quot; id=&quot;' . $this-&gt;name . '&quot;&gt;&lt;/input&gt;';

		$answer = '&lt;input type=&quot;hidden&quot; name=&quot;' . $this-&gt;name . '-answer&quot; value=&quot;' . ( $this-&gt;operand1 + $this-&gt;operand2 ) . '&quot;&gt;&lt;/input&gt;';
		$string = $label . &quot;\n&quot; . $math . &quot;\n&quot; . $answer;

		return $string;
	}

	public function isCorrect()
	{
		$answer = $this->name . '-answer';
		if( !isset( $_REQUEST[ $this->name ] ) || !isset( $_REQUEST[ $answer ] ) )
			return false;

		return (int) $_REQUEST[ $this->name ] == (int) $_REQUEST[ $answer ];
	}

}</code></pre>
<p>To use it, construct a BasicMathSecurity object and display the field:</p>
<pre><code>$math = new BasicMathSecurity( 'math' );
echo $math->getField();</code></pre>
<p>Subsequently, in your form handler, check if the input is correct. Remember to construct the object with the same name parameter:</p>
<pre><code>// construct the object with the same parameter
$math = new BasicMathSecurity( 'math' );
if( $math->isCorrect() ) {
	// process the form
}</code></pre>
<p>And that&#8217;s it! You may <a href="http://demo.lateralcode.com/basic-math-security/">view the demo</a> and <a href="http://www.lateralcode.com/wp-content/uploads2/basic-math-security/basic-math-security.zip">download the files used to create it</a>.</p>
<img src="http://feeds.feedburner.com/~r/lateralcode/~4/texY72VSTC4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/basic-math-security/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
