Categorized | News

URL Validation

Posted on 15 August 2004 by Demian Turner

Hi, I am doing an URL validator. It needs to be the best URL validator I ever made – and it is already quite functional.

Validating URLs with PHP

I’d be very happy if you’d go there and try to break it and tell me what went wrong if you do break it.

In return you are free to copy the code for your own use.

Usage is very simple, you construct an array with some key/value pairs – look in the source for which value pairs you can use.

Here’s an example from my testapp

    /* Make the options */
    $options['AssumeProtocol'] = @ $slcAssumeProt;

    $options['AllowBracks'] = isset($chkAllowBracks);

    @ $chkProthttp ?  $options['AllowedProtocols'][] = 'http' : '';
    @ $chkProthttps ? $options['AllowedProtocols'][] = 'https' : '';
    @ $chkProtmailto ? $options['AllowedProtocols'][] = 'mailto' : '';
    @ $chkProtnews ? $options['AllowedProtocols'][] = 'news' : '';
    @ $chkProtnntp ? $options['AllowedProtocols'][] = 'nntp' : '';
    @ $chkProtgopher ? $options['AllowedProtocols'][] = 'gopher' : '';
    @ $chkProttelnet ? $options['AllowedProtocols'][] = 'telnet' : '';
    @ $chkProtfile ? $options['AllowedProtocols'][] = 'file' : '';

    $options['Require']['Protocol'] = $chkReqProtocol ? true : false;
    $options['Require']['User'] = $chkReqUser ? true : false;
    $options['Require']['Password'] = $chkReqPassword ? true : false;
    $options['Require']['Server'] = $chkReqServer ? true : false;
    $options['Require']['Resource'] = $chkReqResource ? true : false;
    $options['Require']['TLD'] = $chkReqTLD ? true : false;
    $options['Require']['Port'] = $chkReqPort ? true : false;
    $options['Require']['QueryString'] = $chkReqQueryString ? true : false;
    $options['Require']['Anchor'] = $chkReqAnchor ? true : false;

EWValidator :: _ValURL($URL, $options)
Bookmark and Share

1 Comments For This Post

  1. acebone Says:

    It is now very good at http style URL’s. I’ve added a little ‘suck up a bunch-o-links from google’ function, and it really seems to validate most of them.

    I’ve come to realise that in order to make it that generic validator I dreamt about, I’ll have to branch validation into different schemes. Something I haven’t got the time to do right now.

    The tricky part however is doing mangled URLs, I must be very unimaginative, a guy called JetLag gave me some very good malformed URLs, well at least it nails them now :)

Leave a Reply

Categories

Books

Demian Turner's currently-reading book recommendations, reviews, favorite quotes, book clubs, book trivia, book lists

Facebook