Osirion System Technologies Header Split Image
  site search:    
   
Osirion System Technologies Header Split Image

reliable & affordable solutions

Our solutions will not only astound you in their reliability, but their affordability as well. Whether you are looking for web hosting, web design or search engine optimisation or any other of our services - you wont be disappointed! No matter which of our solutions you require, you will see this statement will fit all areas of our expertise.
 
 


 
 
 


 
 
 


 
 
We Accept:


articles

Writing Your Own PHP Function Library


2008-03-24

If you do a lot of web development in PHP, you'll soon notice that you run into the same issues time and time again. Instead of porting a bunch of code from other projects, or spending time working on a problem you've already solved, I recommend building a portable library of custom functions. Obviously your function library will consist of different code then mine, but for the sake of illustrating this in practice I'll outline my some of my function library to illustrate the technique. Since I've used this code in several of my commercial projects, I am releasing the code in this tutorial under the MIT license, which allows you to integrate it into your own projects, both commercial and personal, without royalties.

Let's create a file, we'll call it functions.inc.php Inside this file we're going to define our functions and then we simply include them in any page we need them in.

function am_injection($unprotected) {
if (get_magic_quotes_gpc()) {
$unprotected = stripslashes($unprotected);
}
$protected = mysql_real_escape_string($unprotected);
return $protected;
}


What this function does is it cleans up code for insertion into a MySQL database. Because of the call to mysql_real_escape_string(), a MySQL database connection must be open or this function will fail. Data that is passed to am_injection() through the $unprotected variable is cleaned up and returned ready for SQL. This code doesn't work in versions of PHP lower than 4.3 (not that any web development project should be targeting those versions.) One way to fix that is to put this function in at the beginning of the file:

if (!function_exists("mysql_real_escape_string")) {
function mysql_real_escape_string($string, $null=NULL) {
return mysql_escape_string($string);
}
}


This recreates the mysql_real_escape_string function if it doesn't exist.

Now the technique that I've shown you allows you create any function and call it from anywhere in your web development project. You can also do this with object orientated PHP and define classes in this file instead. Either way, once you start creating your own libraries of reusable code, you'll soon find yourself with a lot more free time. You can thank me later.



View other articles in the Web Development category.
View other articles in the PHP sub-category.
Copyright © 2002-2024 Osirion System Technologies. All Rights Reserved.
Privacy Policy | Terms & Conditions | Terms Of Service | Sitemap