25 PHP Form Validation Snippets

Recently i have involve myself in another application development. Regular Hungred Dot Com visitors will notice that the site currently offer advertisement space through this form. But really, we as a developers are always looking for such snippets or writing them out from scratch every single time regardless of how many time we know we have store it somewhere in our laptop! Man, its really frustrating searching on Google and find all sort of solution and trying to figure out whether the regular expression implemented is expensive or complete. So i came out with an idea to ease my life a bit and other developers by putting up an article such as this for my/our references. (This can be made into a class if you like to)

Validate Email

We can perform an email validation through this function.

14 thoughts on “25 PHP Form Validation Snippets

  1. Pingback: php-html.net
  2. @FormatiX: filter_var is indeed a very good filtering function, I have it already in some production sites where the hosting offers PHP > 5.2.0.

    An easy way to avoid SQL inyection is using the mysqli connector, only available in PHP > 5.0.0. It is the new connector that replaces the old mysql connector. It haves support for multiqueries, OOP and some bunch of other stuff.
    The idea beyond this is that you can use parametrized query, evaluating the type of data (string, integer, float) and passing that to MySQL. Any attempt to inyecting strange code will be threated as a string, and thus will have no negative effects on your database.

    This is a basic idea:
    $dbLink->query('INSERT INTO users (sUser,sPasswd) VALUES (?,?)','unreal4u','password');

    I'm currently writing a class that deals with this and hoping to publish it soon on phpclasses.org. Only bad thing is the lack of available time xD

    Nice post btw... only thing i could add is that in order to validate that an image or URL exist, you could also use cURL, and dealing only with the HTTP headers. It is faster than file_get_contents and normally available in scenarios where allow_url_fopen is disabled in php.ini because of security issues. Only bad thing is that it requires PHP > 5.0.0 (well, maybe, i haven't tested any of my scripts on PHP4).

    Anyway... I see absolutely no reason to stick with PHP4 when PHP5.2 is absolutely better.

    Greetings 😉

  3. @unreal4u : as usual love your comment! You are right curl is better and faster but for people who do not use curl, this is a good solution! Thanks for the comment 🙂

  4. @Harendra Kumar: Thanks
    @Ash Blue: Crap. I haven't updated yet but i'm still using the above code on PHP5.2 . Although it is depreciated but its still usable. ( or else those who upgrade from PHP4 to PHP 5.2 will have a lot of problem with all their website/webportal)

  5. @Ash Blue: it will be really depreciated in PHP6 (AKA raise an E_USER_DEPRECIATED error), for now it is only listed as depreciated in the PHP manual, but it works without problems 😉

    @Clay: long time no see xD

    Greetings !

  6. Oops, more updates: bad news, just checked and eregi (with their similars) is already generating an E_DEPRECATED (note: not an E_USER_XXX, but a full PHP one) error:

    Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice.

    eregi

    Greetings !!

Comments are closed.