Full On Design

Berkshire Based Web Development

 

PHP

Displaying Recent Tweets via Twitter’s RSS

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).

Alternatives to CAPTCHA Image Verification

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

Securing Passwords in PHP

Privacy on your website will be paramount to your success. In the world of application development, security can sometimes be overlooked (normally because accessibility is considered more important). Luckily, modern theories in coding mean we now can now be accessible and secure.

Hashing is a really simple technique to hide data using a one way encryption. It’s especially necessary when dealing with users passwords (In a recent study, 60% of respondents use a similar passwords). Here is an example of how to hash using the MD5 function:

<?php
$password = md5('password');
// $password will now return 5f4dcc3b5aa765d61d8327deb882cf99
?>

However, we can improve on this code. Many hackers now use Rainbow tables to reverse the one-way encryption (and thus find out the secret data). Luckily programmers have come up with a new technique to combat this…Adding a pinch of salt to a hash. In programming terms, a salt is essentially an extra piece of information we add to what the user input to make it unusual. Here is an example how to code this:

<?php
$salt = '%$£Salt_Here*(&^';
$password = md5('password'.$salt);
// $password will now return 5747563a265df7a3250884394c0a05e0
?>

Related Posts

PHP Security Consortium: Password Hashing
Essential PHP Security

Tiny URL API

Believe it or not, TinyURL has an API which allows you to instantly create TinyURL’s of links. Luckily it’s also free to use and currently does not require resistration.

All you need to do is send a request to:

http://tinyurl.com/api-create.php?url=URL_HERE

The code

<?php
echo file_get_contents('http://tinyurl.com/api-create.php?url='.'http://www.example.com/');

/* For example

http://tinyurl.com/api-create.php?url=http://www.fullondesign.co.uk/

Would return:

http://tinyurl.com/d4px9f

*/
?>

Securing Links

Have you ever wanted to secure links on your website (for example hide the real source of a file)? Here is a quick and easy way to do this.

<?php # File created on 11th February 2009 by Mike Rogers (http://www.fullondesign.co.uk/). 

## Start defining constants ## 

define(RUN_ERRORS, TRUE); // Do you want the script to display errors? TRUE = yes you do.

define(redirect_or_echo, 'redirect'); // Do you want to redirect the user to another website, or just echo the other other webpages' content. 'rediect' will redirect, 'echo' will return the web pages constents. I recommend redirect.

## End defining constants ##

/* Start the link codes. The code is the ?code=123 part of the URL. The array should be fotmatted like:

$link['code'] = 'http://URL';

You may find it easier to do this with MySQL or including this as a seperate file. Too many links could lower performance, but for a small website just trying to cloak a few links this is good :) 

*/

$link['1'] = 'http://www.site.com/';

// Start the system.

function external_url($url){

    if($return = @file_get_contents($url)){

        return $return;

    }elseif(function_exists("curl_init")){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        $return = curl_exec($ch);

        curl_close($ch);

        return $return;

    }elseif($return = @implode("", @file($url))){

        return $return;

    } else {

        return NULL;

    }

}

// Checks if the code is a number

if(is_numeric($_GET['code']) && is_array($link)){

    if(isset($link[$_GET['code']])){

        if(redirect_or_echo === 'redirect'){

            header('location: '.$link[$_GET['code']]);

        } elseif(redirect_or_echo === 'echo'){

            echo external_url($link[$_GET['code']]);

        }else{

            if(RUN_ERRORS === TRUE){

                echo 'Sorry, an internal error has occoured.';    

            }

        }

    } else {

        if(RUN_ERRORS === TRUE){

            echo 'Sorry, the code you have provided is incorrect.';    

        }

    }

}else{

    if(RUN_ERRORS === TRUE){

        echo 'Sorry, the code you have provided is incorrect.';    

    }

}

/*

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.

You use this at your own risk.

*/

?>

Download the Code

Useful Links

PHP’s Offical Website – It has a fantastic documentation section. Well worth a look.
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide – The book I learnt PHP from, it’s really good for beginners and reference guide.