Email activation or verification is one requirement when you're building an app with a membership feature. This is one way to detect if there's really a person behind the submitted email address. An email address is considered invalid if no person was able to open it and click the activation link.
Although nowadays, there are some alternative ways to verify the validity of an email address or user, some systems prefer the old school method, like what this post covers.
The alternative way I was talking about is by using a social network login. Facebook, Twitter, Google+ and even Microsoft is providing something called an OAuth (Open Authorization) login, in simple terms, have you ever seen a "Login with Facebook" button?
We see one in StackOverflow login:
Unfortunately, we don't cover OAuth login in this post, but don't be sad, I might do those kinds of post in a simplified way too.
The following steps shows the basic flow how email activation works.
- User fills up your sign up or registration form and submit it to the system.
- System generates unique activation code which acts like a "key"
- System sends a link with the activation code to the email provided during the sign up form.
- User opens his email inbox, found the system email and click the link with the activation code. This is like using the "key" to "unlock the door" which represents your application.
- User was sent to a link saying 'email was activated'
Where are these happening?
To give you a clearer picture where in our code the steps above happens:
Steps 1 to 3 happens in sign_up.php.
Step 4 happens in the user's email provider such as GMail, Y! Mail, etc. User should receive something like this:
Step 5 happens in our activate.php
Alright, so the technologies used in this code are mostly PHP and MySQL. For sending the verification email, we used the PHP mail() function but you can also use a library like PHPMailer if you want to use SMTP such as of GMail.
libs/db_connect.php - for database connection, you know what it looks like, right? Here's the database table structure that can be used, we name it as the 'users' table.
sign_up.php - where the sign up form and processing of user input is located.
activate.php - it has one job, update the the unverified to verified email address.
I provided a download link for you just in case you want to have a copy of the code we discussed. Free code download here.
Please note that this demo is really live, if you enter your email, received an email with the activation link and clicked it, you're subscribed here in our code blog. See the live demo here.
Thanks for reading this email activation PHP script!
For FREE programming tutorials, click the red button below and subscribe! :)