Planet PHP

2 October, 2008 | PHP Websites | No comments

Planet PHP is a feed aggregator blog specifically for PHP related blogs. There are a good number of blogs that the site uses to update with so there is always plenty of stuff going up on the site. The site is not run by PHP, and is an independent project run by Christian Stocker and Tobias Schlitt.

Manage MySQL Databases With phpMyAdmin

The good thing about this site is that it saves having to go through all of the blogs in turn to see what is interesting in the world of PHP. If you run a PHP blog you can submit your site to the list and get your articles included in the updates.

Writing Function Code To Be More Readable

1 October, 2008 | General | No comments

Last month I started writing functions in a particular way, which has made my life as a programmer much easier on more than one occasion. No matter how many comments or verbose parameter names you put in you can end up writing code that you will get lost in. The reason is simple. Lets say you had a function that took in a couple of parameters.

function myFunction($intNum1,$intNum2){
 // function does something
}

Normal practice is to check the parameters to make sure that they are what you expected them to be before continuing on with the rest of the function. Lets say that we only want the numbers to be in a range. If they are not in the range the function should return false. Many programmers might start of writing something like this.

function myFunction($intNum1,$intNum2){
 if($intNum1 > 0 && $intNum1 < 1000){
  if($intNum2 > 0 && $intNum2 < 1000){
   // do something with the numbers.
   return $value;
  }else{
   return false;
  }
 }else{
  return false;
 }
}

This code will still produce the desired effect of checking to see that the numbers are within a range, but there is a simpler and more elegant way of doing this. For example, what if you wanted to add a third parameter, or a fourth? Having nested if statements is hard enough, but when you get to four levels things get a bit silly.

Here is the same code, but refactored in a way that allows for better readability and maintenance. Notice that the if statement logic has been reversed.

function myFunction($intNum1,$intNum2){
 // check parameter 1
 if($intNum1 < 0 && $intNum1 > 1000){
  return false;
 }
 // check parameter 2
 if($intNum2 < 0 && $intNum2 > 1000){
  return false;
 }
 // do something with the numbers.
 return $value;
}

This way, we get past all of the logic checking before getting down to what we want the function to do, and it is readable. Also, if you wanted to add another parameter you would just add another if statement to check that parameter. There if no juggling of nested statements or complicated syntax.

The above function also doesn’t take into account the fact that the parameters might not be numbers, additional checking can be added at the top of the function to account for this.

I have posted this in the general category because I have used this technique in at least 3 different languages since I started doing it in PHP. All of the code written here is in generic PHP like syntax, but the idea stands. Hopefully, you should get something out of this as well.

Simple Swear Filter In PHP

30 September, 2008 | PHP Strings | No comments

Use the following function to filter out words from user input. It works by having a pre-set array of words that are to be excluded, this array is then looped through and each item is used to replace any instances of that word within the text. The regular expression uses the \b character class, which stands for any word boundary. This way you don’t get the middle of words being filtered out when they are not meant to be.

By using the e of the preg_replace function it is possible to run PHP functions within the output. In this case we count the number of characters found in the replace and use this to create a string of stars (*) of equal length.

function filterwords($text){
 $filterWords = array('gosh','darn','poo');
 $filterCount = sizeof($filterWords);
 for($i=0; $i<$filterCount; $i++){
  $text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('*',strlen('$0'))",$text);
 }
 return $text;
}

When the following text is run through this function.

echo filterwords('Darn, I have a mild form of torretts, poo!');

It produces the following result.

****, I have a mild form of torretts, ***!

Search Engine Spider Detection With PHP

29 September, 2008 | PHP | No comments

Part of any search engine optimisation strategy should always be that the user and the search engine see the same thing. If you start delivering different content you will either end up not performing or just getting outright banned. However, there are certain circumstances where you will want to detect the presence of a search engine spider. For example, lets say that you had a link to a section of your site, and you wanted to add a counter to it that registered an action every time a user clicked on the link. One way of doing this would be to add a parameter to the URL of the link, if the parameter is present then it is a user going through the site and so the action will be registered. You don’t want to register every time a search engine bot spiders the site so using the following function will allow you to turn off this parameter for these spiders.

function spiderDetect() {
 $agentArray = array("ArchitextSpider", "Googlebot", "TeomaAgent",
  "Zyborg", "Gulliver", "Architext spider", "FAST-WebCrawler",
  "Slurp", "Ask Jeeves", "ia_archiver", "Scooter", "Mercator",
  "crawler@fast", "Crawler", "InfoSeek Sidewinder",
  "almaden.ibm.com", "appie 1.1", "augurfind", "baiduspider",
  "bannana_bot", "bdcindexer", "docomo", "frooglebot", "geobot",
  "henrythemiragorobot", "sidewinder", "lachesis", "moget/1.0",
  "nationaldirectory-webspider", "naverrobot", "ncsa beta",
  "netresearchserver", "ng/1.0", "osis-project", "polybot",
  "pompos", "seventwentyfour", "steeler/1.3", "szukacz",
  "teoma", "turnitinbot", "vagabondo", "zao/0", "zyborg/1.0",
  "Lycos_Spider_(T-Rex)", "Lycos_Spider_Beta2(T-Rex)",
  "Fluffy the Spider", "Ultraseek", "MantraAgent","Moget",
  "T-H-U-N-D-E-R-S-T-O-N-E", "MuscatFerret", "VoilaBot",
  "Sleek Spider", "KIT_Fireball", "WISEnut", "WebCrawler",
  "asterias2.0", "suchtop-bot", "YahooSeeker", "ai_archiver",
  "Jetbot"
 );
 
 $theAgent = $_SERVER["HTTP_USER_AGENT"];
 
 for($i=0;$i<count($agentArray);$i++){
  if(strpos(" ".strtolower($theAgent), strtolower($agentArray[$f]))!= false){
   return true;
  };
 };
 return false;
}

The function works by finding the current user agent string of the visitor and the comparing it to the list of user agents in an array. If the user agent is found then true is returned, otherwise the return value is false.

With this function present you can now include an if statement to see if the user agent is a search engine spider or not.

if(spiderDetect()){
 // do something for spiders
}else{
 // do something for users
}

Please be careful with this function. If you server different content to users and search engine spiders you will more than likely get banned for your efforts.

Also, this might be an incomplete list of search engine spider user agents, if you know any more then please write a comment and I will add them onto this list.

Directory Iteration With DirectoryIterator() In PHP 5

26 September, 2008 | PHP OOP | No comments

The normal way of looping through a directory is to get a handle on the directory, then go through each item, making sure that the file is readable and is not "." or ".." before doing something with the file. Because this is done a lot the DirectoryIterator() object was created in PHP5 to simplify this process.

The constructor of the DirectoryIterator() object takes a single parameter, this is the path of the directory that is to be iterated over.

$dir = new DirectoryIterator('/');

To get the current working directory you can use the PHP function getcwd(). This will return the directory that the PHP script is being run from.

$dir = new DirectoryIterator(getcwd());

The previous bit of code will open up the root directory of your server. If you are on a Linux server you will see directories like dev, var, srv, etc, lib, home, root, sbin and usr.

With the object now created there are some different functions that can be used to look at different things. Here is a small list of what can be used:

  • isDot(): This function is used to see if the file is a "." or a "..".
  • isReadable (): Checks if the file is readable. If it is then it returns true, otherwise it is false.
  • next(): Moves the iterator onto the next item in the list.
  • valid(): This function checks to see if there is anything in the directory that can be iterated over. Basically, this function returns false if the iterator is at the end of the directory.
  • current(): Returns the name of the current file.

These functions can be put together in the following way. This snippet of code will print out all of the files in a directory.

while($dir->valid()){
 if(!$dir->isDot()){
  print $dir->current()."<br />";
 }
 $dir->next();
}