how to create email verification script in php and mysqli

If you want to create a website which you need to create user accounts than you have to verify the user by email address. You can create a very simple email verification script using php and mysqli database. So i will show you how you can do this for your php website. We will follow these simple steps to do so:

  • Create mysqli table for users
  • Create a Register/Login form code
  • Sending Verification Email

Create mysqli table for Users

First we will create a mysqli table for users where. You can use mysql workbench or phpmyadmin to do this. The “users” table will have 4 columns.

  • email
  • password
  • act_status
  • act_token

The column “act_token” will store the unique activation token(used to activate the account) of the user and “act_status” will store the account status. If it is 0 the account is not activated and if it is one the account is activated and user can login,”act_status” will have a  default value of  0.

    Create a Register/Login form code

Now we will create a simple html form for user login and registration. The html code is given below:

Form.html

<html>
<head><title>Register OR Login</title>
<link rel=”stylesheet” href=”style.css” />
</head>
<body>
<form action=”LoginORRegister.php” method=”post”>
<table>
<tbody ><tr><td colspan=”2″><h1>Register OR Login</h1></td><tr>
<tr><td>Email : </td><td><input name=”email” type=”email” placeholder=”Your Email” /></td></tr>
<tr><td>Password : </td><td><input name=”password” type=”password” placeholder=”Your New Password” /></td></tr>
<tr><td><input name=”register” type=”submit” value=”Register” /></td>
<td><span>OR</span><input type=”submit” name=”login” value=”Login” /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

After entering the email and password if user clicks register he will be registered and if login is clicked and the account is created and activated  he will login. A unique activation token will also be stored in the database along with the other information when user will register. This token will be random so user must have to go and click the activation link to enable his account.

Below are some CSS styles for the form.

Sending Verification Email

Now we need to create script for sending  the activation email to the user so that user can proceed with the account verification.The verification email will be sent during the registeration process. The activation link in the email will also contain the activation token of the user. Below is the code to login and register:

In the code above first we have included the PEAR mail API(included in the downloadable source) files which we will use to send the email to user. Then in the if condition we have checked if the register button is clicked and email,password are provided(isset$_POST[’email’],isset$_POST[‘password’])  if yes we will create a unique activation token on line 7 , then we will connect to database(line 8) and save the user’s information along with the token in the database. And after saving user in the database we will send an html email to the user with the activation link on line 17 the sendMail function contains simple script for sending an email in html format.

Else if the login button was clicked and email and password are entered. Check if a user with the same email and password exists in the database and if that account is activated(act_status=’1′) if both of  these are true show a message saying “login Successfull” and if not show an error message. Below is the the simple script for account activation.

When the user clicks the verification link in the email a GET request is made to “Activate.php” with the “actToken” as a parameter. In the above code if the URL contains  the “actToken” the account status of the account with that token is activated by setting the act_status to ‘1’.

Download the source code by clicking the button below.

Download Source

Also Check : learn to create a contact form

So that’s the end of this tutorial. If you have any questions do not hesitate and drop a comment i will be happy to help you.

11 thoughts on “how to create email verification script in php and mysqli

Leave a Reply

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

CommentLuv badge