- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
How to Email Address confirmation / Verification in PHP
Introduction
In this tutorial, We will develop some php functions which are foremost tasks in email confirmation script. Email confirmation is the most easiest method,
- to verify person's identity,
- to verify whether registrant is human or machine,
- to just simply verify it's email for future conversations, contacts etc.
It is very easy to develop email confirmation script in php. You need not to be a professional for such task. Below are the steps that need to be followed while developing such script.
Steps TO Follow
- First, Generate and store activation key and activation status in database while registration,
- Secondly, Send an email to registrant's email ID containing activation link which will take user to activation page on our site,
- Activation link should contain at-least two parameters in URL, One holding registrant's 'email ID' and second one holding an 'activation key' associated with that ID. For example, "http://example.com/activation.php?email=registrant@gmail.com&key=gd7636gd7363gh8746".
- When, User will land on our activation page, we will fetch those parameters from the activation link and will try finding out such combination in our database,
- And, If such combination found, We will set activation status of associated user to active in our database.
- And Finally, Email has been Confirmed.
Generating Activation Key
Well, It is easy to generate any random key but we will try generating an unique key by using email address of user, See the PHP function shown below that can be used for this purpose.
PHP Function to Generate Activation KEY
function gen_activation_key($email){ $generatedKey = sha1(mt_rand(10000,99999).time().$email); return $generatedKey; }
Sending Confirmation Email to User
Now, after generating and storing 'Activation Key' in our database, We need to send an email to registrant's Email ID containing a link pointing to our activation page. The link should contain two type of parameters,
- email=registrant@example.com,
- key=(activation key), without braces.
See the PHP function below, which would send an confirmation email to registrant (Do changes as per your requirements).
PHP Function for Sending Confirmation Email
function verificationMAIL($gKEY,$email){ $to=$email; $from='do-not-reply@example.com'; $subject='Verify your Email Address - EXAMPLE.COM'; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $mailMsg='<h2>Verify Your Email Address</h1><br /> <a href="http://example.com/activation.php?email='.$email.'&key='.$gKEY.'">Click Here to Verify.</a>'; mail($to,$subject,$mailMsg,$headers); }
Confirm and Activate User
Now, When User will land on our activation page after clicking on activation link, We will fetch those email and key combination from URL and will try finding out such combination in our database. If found one, We'll set Activation status of associated user to 'Active'.
PHP Function to Confirm and Activate User
function verifyEmailAddress($email, $vkey){ msConnect(); $query=mysql_query("SELECT * FROM users WHERE email='$email' AND activation_key='$vkey'"); $fetch=mysql_fetch_array($query); if($fetch['activation_status'] == 'Active'){ $output='Your Email has already been verified.'; }else{ $cnt=mysql_num_rows($query); if($cnt=='1'){ $updateQ=mysql_query("UPDATE users SET activation_status='Active' WHERE email='$email' AND activation_key='$vkey'"); if($updateQ){ $output='Email Verified'; }else{ $output='System Faced an error while updating your status.'; } }else{ $output='Unable to verify your email address.'; } } mysql_close(); return $output; }
Important information about msConnect() function
msConnect() function above is actually a single custom build function (Doesn't exist in PHP functions Library) for connecting to and selecting a database from MySQL, You will be required to create similar one, Click here for more information.