My Twitter

Displaying Recent Stumbles (From StumbleUpon)

April 3rd, 2009

StumbleUpon is a funky web 2.0 community where it recommends websites you may be interested in based on other people similar interests. Unfortunately they do not have an API for easy integration. Luckily though, they do offer RSS Feeds for user’s activity. Here is the code you need to show your recent stumbles:

<?php # File created on  3rd April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
/*
function - recent_stumbles(string $username [, string $type= NULL [, int $limit = 5]])
	$username - The stumbleupon username, such as rogem002
	$type - Default: NULL - What you want to limit your rss to show. Can be NULL, blog, comments, favorites or reviews
	$limit - Default: 5 - how many tweets you wish to show, must be numeric.
*/

function recent_stumbles($username, $type=NULL, $limit=5){
	if(!is_numeric($limit)){$limit = 5;}
	if($type !== NULL && $type !== 'blog' && $type !== 'comments' && $type !== 'favorites' && $type !== 'reviews'){$type = NULL;}
	$xml = simplexml_load_file('http://rss.stumbleupon.com/user/'.urlencode($username).'/'.$type);
	$items_count= count($xml->channel->item);
	if($items_count < $limit){$limit = $items_count;}
	$i = 0;
	$return .= '
<ul>';
	while($i < $limit){
		$return .= '
<li title="'.$xml->channel->item[$i]->title.'"><!-- '.$xml->channel->item[$i]->pubDate.' -->
		<a href="'.$xml->channel->item[$i]->link.'" title="'.$xml->channel->item[$i]->title.'"><img src="'.$xml->channel->item[$i]->enclosure["url"].'" alt="'.$xml->channel->item[$i]->title.'" border="0" />
		'.$xml->channel->item[$i]->title.'</a></li>

';
		$i++;
	}
	$return .=  '</ul>

';

	return $return;
}

echo recent_stumbles('Rogem002', 'favorites', 5);

/*
You are free to share, modify and use this code for commercial  uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
*/
?>

Displaying Recent Tweets via Twitter’s RSS

April 1st, 2009

Twitter has sprung to fame in the last few months, mostly thanks to its fantastic API. Here is a really quick and easy way to display your recent tweets on your website.

<?php # File created on  1st April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
/*
function - recent_tweets(string $username [, int $limit = 5])
	$username - Your twitter username, such as rogem002
	$limit - Default: 5 - how many tweets you wish to show, must be numeric.
*/

function recent_tweets($username, $limit=5){
	if(!is_numeric($limit)){$limit = 5;}
	$xml = simplexml_load_file('http://search.twitter.com/search.atom?q=from%3A'.urlencode($username));
	$items_count= count($xml->entry);
	if($items_count < $limit){$limit = $items_count;}
	$i = 0;
	$return .= '
<ul>';
	while($i < $limit){
		$return .= '
<li title="'.$xml->entry[$i]->title.'"><!-- '.$xml->entry[$i]->published.' -->'.$xml->entry[$i]->content.'</li>

';
		$i++;
	}
	$return .=  '</ul>

';

	return $return;
}

echo recent_tweets('rogem002', 5);

/*
You are free to share, modify and use this code for commercial  uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
*/
?>

Edit: This only works for Twitterers who have privacy settings open (thanks Dan from XDnet.co.uk for the heads up).

CSS Tip: Reset Styles

March 30th, 2009

CSS is incredible, however its implementation in browsers has not been standardised, meaning that every browser has a slightly different default settings for how elements should be rendered. Luckily there is an easy fix in CSS to reduce the level of dissimilarity.

Add the following to the top of your CSS:

* {
	margin: 0;
	padding: 0;
	color: #000;
	background: #FFF;
	font-style: inherit;
	text-decoration: none;
	font-size: 100%;
	font-weight: normal;
}

Basic PHP Security

March 27th, 2009

Saying “Don’t trust your users” would be a little over statement, but in the world of programming it’s the best mindset. Here are some really basic tips for programming in general (but mostly focusing on PHP).

Validate All Input

It’s very important to check all input to your script; a client could accidentally put a semi-colon in a field and possibly break your code. Always check that the user is posting what you expect. Here are some simple methods to validate input.

<?php
// Input must be a number
if(is_numeric($input)){
	echo 'Input is a number';
} else {
	echo 'Input is not a number';
}

// Input can only contain numbers and letters.
if(preg_match('/([^A-z0-9])/', $input)){
	echo 'Input does not contain only numbers and letters.';
} else {
	echo 'Input contains only numbers and letters.';
}

// Input must be an email
if(preg_match('/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/', $input)){
	echo 'Email Is Valid.';
} else {
	echo 'Email Is Invalid.';
}
?>

Hash Passwords

Hashing passwords is mostly important from a privacy view point; if a hacker gets in they could sell your users details.

Use Sessions, not cookies

Never set sensitive data in cookies, users could edit them and potentially cause problems on your website. Instead use Sessions; they are a server side solution which is a little more secure.

<?php
session_start(); // Start the session. Always put this at the top of your html.

// Set some sessions
$_SESSION['name'] = 'Example 1';

// echo session data
echo $_SESSION['name'];
// Would return Example 1
?>

Read the rest of this entry »

Alternatives to CAPTCHA Image Verification

March 25th, 2009

CAPTCHA Image Verification is one of the worst ideas in the world from usability point of view, users with poor vision or who are fatigued may find complex CAPTCHA difficult.

stupid_captcha1The phrase “You what now?” comes to mind.

Here are some alternatives, which are a little more user friendly.

Logic Test

This method essentially asked your users to solve a simple puzzle, which a bot will find difficult, or CPU intensive to solve. In this simplified example, where we are going to ask the colour of the box (or the circle in the box).

logic_test_exampleFew users will find this test hard

The combination of differentiating between shapes and colours is quite easy for a human to do, but a computer can find this quite difficult. On the other hand though, this is quite work intensive for the programmer and unless you create a wide range of questions, you are a little limited.

View Example

Dummy Fields

In a nutshell, this puts a field which is hidden via CSS which requests a common piece of information (such as email, or message). If the field has been filled in, you can assume that a bot is being used.

Unfortunately, some modern browsers sometimes automatically fill in form for the user, which is a moderate problem.

View Example

Response Timer

This is another method which does not involve users having to do anything. The theory behind this method, is to see how long the user takes to submit the form. If the user takes a short amount of time, they are either super human, or bots. This is relatively full proof as most bots will instantly submit a form.

View Example

JavaScript Extra

This technique assumes that most bots are unable to use JavaScript, so making JavaScript write a little extra piece of information to the form should stop bots. According to the W3C 95% of users have JavaScript turned on, which is good.

View Example

Akismet

This is one of the best methods of stopping spam, essentially Akismet compare what your user has posted with other stuff posted all over the internet. For me, it’s never missed any piece of spam.

Conclusion

Is there a single alternative to do it all? No, but with a combination of all the methods we can reduce the overall amount of spam. In the next example I’ve combined all the above methods (minus Akismet) into a single file which assess weather a user is human on a point system (3 out of 4 will assume human). Feel free to copy it.

View Example | View Class File