I'm a Designer, graphic and web. Here I muse about things that inspire me, frustrate me, teach me and are of me... related to design.


Monday, June 6, 2011

PHP 5.3.0 Broke My Contact Form

Yeah, so one of my former class mates, Krissy Morley, was playing around with her website, which has a contact form and she discovered that it was suddenly giving her PHP errors. She hadn't touched it, so qu'est-ce qui ce passe? Knowing that the rest of us in our program used similar PHP validation, she went to our websites and found that our contact forms gave the same errors. What the heck, right?

Yeah, well, it turns out that if you upgrade to PHP 5.3.0 there are certain functions that are now deprecated. The one that was giving me issues, and probably the same one that was giving Krissy issues, was eregi(). I used it to validate the name and email fields in my contact form

How did I fix it? It was actually 100 times easier than I thought it would be. I just Googled the error I was getting and found this handy dandy article:

It suggests replacing eregi() with preg_match() and also tells you a whole bunch of other PHP functions that are now deprecated. 

One thing I found when I did this switch is that the article tells you to use the i modifier, which makes it case sensitive. However, I didn't want that in my validation, so I just omitted it without any problems. 

Another thing I noticed is that with eregi(), there's no / before the ^, as in this: '^[a-zA-Z\' -.]+$/'. 

However, preg_match() needs the /, so I just added it in, so it became '/^[a-zA-Z\' -.]+$/'

If that's confusing, this is what my name and email validation now looks like as a whole:

// Name validation
function a2z( $string )
{
    $pattern = '/^[a-zA-Z\' -.]+$/';

    if (preg_match( $pattern, $string ) )
    {
        return true;
    }
    else
    {
        return false;
    }  
}
//Email validation
{
    $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/";

    if (preg_match( $pattern, $email) )
    {
        return true;
    }
    else
    {
        return false;
    }  
}

Hope that helps!