PHP
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 ).
Posted June 3rd, 2009
This is a Q&A section where I answer some questions I have been asked.
Dealing With XML
Question: Is there a quick and easy built in function in PHP to convert XML to an array?
In short yet, assuming the XML your trying to sieve through is not too complex the simplexml_load_file() function should be able to handle your needs.
Telling a friend they got ‘had’
Question: A colleague of mine recently went about starting his own .com, unfortunately the Designer/Coder they used was awful (for some reason, they did not ask me as well). Basically they paid (a large sum of money) to have a wordpress blog with a tacky (and invalid) design slapped on top of it. Should I tell them they got ripped off, and if yes how should I go about it?
This is quite a sensitive subject, on the one hand they have been seriously ripped off (unclosed tags is not forgivable), but on the other hand they also may not take too kindly to finding out that their website is about as useful as a burning boat.
I suggest showing your friend his website in another web browser, generally if the coding sucks it should render incorrectly. Hopefully the little nudge in the right direction should set the ball rolling in terms of fixing the problems. If they don’t fix it (or ignore the problem), their loss.
Posted May 29th, 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.
Read the rest of this entry »
Posted May 5th, 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.
Posted April 25th, 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
As 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();
}
?>
Read the rest of this entry »
Posted April 6th, 2009