<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Code and Stuff, mostly, code.

It’d be best to check out my full blog for proper reading. Or Twitter.

Contact me.

Any source code listed on this site may be used freely and without attribution. I reserve no responsibility for the correctness of this source code.</description><title>Code</title><generator>Tumblr (3.0; @nickio)</generator><link>http://code.nickcharlton.net/</link><item><title>Create a MySQL TIMESTAMP from a Unix Timestamp</title><description>&lt;p&gt;This simple function formats a Unix Timestamp as one ready to be inserted into MySQL.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function mktimestamp($epoch) {
	$epoch = date("Y-d-m H:i:s", $epoch);
	
	return $epoch;
}
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/146345783</link><guid>http://code.nickcharlton.net/post/146345783</guid><pubDate>Tue, 21 Jul 2009 23:17:00 +0100</pubDate><category>php</category><category>mysql</category></item><item><title>Correct Format for Producing MySQL TIMESTAMPS</title><description>&lt;p&gt;This is the correct formatting to use when using the date() function when building MySQL TIMESTAMPS.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
    date("Y-d-m H:i:s");
&lt;/pre&gt;

&lt;p&gt;Example: 2009-21-07 20:58:15&lt;/p&gt;

&lt;p&gt;Other than that it’s pretty simple. Unfortunately, MySQL doesn’t support Unix Timestamps, which would mean that it’s a good idea to have a function to deal with this.&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/146235817</link><guid>http://code.nickcharlton.net/post/146235817</guid><pubDate>Tue, 21 Jul 2009 20:00:00 +0100</pubDate><category>php</category><category>mysql</category></item><item><title>OPML Sample</title><description>&lt;p&gt;This is a basic OPML sample to base OPML building scripts on top of. Pulled from a nearly blank OmniOutliner OPML export. The fields “text”, “Date”, “URL” can be changed to how you desire.&lt;/p&gt;

&lt;pre class="sh_xml"&gt;
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;opml version="1.0"&gt;
  &lt;head&gt;
    &lt;title&gt;example&lt;/title&gt;
    &lt;expansionState&gt;&lt;/expansionState&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;outline text="My Title" URL="My URL" Date="It's Date"/&gt;
  &lt;/body&gt;
&lt;/opml&gt;
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/144380820</link><guid>http://code.nickcharlton.net/post/144380820</guid><pubDate>Sun, 19 Jul 2009 00:25:00 +0100</pubDate><category>code</category><category>opml</category><category>xml</category></item><item><title>Geocode an address with the Google Maps API</title><description>&lt;p&gt;This &lt;strike&gt;code snippet&lt;/strike&gt; mini-project, returns a geocode (that is, Latitude and Longitude) from a given address.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; This requires that you obtain your own Google Maps API Key and on it’s own violates the &lt;a href="http://code.google.com/apis/maps/terms.html#section_10_12"&gt;Google Maps API Terms and Conditions&lt;/a&gt;. In short, you must accompany this with a map, then you’re safe.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It features some basic error handling, which will catch the status messages returned. Unfortunately, this makes the code a little messy, but otherwise you’d have nothing returned. &lt;/p&gt;

&lt;pre class="sh_php"&gt;
	$key = "your-key";
	
	/* Given data */
	$street = "My Street";
	$town = "My Town";
	$country = "My Country";
	$postcode = "My Postcode";
	
	$address = urlencode($street . ", " . $town . ", " . $country);

	$xml_input = "http://maps.google.com/maps/geo?q={$address}&amp;output=xml&amp;oe=utf8&amp;sensor=false&amp;key={$key}";
	$xml = simplexml_load_file($xml_input);
	
	$status =  $xml-&gt;Response-&gt;Status-&gt;code; // returned status code
	
	if ($status == "500") {
		echo "Request could not be completed due to a server error. Please try again.";
	} 
	if ($status == "601") {
		echo "You didn't pass an address. Please try again.";
	}
	if ($status == "602") {
		echo "The address could not be found. Please try again.";
	}
	if ($status == "603") {
		echo "The address cannot be returned due to legal or contractual reasons.";
	}
	if ($status == "610") {
		echo "The API key used is invalid or doesn't match the domain.";
	}
	if ($status == "620") {
		echo "Too many requests have been sent. You've either reached the limit, or sent too many at once.";
	}
	else {
		echo "&lt;h2&gt;Coordinates";
		$coords = $xml-&gt;Response-&gt;Placemark-&gt;Point-&gt;coordinates; // return coords
		// splits and reverses the returned string to make it correct
		$latlong = explode(",", $coords);
		echo $latlong[1] . ", " . $latlong[0];
	}
&lt;/pre&gt;

&lt;p&gt;This returns the latitude and longitude of the address passed to it, for example: 51.2343500, -0.7325510. You can also access this through the $latlong array. &lt;/p&gt;

&lt;p&gt;Naturally, it’s had my details stripped out of it. &lt;a href="http://code.google.com/apis/maps/signup.html"&gt;You can get an API key here.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Read more: &lt;a href="http://code.google.com/apis/maps/documentation/geocoding/index.html"&gt;&lt;a href="http://code.google.com/apis/maps/documentation/geocoding/index.html"&gt;http://code.google.com/apis/maps/documentation/geocoding/index.html&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/143704741</link><guid>http://code.nickcharlton.net/post/143704741</guid><pubDate>Fri, 17 Jul 2009 21:44:00 +0100</pubDate><category>code</category><category>php</category><category>geocode</category><category>googleapi</category></item><item><title>Microformats: Geo</title><description>&lt;p&gt;This is the first in a series of posts which will provide examples of the various Microformats. “Geo” is used to markup Latitude and Longitude data into web pages, rather useful for providing location data near/around maps.&lt;/p&gt;

&lt;pre class="sh_html"&gt;
    &lt;div class="geo"&gt;
        &lt;abbr class="latitude" title="37.408183"&gt;N 37° 24.491 
        &lt;abbr class="longitude" title="-122.13855"&gt;W 122° 08.313
    &lt;/div&gt;
&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Source: &lt;a href="http://microformats.org/wiki/geo"&gt;Microformats Wiki: Geo&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/142990305</link><guid>http://code.nickcharlton.net/post/142990305</guid><pubDate>Thu, 16 Jul 2009 21:05:01 +0100</pubDate><category>code</category><category>html</category><category>microformats</category></item><item><title>Gravatars as User icons in PHP</title><description>&lt;p&gt;This is an extension of the &lt;a href="http://en.gravatar.com/site/implement/"&gt;Gravatar docs&lt;/a&gt;, which seemed to be a &lt;a href="http://en.gravatar.com/site/implement/php"&gt;little lackluster in the PHP department&lt;/a&gt;.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
	$email = "nickcharlton91@gmail.com";
	$size = "80";
	$gravatar =  "http://www.gravatar.com/avatar/" . md5($email) . "?s={$size}";
&lt;/pre&gt;

&lt;pre class="sh_html"&gt;
        &lt;img src="&lt;?php echo $gravatar ?&gt;" alt="Gravatar" width="&lt;?php echo $size ?&gt;" height="&lt;?php echo $size ?&gt;" /&gt;
&lt;/pre&gt;

&lt;p&gt;I’m sure there are similar ways to do this, some most likely more flexible, but this is a great way to slot in user icons in projects. I especially like Github’s way of not making you upload a photo. (Obviously, it’s biggest use is in Blogs, but I digress.)&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/142878492</link><guid>http://code.nickcharlton.net/post/142878492</guid><pubDate>Thu, 16 Jul 2009 17:26:00 +0100</pubDate><category>code</category><category>php</category></item><item><title>Strip Tags &amp; Protect Against Injection Attacks</title><description>&lt;p&gt;Whilst this is not a perfect way to protect against attacks, it is a step.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function clean_input($input) {
	// note, needs a MySQL connection
	$clean = strip_tags($input);
	$cleaner = mysql_real_escape_string($clean);
	
	return $cleaner;
}
&lt;/pre&gt;

&lt;p&gt;Best used wrapped around a string. e.g. &lt;code&gt;clean_input($string);&lt;/code&gt;&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/141462709</link><guid>http://code.nickcharlton.net/post/141462709</guid><pubDate>Tue, 14 Jul 2009 15:01:50 +0100</pubDate><category>php</category><category>code</category><category>mysql</category></item><item><title>Validate an Email Address using Regex</title><description>&lt;p&gt;This function validates an email address passed to it and returns either True or False.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function validate_email($email) {
	$match = "/^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/";
	$regex = preg_match($match, $email);
	
	return $regex;
}
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/134882321</link><guid>http://code.nickcharlton.net/post/134882321</guid><pubDate>Fri, 03 Jul 2009 18:52:46 +0100</pubDate><category>code</category><category>php</category><category>regex</category></item><item><title>Header Redirect</title><description>&lt;p&gt;This simple snippet redirects from one page to another. &lt;em&gt;Note: Must be sent before the rest of the content.&lt;/em&gt;&lt;/p&gt;

&lt;pre class="sh_php"&gt;
    header('Location: login.php');
    exit();
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/133529617</link><guid>http://code.nickcharlton.net/post/133529617</guid><pubDate>Wed, 01 Jul 2009 14:57:32 +0100</pubDate><category>php</category><category>code</category></item><item><title>An Improved Linux Uptime Function</title><description>&lt;p&gt;This is an improvement over &lt;a href="http://nickio.tumblr.com/post/117646716/linux-uptime"&gt;this post&lt;/a&gt;. This also produces an uptime, but properly splits the seconds in “days”, “hours”, “minutes” &amp; “seconds”, as well as appending s or not if it should be there. &lt;/p&gt;

&lt;p&gt;Could further be improved to include options to be called from the function.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function uptime() {
	$procuptime = popen("cat /proc/uptime | awk -F \" \" '{print $1}'", "r");
	$read = fread($procuptime, 2096);
	$day = floor($read / 86400);
	$hour = floor(($read % 86400) / 3600);
	$minute = floor((($read % 86400) % 3600) / 60);
	$second = floor(((($read % 86400) % 3600) % 60));
			
	if ($day == "1") {
		echo "$day day ";
	} else {
		echo "$day days ";
	}
	if ($hour == "1") {
		echo "$hour hour ";
	} else {
		echo "$hour hours ";
	}
	if ($minute == "1") {
		echo "$minute minute ";
	} else {
		echo "$minute minutes ";
	} 
	if ($second == "1") {
		echo "$second second ";
	} else {
		echo "$second seconds ";
	} 
}	
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/127783377</link><guid>http://code.nickcharlton.net/post/127783377</guid><pubDate>Mon, 22 Jun 2009 01:35:00 +0100</pubDate><category>code</category><category>php</category><category>linux</category></item><item><title>Linux Free, Used &amp; Total Memory</title><description>&lt;p&gt;This shows free, used and total memory. This like the &lt;a href="http://nickio.tumblr.com/post/117648491/linux-used-and-free-disk-space-on-a-volume"&gt;other one&lt;/a&gt;, is somewhat complex and uses a mix of shell commands and PHP.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
// displays total and free memory
	// total memory
	$total = popen("/usr/bin/free | grep \"Mem:\" | awk -F\" \" '{print $2}'", "r");
	$read = fread($total, 2096);
	$totalram = round($read / 1000, 2);
	pclose($total);
	// used memory
	$used = popen("/usr/bin/free | grep \"Mem:\" | awk -F\" \" '{print $3}'", "r");
	$read = fread($used, 2096);
	$usedram = round($read / 1000, 2);
	pclose($used);
	// free memory
	$freeram = $totalram - $usedram;
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/117649473</link><guid>http://code.nickcharlton.net/post/117649473</guid><pubDate>Thu, 04 Jun 2009 02:49:31 +0100</pubDate><category>code</category><category>php</category><category>shell</category><category>linux</category></item><item><title>Linux Used and Free Disk Space on a Volume</title><description>&lt;p&gt;This somewhat complex bit of code shows the used and free disk space on a specified volume. Uses a mix of shell commands and PHP here.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
// displays disk space for the "/dev/xvda1" volume
	// used space
	$used = popen("/bin/df -k | grep \"xvda1\" | awk -F\" \" '{print $3}'", "r");
	$read = fread($used, 2096);
	$systemused = round($read / 1000000, 2);
	pclose($used);
	// free space
	$free = popen("/bin/df -k | grep \"xvda1\" | awk -F\" \" '{print $2}'", "r");
	$read = fread($free, 2096);
	$systemcap = round($read / 1000000, 2);
	pclose($free);
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/117648491</link><guid>http://code.nickcharlton.net/post/117648491</guid><pubDate>Thu, 04 Jun 2009 02:47:06 +0100</pubDate><category>code</category><category>linux</category><category>php</category><category>shell</category></item><item><title>Linux Kernel Version</title><description>&lt;p&gt;Prints the current Kernel Version, eg “2.6.18-xenU”&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function kernel() {
		$la = popen("uname -r", "r");
		$read = fread($la, 2096);
		echo $read;
	}
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/117647511</link><guid>http://code.nickcharlton.net/post/117647511</guid><pubDate>Thu, 04 Jun 2009 02:44:41 +0100</pubDate><category>code</category><category>linux</category><category>php</category></item><item><title>Debian Version Number</title><description>&lt;p&gt;This shows the Debian version number, eg “4.0”.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function debianversion() {
		// naturally only works on Debian.
		$la = popen("cat /etc/debian_version", "r");
		$read = fread($la, 2096);
		echo $read;
	}
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/117647101</link><guid>http://code.nickcharlton.net/post/117647101</guid><pubDate>Thu, 04 Jun 2009 02:43:40 +0100</pubDate><category>linux</category><category>code</category><category>php</category><category>debian</category></item><item><title>Linux Uptime</title><description>&lt;p&gt;This prints the machines uptime, but only in days.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function uptime() {
		$la = popen("/usr/bin/uptime", "r");
		$read = fread($la, 2096);
		$value = substr($read, 13, 7);
		echo $value;
	}
&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Note: This should be improved to show a much more complex date/time. Not showing the time is, annoying.&lt;/em&gt;&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/117646716</link><guid>http://code.nickcharlton.net/post/117646716</guid><pubDate>Thu, 04 Jun 2009 02:42:35 +0100</pubDate><category>code</category><category>php</category><category>linux</category></item><item><title>Linux Load Average</title><description>&lt;p&gt;This example prints the &lt;a href="http://en.wikipedia.org/wiki/Load_average#Unix-style_load_calculation"&gt;load average&lt;/a&gt; of the machine.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function loadaverage() {
		$la = popen("/usr/bin/uptime", "r");
		$read = fread($la, 2096);
		$value = substr($read, -17);
		echo $value;
	}
&lt;/pre&gt;

&lt;p&gt;Printed like: “0.02, 0.01, 0.07”.&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/117645724</link><guid>http://code.nickcharlton.net/post/117645724</guid><pubDate>Thu, 04 Jun 2009 02:40:00 +0100</pubDate><category>code</category><category>linux</category><category>php</category></item><item><title>Convert kB to MB &amp; kB to GB</title><description>&lt;p&gt;The first example converts kB (kilobyte) to MB (megabyte) and the second from kB to GB (gigbyte) and rounds it to a nice 2 decimal places.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
    round($read / 1000, 2)
&lt;/pre&gt;

&lt;pre class="sh_php"&gt;
    round($read / 1000000, 2)
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/117536670</link><guid>http://code.nickcharlton.net/post/117536670</guid><pubDate>Wed, 03 Jun 2009 22:38:00 +0100</pubDate><category>code</category><category>php</category></item><item><title>Rewriting URL's with Strings</title><description>&lt;p&gt;This example uses Apache’s mod_rewrite to convert a url like http://url/img.php?img=5 to http://url/5.&lt;/p&gt;

&lt;pre&gt;
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ img.php?img=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ img.php?img=$1
&lt;/pre&gt;</description><link>http://code.nickcharlton.net/post/114027366</link><guid>http://code.nickcharlton.net/post/114027366</guid><pubDate>Thu, 28 May 2009 01:29:21 +0100</pubDate><category>code</category><category>apache</category><category>linux</category></item><item><title>Calculate Directory Size</title><description>&lt;p&gt;In this example, the size of a given directory is output. &lt;em&gt;Note: This will only work on Linux.&lt;/em&gt;&lt;/p&gt;

&lt;pre class="sh_php"&gt;
$f = "./files";
$io = popen ("/usr/bin/du -sk " . $f, "r");
$size = fgets ($io, 4096);
$value = substr($size, 0, -8);
pclose ($io);
echo $value;
&lt;/pre&gt;

&lt;p&gt;The directory name is held in $f, the “-8” value would need to be changed to remove the correct number of characters, or replaced with another function which would calculate the length of $f. Not used here to keep it simple.&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/113987759</link><guid>http://code.nickcharlton.net/post/113987759</guid><pubDate>Thu, 28 May 2009 00:16:54 +0100</pubDate></item><item><title>pushimage: A Function to Dynamically Output Images</title><description>&lt;p&gt;This example provides a function which when passed a variable, will output the file of that variable. This makes the assumption that the file is located in “files/thenameofthefile.ext”.&lt;/p&gt;

&lt;pre class="sh_php"&gt;
function pushimage($file) {
	$file = "files/" . $file;
	if (file_exists($file)) {
		header('Content-Type: image/jpeg');
		header('Expires: 0');
		header('Content-Length: ' . filesize($file));
		ob_clean();
		flush();
		readfile($file);
		exit;
	}
}
&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Note: At the time of posting, this does not check to ensure the file extension being returned is correct.&lt;/em&gt;&lt;/p&gt;</description><link>http://code.nickcharlton.net/post/113913463</link><guid>http://code.nickcharlton.net/post/113913463</guid><pubDate>Wed, 27 May 2009 21:07:00 +0100</pubDate><category>php</category><category>code</category></item></channel></rss>
