Some interesting links for October 13th

Here are some links that I’ve found interesting:

Posted in random | Tagged | Leave a comment

Interesting Links

Here are some links that I’ve found interesting:

Posted in random | Leave a comment

Improve the typography on your Mac in 5 seconds.

As you are surely aware, Microsoft ripped off Helvetica to create the Arial font. It’s not as nice, and doesn’t look nice on screen at all. The image below shows some of the differences.

As you can see, the ends of each letter aren’t lined up properly, they are all at weird angles.

Though Helvetica is the superior font, many sites specify that they should use Arial preferentially to Helvetica. This seems to be a throwback from Dreamweaver, which defines it’s default font stack as Arial, Helvetica, sans-serif. As Arial comes with Macs, no one will get the Helvetica in this situation.

Luckily, on your Mac there is an easy fix to rid websites of Arial – just open Fontbook, disable Arial and restart your web browser. Arial no more, Helvetica is back!

Posted in Uncategorized, osx | Tagged , , , , | 2 Comments

How to know that your PHP is borked before your clients kill you

Maybe it’s just me, but every now and then I make a quick change to a PHP script, don’t bother checking it, then a few hours/days/months later either realise that I missed a vital semicolon, or mismatched a bracket. This started to get on my nerves, as now this has happened a couple of times I’d like to prevent it from happening again.

After thinking about this a little, and asking the wonderful stackoverflow.com, a person known as okoman suggested using the cli version of PHP to test parse the code using the -l flag.

I quickly wrapped it into a parcel of (ugly) code, set up a cron job to run it as frequently as I estimate I make errors and can now sleep peacefully knowing that all semicolons are in place, and all if’s have the right number of brackets.

So, here’s the delicious file which I’ve placed on my server. You can edit it not to produce output if you’d like and just to send emails/tweets.

<?
function list_dir($dir) {
 
	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if (substr("$file", 0, 1) != ".") {
					$files[] = $file;
				}
			}
			closedir($dh);
		}
	}
	sort($files);
	return $files;
}
 
$dir = "/path/to/code//";
$files = list_dir($dir);
$num_files = count($files);
 
for($i=0;$i<$num_files;$i++) {
	if (strpos($files[$i],".php") !== false) {
		$path = $dir.$files[$i];
		$result = `php -l $path`;
 
		if (strpos($result, "Errors parsing") !== false) {
			//tweet_error("Syntax",$files[$i]); //Function in my last post
			mail("emailaddress@example.com","Syntax Error",$files[$i]);
			echo "Syntax error in $files[$i]\n";
		}
	}
}
 
?>

And that’s all there is to it.

To check that it’s working OK, try making a file such as:

<?
if () {
?>

and run the error checking file.

This could be extended by parsing out line numbers and error types, but this minimum setup works for me.

Posted in PHP | Tagged , , | 3 Comments

How to use Twitter as an error log

Sure this has been done before, but I had a brainwave today – why not use Twitter as an error log for web apps?

I already have error handling on my functions, so surely this shouldn’t be a difficult addition… turns out it’s not.

First, you’ll need a twitter account. I’d recommend setting one up, then protecting the updates. Keep the username and password, you’ll need them in a minute. I’d then follow the new account you set up, as well as anyone else on your team.

Then, define a function as follows in PHP, preferably in a global include or some such thing.

function tweet_error ($error, $description) {
	$username = 'yourusername';
	$password = 'yourpassword';
	$status = "#$error - $description";
 
	$update_url = 'http://www.twitter.com/statuses/update.xml'; // http://identi.ca/api/statuses/update.xml will use identi.ca instead.
 
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "$update_url");
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
	curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
 
	$result = curl_exec($curl);
	$resultArray = curl_getinfo($curl);
 
	curl_close($curl);
 
	return ($resultArray['http_code'] == 200);
}

My function here takes two parameters, the error code as well as a description. You could generalise this and rejig it a bit, as well as perhaps changing the $status variable to look different.

For instance,

$status = "d username #$error - $description";

or

$status = "@username #$error - $description";

to send it to you. You could even define different usernames for different types of errors, if you are in a team.

In your code, then call it like this:

tweet_error ("404", $_SERVER[SCRIPT_URL]);

to log a 404, as well as the page it came from, or:

tweet_error ("DB-Connect", "Failed");

In your database connection script, or even:

tweet_error ("Possible spam", "From user XXXX");

in your comment/email form handling.

Hope this comes in useful!

Posted in PHP, tips | 19 Comments

Using Texter to speed up web design/development

Lifehacker’s Texter let’s you define short macros that allow you type a little, and get a lot back. I’ve not used this before, but am constantly finding myself typing the same bits over and over and over, so this is a very neat and helpful little tool.

Basically, you just define a command, then specify what that should expand to. The command %| inserts the cursor in that location after the replacement. A few macros that I’ve actually found myself using are:

Hotstring Replacement
$g $_GET['%|']
$p $_POST['$|']
<> <?= %| ?>
£ &pound;
l” &ldquo;
r” &rdquo;
msre mysql_real_escape_string(%|)

All nice simple things that make a big difference through the day, particularly <> and the Get and Post ones!

Any more useful ideas for this tool?

Posted in PHP, software | Tagged , , , | 1 Comment

Enable better caching on your website using PHP

Just a quick snippet to help you tell useragents accessing your site that the content hasn’t changed.

function caching_headers ($file, $timestamp) {
	$gmt_mtime = gmdate('r', $timestamp);
	header('ETag: "'.md5($timestamp.$file).'"');
 
	if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
		if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $gmt_mtime || str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == md5($timestamp.$file)) {
			header('HTTP/1.1 304 Not Modified');
			exit();
		}
	}
 
	header('Last-Modified: '.$gmt_mtime);
	header('Cache-Control: public');
	return 1;
}

Then call this in the top of your code like this:

caching_headers ($_SERVER['SCRIPT_FILENAME'], filemtime($_SERVER['SCRIPT_FILENAME']));

This sets an Etag based on the filename and modification date, and just returns a 304 header without any content if the browser already as the content.

You can also supply a different unix timestamp as the second argument allowing the timestamp to come from a database if applicable.

Hope this comes in useful!

Posted in PHP | Tagged | 2 Comments

7 differences between Linux and Windows

Background

Having mainly used various linuxes over the last few years, I’ve returned to using Windows 7 on my main desktop (out of curiosity) and XP on my work laptop (out of the fact it’s not mine!). Thought I’d share somethings I’ve noticed now I’m moving between them daily.

Read More »

Posted in linux, software | Tagged , | 3 Comments

Friendfeedifying your feeds

Although these tips are designed for feeds used with Friendfeed, the first two aren’t just for that – it’s just that Friendfeed exposes the extra functions in it’s interface. The third tip regarding SUP currently is only really relevant for Friendfeed.

Read More »

Posted in PHP | Tagged , , , , , | 1 Comment

5 terrible SEO ideas

Having looked at many small businesses websites, I’ve compiled a list here of 5 things that many of them are doing wrong with regards to SEO. I’m not saying that SEO isn’t important, but some techniques just don’t work. So, here goes… Read More »

Posted in tips | Tagged , , | 37 Comments