E-mail validation with PHP

No picture

About this article: Posted in: Learned along the way
By: Blockcoder ( Admin ) / 16.10.2011
Stats: one response / Views: 1,488
Tags: , , ,

How to make bitcoin online.

Preparations

Hello everyone, this is again a short answer blog post — which is typical to me — and the today’s topic will be “How to validate e-mail address with PHP?“. Off topicly, I’m now wondering when we are doing our first Blockcoders LONG answer. Well, maybe that day will come soon enough.. but now to the point!

What I am assuming you have already:

  • A website where is a form which receives e-mail addresses
  • A .php file which consists of php functions
  • A function which sends the given information into a database, maybe
  • Same function checks all the information which are coming from the form

You DON’t have a working function that checks the e-mail addresses, I assume.


Introducing the PHP filters

Have you ever heard of PHP filters? Maybe not? Well, if you want really easy way to validate an e-mail address, you should take your time with the filters. Yet — you don’t need to search about them because I am now gonna tell you how to use two of them.

First a little theory lesson. There are php functions called: filters. These functions filters data which is coming from insecure places like from a user input. In this block of code we are goin to use “filter_var()” function which filters a single variable ( this case a sent e-mail address ).

There are two main filters which validates email addresses:

  • FILTER_SANITIZE_EMAIL
  • FILTER_VALIDATE_EMAIL

The explanations

I will now explain you what these two main functions does:

FILTER_SANITIZE_EMAIL, filter removes a dozen of illegal characters like ( $-_.+!*’{}|^~[]`#%/?@&= ) from a string sent by user.

FILTER_VALIDATE_EMAIL, validates the user sent string as an e-mail address. — it just mysteriously validates it.


An example of use

Let’s take a look at a living example of how to use the filters:

function validateEmail( $email )
      {
           //first we want to remove all illegal characters from the string
           $email = filter_var( $email, FILTER_SANITIZE_EMAIL );

          //next we want to validate the string as an email-address.
          if(filter_var( $email, FILTER_VALIDATE_EMAIL ))
            {
                return TRUE;
            }
          else
            {
                return FALSE;
            }
      }

Finally, if this function returns true, sent string is an e-mail. If not, string is not an email address.
It is that simple.

However, remember that these functions doesn’t grant all the wishes you would want. These just checks if the string is in the shape of e-mail – and removes the forbidden characters from it. But yet, it helps.

Thanks folks, lets do more codeblocks!


There are 1 responses on this post

Email Addiction
It's hard to find knowledgeable people on this topic, but you sound like you know what you're talking about. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

13,864 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>