Archive for the ‘code’ Category

04/29/13
Paul Savage
tags:  

Jobberbase email validation


I’ve written before about a few tweaks to jobberbase I’ve done :

Today a new small issue came up, where a client wanted to use a .INFO email address, but was being blocked for submitting it. The error returned by jobberbase said that it wasn’t a valid email address.

Poking around in the code I came across the file located in /_includes/function.validate_email.php the function contained here returns true if it’s a valid email address. The original version was a RegEx that said

if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[_a-z0-9-]+)*(\.[a-z]{2,3})$/i", $string))

The 2,3 is the interesting part here at the end, it limits the number of expected characters. Generally email addresses have 2 or 3 characters at the end, for example .IE or .COM, but in the case of .INFO or other domains that are longer than 3 characters it will fail. This code can be fixed by increasing the number from 2,3 to something higher. If you only want to allow 4 character domain names then this will do the trick

if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[_a-z0-9-]+)*(\.[a-z]{2,4})$/i", $string))

02/26/13
Paul Savage
tags:  

Jobberbase – 404 error code for removed jobs


One slight issue with jobberbase.org is that when you or clients remove an ads, they are redirected to a notice to say that the page is not available. This in itself is fine, but the issue comes when you should be taking this page properly out of the index, and telling search engines that this page is gone. Out of the box jobberbase sends a HTTP status of :

HTTP/1.1 200 OK

but you want

HTTP/1.0 404 Not Found

Code changes

This can quickly be fixed by changing one file, jobberbase_main_directory/index.php find
case 'job-unavailable':
$html_title = 'Unavailable job / ' . SITE_NAME;
$template = 'no-job.tpl';
$flag = 1;
break;

and replace it with

case 'job-unavailable':
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");

$html_title = 'Unavailable job / ' . SITE_NAME;
$template = 'no-job.tpl';
$flag = 1;
break;

now when searchengines check

/job-unavailable/

they will get the correct HTTP status.

01/29/13
Paul Savage
tags:   ,

Blocking IP ranges with PHP


Recently I wanted to block certain ranges of IP address via PHP from visiting a website. I discovered a handy function ip2long, which can take incomplete ranges of IP addresses. Here is an example


<?php
// by BlackDog 20130129
$location = 'http://www.male.ie/'; // where to send bad people

$range_low = ip2long("100.100.100.100");
$range_high = ip2long("200.200.200.200");
// AAA.BBB.CCC.DDD

$ip = ip2long($_SERVER['REMOTE_ADDR']);
if ($ip >= $range_low && $ip <= $range_high) {
// what to do if in bad IP range
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
}
else {
// do something else or nothing at all
}
?>

The code above will block anyone from IP ranges 100.100.100.100 through to 200.200.200.200, and will redirect them to another website .

Other ways

This is quite a quick and simple way of achieving the goal of block a range of IP addresses. Other ways would be to perhaps have a database table with a list of bad IPs, and do a query per page request to see if they are white-listed. There is also a good article about saving IP addresses to databases that’s worth reading. This code will probably have to be updated once IPv6 comes more and more popular, but for the moment only works for IPv4.

11/23/12
Paul Savage
tags:   ,

Yoast’s SEO WordPress plugin for sitemaps without htaccess


Recently, for a client, I started using Yoast’s SEO for WordPress for their blog. Everything was running nicely until we hit a snag with the .htaccess redirect for the sitemap. The client’s website, uses lighttpd, and therefore it doesn’t have all the .htaccess functionality out of the box. With Yoast’s SEO plugin your file will appear at /sitemap.xml [see mine here], which is called via htaccess. Instead of hacking the server to get the XML sitemaps working, I decided to link to the non-rewritten sitemap URL. After searching on the net and not finding any answers, I decided to delve into the code, and below is my findings.

Linking directly to the individual sitemaps

Poking into the code I’ve found out that the links appear as

  • Page www.youblog.com/index.php?sitemap=page&sitemap_n=1
  • Posts www.youblog.com/index.php?sitemap=post&sitemap_n=1
  • Category www.youblog.com/index.php?sitemap=category&sitemap_n=1
  • Post Tags www.youblog.com/index.php?sitemap=post_tag&sitemap_n=1
These link to the posts, pages and less importantly to the categories and tag pages. In each of these cases you may need to increment the parameter sitemap_n=, if you have more than  1000 posts (you shouldn’t really have more than 1000 categories or post tags). The plugin limits the number of  URLs listed to 1000 per individual sitemap,  most likely speed.
More details about what the SEO  plugin for WordPress does can be seen here on Yoast’s site.
03/09/12
Paul Savage
tags:   ,

WordPress Comment Error


Earlier today I had a strange issue with my blog where comments weren’t coming through. It’s not like I get a  lot of comments but I noticed something was off with the emails the blog sends when a comment gets posted. Basically the details of the comment was blank. See here for a typical email :

Author :  (IP:  , )
E-mail :
URL    :
Whois  : http://whois.arin.net/rest/ip/
Comment:

Doing a bit more testing, I tried to post a comment myself, to see if it mattered if I was logged in or not. Sadly this didn’t make a difference. But I did notice that the URL that it was posting to was strange

/comment-page-/#comment-

After a bit of poking, and looking at the error log it was an issue with a corrupt table. The simple fix was that the wp_commentmeta had crashed, and you’ll need to run the repair procedure on the table.  Maybe you’ve just had this issue and you are lost for answers on how to fix it. Hopefully you’ll find this helpful.

06/21/11
Paul Savage
tags:  

How to reset Facebook Cache


If you are deep in developing your app, and the time comes to look at Facebook and OpenGraph, you might find yourself on a stumbling block I came across last night. The annoying part happens due to the fact that Facebook caches the results of your page. And this really gets in the way of testing changes. In fact I’m not really sure how long this cache even lives for. What I’ve found to be quite helpful Facebook’s URL Linter. This forces a reload of your data and removes the cached copy, and you will be able to see the changes in your app a lot sooner. The tool doesn’t just reload cache, rather it’s there to let you know if you have any issues with your code.

Perhaps I’m the wrong type of developer, but I sometimes like to see the minor changes in my code I test as I go along. I’m not able to visualise multiple changes so easily. So if you are like me, give this a whirl ! We found this tool via a post from developer blog post by Paul Tarjan this time last year.

Edit : see below in the comments for a better alternative.