My Twitter

Posts Tagged ‘PHP’

5 changes I would like to see in 2010

Wednesday, December 16th, 2009

2009 has been a somewhat interesting year for the internet; it’s become more mobile & more user friendly. However here are a few ideas I have had over the year, which I would like to see arrive in 2010.

Video Stream Caching

One of the big annoyances I have with streaming video files (normally on websites such as YouTube & Megavideo) is whenever I go to jump to another section of the video I lose what I currently have already loaded. It would be really useful if I could quickly go back to the section I’ve previously loaded.

video_stream_caching
This is a mock up I done, the video I used is from RayWilliamJohnson

I’m quite sure this would need to be implemented by the software, but I can’t see how it would be difficult to add.

(more…)

Handling Errors In PHP

Wednesday, June 3rd, 2009

Handling errors in PHP can be quite a handful at times. Here is a really simple PHP class which I use to manage errors:

<?php
/*
errors class - Helps management of errors in a script.

@version
	1.0
@author
	Mike Rogers (FullOnDesin.co.uk)
@last updated
	03 June 2009
@usage
	You are free to share, modify and use this code for commercial or non-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.
*/
class errors {
	var $errors_data;

	/*
	Add the error from $new_error into an array of errors.

	@param
		$new_error	string The text related to your error.
	@return:
		True - Error has been Added
	@example
		add_error('Username is Incorrect');
	*/
	public function add_error($new_error){
		$this->errors_data[] = $new_error;
		return TRUE;
	}

	/*
		Outputs the errors.

	@param
		None
	@return:
		- A div (ID - error) which contains the errors.
		NULL - No errors
	@example
		echo output_errors();
	*/
	public function output_errors(){
		if(is_array($this->errors_data)){
			// Cycle through the errors.
			foreach($this->errors_data as $error)	{
				$return .= '<p>'.$error.'</p>';
			}
		// Add it to the error div
		return '<div id="error">'.$return.'</div>';
		}
		return NULL;
	}
}

// @Example - creating the class:
$errors = new errors;

// @Example - Add an error
$errors->add_error('Username is incorrect');

// @Example - Return the errors
echo $errors->output_errors();
?>

If you are looking to fully integrate a script similar to the above, there is a really good post regarding the set_error_handler() function on Tinsology ( PHP Error Handling ).

Making a simple Facebook Application

Tuesday, May 5th, 2009

In this quick tutorial I’m going to show you how to make a simple “return all” application for facebook. Before you start take a look at the Facebook Developers page and the Facebook Developers Wiki as it’s full of useful stuff.

The Core Components

  • API – Lets you talk to the facebook servers. [put picture here explaining in detail]
  • FBML – Facebook markup language, it’s like a cute little html snippet which facebook turns into normal HTML.
  • XFBML – A javascript which lets you use FBML in iframes.
  • FQL – Lets you run SQL type query’s.
  • FBJS – This changes your javascript so you can only work within a close environment.
  • PHP Client Libraries – The PHP Facebook provides to communicate with their servers.

(more…)

Simple PHP Download Counter

Saturday, April 25th, 2009

Here is a really easy and lightweight way of logging the amount of downloads.

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

## Start defining constants ##
define(LOG_URL, '/home/user/download_logs/'); // Put the location of where you want to put the logs. Make sure this is absolute
# $_GET['ID'] - this is the ID of the file we want. It gets the value from the URL.

## Now set the data you wish to use - this can be moved to an include if you want ##
$file[0] = 'http://www.example.com/file.pdf';

## Define the functions required to update the file.
function update_file($filename, $value){
	if(is_writable($filename) && is_readable($filename)){
		if(file_put_contents($filename, $value)){
			return TRUE;
		}
	}
	return NULL;
}
function pull_file($filename){
	if(is_writable($filename) && is_readable($filename)){
		return file_get_contents($filename);
	}
	return NULL;
}
function rebuild_file($filename){
	if(is_writable($filename) && is_readable($filename)){
		file_put_contents($filename, '1');
	}
}

if(is_numeric($_GET['ID'])){ // Meaning the Data sent is safe.

	// Build log file URL
	$filename = LOG_URL.$_GET['ID'].'.log.txt';
	$value = pull_file($filename);

	header('location:'.$file[$_GET['ID']]);

	// Update logs
	if(is_numeric($value)){
		update_file($filename, ($value+1));
	} else { // Meaning the file does not exist or has been messed with.
		rebuild_file($filename);
	}
} else {
	echo 'Sorry, there was an error.';
}

/* If you want to see how many people have downloaded a file, run something like:
# pull_file(LOG_URL.numberishere.'.log.txt');
/*

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.

I claim no liability for this code, you use it at your own risk.

*/

?>

You can run this by changing the URL that accesses the file. For example:
file.php?ID=0

Feel free to edit and share this code.

5 Tips for Coding Cleaner PHP

Monday, April 6th, 2009

PHP is lovely, though if you ever work with other people having good code is important. Here are some tips to improve your code.

Comment & Document

It’s really important that people understand why you are doing certain things in certain ways. Adding a quick comment above sections of code should be adequate, but documenting classes and functions (Even if it’s in a Wiki) is fantastic.

Give Variables, Functions and Classes Meaningful names

Nothing is worse than trying to figure out what a function called “SIDFE()” does. Give everything a name that if someone else looked at it, they could figure out what it does. The above is a real example I have come across while adjusting a clients website, if the other programmer had called it something like Scan_Incomming_Data_For_Evil() it would have been a lot more straightforward.

White space

whitespaceAs you can see, a little space here and there makes life a lot easier.

No one likes to have to search for the start of a function. Make sure you indent your code and keep it easy to read quickly.

Never Delete – Comment out

This one is a little hard to grasp, but imagine you just fixed a bug (say 100 lines of code to fix it) and something else has broken. It makes sense to be able to go back and see the old code without modification of the new code. Also, doing this helps people see where an old bug was (assuming you comment that the section of code is evil) for future reference.

Use Braces

Braces are those neat } and { things. If you don’t use them on various functions it’s a pain to figure out where a loop starts and finishes. This is especially important when programming on a large scale because no one likes debugging fugly code. Here is an example of good and bad code:

<?php
/* Examples of annoying code */
if ( $coder === 'Silly' ) bang_head(); 

while ( $coder === 'Silly' )
    bang_head();

/* Examples of good code*/
if ( $coder !== 'Silly' ){ Drink_Beer(); }

while ( $coder !== 'Silly' ){
    Drink_Beer();
}
?>

(more…)