A short function to extract numbers from a string in PHP using a regular expression.

function extract_numbers($string) {
	preg_match_all('/([\d]+)/', $string, $match);
	return $match[0];
}

This function returns numbers in an array. See The Bit Repository for more details.  Kudos to Gabriel C for a straight forward solution.

 

wiki’s make it easy to edit through the browser. I like everything about them except for two irritating problems.

  • They all look the same
  • They are not easy to customise

Both problems now solved by wikkawiki.

It remains a wiki without the extensive features of the growing groupware solutions such as tikiwiki which are amazing feats of engineering, but only serve to complicate something that is supposed to be easy.

wikkawiki produces excellent documentation, fast.  Setup accounts for your editors then disable login and you remain in control of who gets to edit.

Styling the wiki can be done through CSS plus a header.php and footer.php file. Click here for an example of one with a modified design.

It includes Geshi by default (for colourised code snippets) and you can wirte PHP directly into a page and it will be evaluated at read time.

see http://wikkawiki.org/HomePage for more details.

 

mypph 300x228 Using twitter as an alerting platformI received an email from Clickatell inviting me to top up my SMS account.  After a split second consideration I realised I did not need them anymore.  Sorry Clickatell!

Twitter is more convenient.  I have set up a few private accounts and subscribed to them through my own.  When an event occurs I receive notification direct to my phone as a free SMS message direct from twitter.

Although twitter has been unreliable lately it has proven more convenient than using Clickatell.  In time twitter will fix the problems and they may even be suitable for mission critical solutions.  I hope so because they are easy to use and should spawn many superb product ideas all designed to keep us informed with the information we want now, not just when we visit facebook.




 

homepage 150x150 Payment Processing for OnelogbookOnelogbook is an outdoor sports activity program currently in development. My task was to add Paypal payment processing to the joining form.

Technologies used were PHP, MySQL, Javascript and I created a few minor graphics. Payment information is sent to Paypal for processing. On successful payment Paypal return IPN (Instant Payment Notification) code to the website and the user is authorised to use the website extended features.

Javascript is used for form validation and PHP provides the server side code. If a payment is cancelled user details are removed from memory and the database is not updated (you can see the cancellation notice at the end of this short high speed video).

And a “Please Wait” graphic – scourge of Paypal Payment Processing!

pleasewait Payment Processing for Onelogbook

 

Styleroomz asked for a widget to showcase products. Data is retrieved from their affiliate source as XML in a zip file.

The XML is updated daily to ensure prices and special offers are fresh.

The widget uses HTML, XML, MySQL, Javascript and PHP. Data sources include XML for the affiliate information such as coded links, pictures, pricing and store information, and MySQL used by the Styleroomz ecommerce software to link to their pages. The Yui library from Yahoo was used to create the carousel effect.

The following short video demonstrates how the widget in use.

 

search results 300x230 Redesigning Styleroomz Product SearchI was given the opportunutiy to alter the search results layout for Styleroomz.com, a fashion website for dress sizes between 16 and 20.  The improved search design required alterations to PHP and Javascript.

 

I just helped with some of the Javascript behind a twitter and Google Mashup called Questioon.

questioon Do you have any Questioons?

Currently available only on the .co.uk but Tunde, the owner, intends to update the .com soon.  Some of the features I added include:

  • A default set of 7 searches on the front page (called quick links)
  • Default searches replaces with the last 7 searches you made
  • Ability to delete an individual search
  • Ability to reset all quick links
  • Used Juitter to embed twitter feed on the results page

The design was created by another developer.

 

Bit.ly API documentation http://code.google.com/p/bitly-api/wiki/ApiDocumentation.

Get your API Key http://bit.ly/account/your_api_key
(You will need to create an account if you do not already have one).

$url_to_shorten = urlencode("http://www.youtube.com/watch?v=F-QA2rkpBSY");
 
$bitly_key = "R_86df2938479233454a0a29";   # Replace this with your API key
$bitly_acc = "Your Acount Name";   # Replace this with your bit.ly username
$bitly_qry = "login=$bitly_acc&apiKey=$bitly_key";
$bitly_url = "http://api.bit.ly/v3/shorten?$bitly_qry&longurl=[url]&format=json";
$bitly_url = str_replace('[url]',$url_to_shorten,$bitly_url);
 
$short_json = file_get_contents($bitly_url);
$short_array = json_decode($short_json);
 
echo "Short link is: ".$short_array->data->url;

The use of json_decode limits this to PHP 5.2.0 or better. If you are using anything less check with your host, you may find it can be enabled directly through your control panel.

 

If you would like to redirect browsers based on their IP the following method can be used to handle multiple IP’s.  You can choose to redirect entire networks or a single ip.

<?php
//array of ip's you wish to block.  Note that you can block an
//entire class by replacing it with 0, so to block a class c
//(254 computers) use something like 123.123.123.0
$blockIP = array('123.123.123.0','100.100.100.101');
 
$remote = explode('.',$_SERVER['REMOTE_ADDR']);
foreach($blockIP as $ip) {
  $goodIP = false;
	for($i=0;$i<4;$i++) {
    $ipSeg = explode('.',$ip);
    if($remote[$i] == $ipSeg[$i] || $ipSeg[$i] == '0') {
      //segment qualifies
      $goodIP = true;
    } else {
      //ip no good so move to the next
      $goodIP = false;
      continue 2;
    }
  }
  if($goodIP) {
    //ip passes so no need to check the rest
    $blockThisIP = $ip;
    break;
  }
 
}
//for convenience test $blockThisIP and process here
//replace www.crayola.com with the place you wish to
//send ip's too
if($blockThisIP) {
  //php header method - can only use this if the page
  //has not begin to display in the browser
  header('Location: http://www.crayola.com');
 
  //javascript redirection - use this method if browser has
  //begun to display page
  echo "<script type=\"text/javascript\">
  window.location = \"http://www.crayola.com\";</script>";
}
?>
 

Most of our hosting servers run Centos Linux but we have Windows servers too and a problem common to both is the occasional high volume of traffic generated by non organic growth.  I don’t mean that someones blog hits the front page of digg, rather a malicious or DDOS attack against a website.  On shared hosting this will effect all sites on the server.  There are bandwidth control tools for both operating systems but they all come at a price to CPU or wallet or both.  I have tried all I could find and none of them have been effective.

Some datacenters offer solutions which usually require traffic being filtered before it reaches your server.  This has obvious advantages but comes with one major drawback and that is you have lost control of your network and are relying on unknown parameters setup by your datacenter, and this can result in false positives which may include potential business for your customers

Liquidweb are the only datacenter I have used to date that rely on monitoring service to alert them of a problem plus a human to decide the appropriate reaction.  As they will notify you of changes made or any IP’s blocked you can recover the situation if they block access incorrectly.  This is as close to remaining in control and could be enough for your needs.

Another way is to utilise services built-in to the operating system.  In the attached autoban.zip file I have prepared bash and php files which placed into a cron monitors connections by count and rejects those who try to open too many.  You can also ban countries from accessing your website too.  Parameters allow you to choose how long the ban should be for and you can set levels so that should someone repeatedly be trying to attack the server their ban can be extended.  I find a 1 hour ban followed by a 3 hour, then 6 hour and then a 31 day ban completely eradicates Denial of Service Attacks.

autoban

You will need root access to the server, see readme.txt for instructions.

© 2011 Martyn Walker | Software Architect | Hiker And Hacker Suffusion theme by Sayontan Sinha