-->

Login and Sign up Form with Email Verification in PHP/MySQLi

This tutorial will teach you on how to send email/verification email in PHP/MySQLi using mail() function. The function will both work in local server(ex. XAMMP) or hosted server(online) but in case you wanted to send email via the local server, you need to modify your local server to allow it to send email. In my case, I'm using XAMMP, so, I'm going to show you how to set up your XAMMP to send email.

Setting up Local Server to Send Email

Configure php.ini
1. Open php.ini in C:\xampp\php\php.ini.
2. Find extension=php_openssl.dll and remove the semicolon from the beginning of that line.
3. Find [mail function], enable the ff. by removing the semicolon at the start of the line then, change their values:
  • SMTP=smtp.gmail.com
  • smtp_port=465
  • sendmail_from = ndevierte@gmail.com
  • sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
php.iniConfigure sendmail.ini
1. Open sendmail.ini in C:\xampp\sendmail\sendmail.ini
2. Find [sendmail], enable the ff. by removing the semicolon at the start of the line, then change their values:
sendmail.inisendmail.ini2
Lastly. make sure to turn this on to allow XAMMP to send email to your email. Use this link.
And you're done. Restart XAMMP and you should now be able to send email using your XAMMP.

Working Example

To test if your XAMMP is working, here's our example. We will create a registration with verification email.
NOTE: Scripts and CSS used in this tutorial are hosted, so, you might need internet connection for them to work.

Creating our Database

First step is to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as sample.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `email` VARCHAR(50) NOT NULL,
  4. `password` VARCHAR(50) NOT NULL,
  5. `code` VARCHAR(20) NOT NULL,
  6. `verify` INT(1) NOT NULL,
  7. PRIMARY KEY(`userid`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
sendmail

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.
  1. <?php
  2. $conn = mysqli_connect("localhost","root","","sample");
  3. if (!$conn) {
  4. die("Connection failed: " . mysqli_connect_error());
  5. }
  6. ?>

index.php

This contains our login form but you can access a link here if you want to sign up.
  1. <?php
  2. include('conn.php');
  3. if(isset($_SESSION['id'])){
  4. header('location:home.php');
  5. }
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <title>Sign up Form with Email Verification in PHP/MySQLi</title>
  11. <script src="<a href="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  12. " rel="nofollow">https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></scri...</a> <script src="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  13. " rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></s...</a> <link href="<a href="https://fonts.googleapis.com/css?family=Open+San">
  14. " rel="nofollow">https://fonts.googleapis.com/css?family=Open+San">
  15. </a> <link href="<a href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  16. " rel="nofollow">https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min....</a> <link rel="stylesheet" href="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></a>
  17. <style>
  18. #login_form{
  19. width:350px;
  20. position:relative;
  21. top:50px;
  22. margin: auto;
  23. padding: auto;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="container">
  29. <div id="login_form" class="well">
  30. <h2><center><span class="glyphicon glyphicon-lock"></span> Please Login</center></h2>
  31. <hr>
  32. <form method="POST" action="login.php">
  33. Email: <input type="text" name="email" class="form-control" required>
  34. <div style="height: 10px;"></div>
  35. Password: <input type="password" name="password" class="form-control" required>
  36. <div style="height: 10px;"></div>
  37. <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button> No account? <a href="signup.php"> Sign up</a>
  38. </form>
  39. <div style="height: 15px;"></div>
  40. <?php
  41. if(isset($_SESSION['log_msg'])){
  42. ?>
  43. <div style="height: 30px;"></div>
  44. <div class="alert alert-danger">
  45. <span><center>
  46. <?php echo $_SESSION['log_msg'];
  47. unset($_SESSION['log_msg']);
  48. ?>
  49. </center></span>
  50. </div>
  51. <?php
  52. }
  53. ?>
  54. </div>
  55. </div>
  56. </body>
  57. </html>

signup.php

This is our sign up form.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Sign up Form with Email Verification in PHP/MySQLi</title>
  5. <script src="<a href="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  6. " rel="nofollow">https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></scri...</a> <script src="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  7. " rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></s...</a> <link href="<a href="https://fonts.googleapis.com/css?family=Open+San">
  8. " rel="nofollow">https://fonts.googleapis.com/css?family=Open+San">
  9. </a> <link href="<a href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  10. " rel="nofollow">https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min....</a> <link rel="stylesheet" href="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  11. <style>
  12. " rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  13. ...</a> #signup_form{
  14. width:350px;
  15. position:relative;
  16. top:50px;
  17. margin: auto;
  18. padding: auto;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div id="signup_form" class="well">
  25. <h2><center><span class="glyphicon glyphicon-user"></span> Sign Up</center></h2>
  26. <hr>
  27. <form method="POST" action="register.php">
  28. Email: <input type="text" name="email" class="form-control" required>
  29. <div style="height: 10px;"></div>
  30. Password: <input type="password" name="password" class="form-control" required>
  31. <div style="height: 10px;"></div>
  32. <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Sign Up</button> <a href="index.php"> Back to Login</a>
  33. </form>
  34. <?php
  35. if(isset($_SESSION['sign_msg'])){
  36. ?>
  37. <div style="height: 40px;"></div>
  38. <div class="alert alert-danger">
  39. <span><center>
  40. <?php echo $_SESSION['sign_msg'];
  41. unset($_SESSION['sign_msg']);
  42. ?>
  43. </center></span>
  44. </div>
  45. <?php
  46. }
  47. ?>
  48. </div>
  49. </div>
  50. </body>
  51. </html>

register.php

This contains our signup code as well as our send verification email code.
  1. <?php
  2. include('conn.php');
  3. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  4. function check_input($data){
  5. $data=trim($data);
  6. $data=stripslashes($data);
  7. $data=htmlspecialchars($data);
  8. return $data;
  9. }
  10. $email=check_input($_POST['email']);
  11. $password=md5(check_input($_POST['password']));
  12. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  13. $_SESSION['sign_msg'] = "Invalid email format";
  14. header('location:signup.php');
  15. }
  16. else{
  17. $query=mysqli_query($conn,"select * from user where email='$email'");
  18. if(mysqli_num_rows($query)>0){
  19. $_SESSION['sign_msg'] = "Email already taken";
  20. header('location:signup.php');
  21. }
  22. else{
  23. //depends on how you set your verification code
  24. $set='123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  25. $code=substr(str_shuffle($set), 0, 12);
  26. mysqli_query($conn,"insert into user (email, password, code) values ('$email', '$password', '$code')");
  27. $uid=mysqli_insert_id($conn);
  28. //default value for our verify is 0, means it is unverified
  29. //sending email verification
  30. $to = $email;
  31. $subject = "Sign Up Verification Code";
  32. $message = "
  33. <html>
  34. <head>
  35. <title>Verification Code</title>
  36. </head>
  37. <body>
  38. <h2>Thank you for Registering.</h2>
  39. <p>Your Account:</p>
  40. <p>Email: ".$email."</p>
  41. <p>Password: ".$_POST['password']."</p>
  42. <p>Please click the link below to activate your account.</p>
  43. <h4><a href='<a href="http://localhost/send_mail/activate.php?uid=" rel="nofollow">http://localhost/send_mail/activate.php?uid=</a>$uid&code=$code'>Activate My Account</h4>
  44. </body>
  45. </html>
  46. ";
  47. //dont forget to include content-type on header if your sending html
  48. $headers = "MIME-Version: 1.0" . "\r\n";
  49. $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  50. $headers .= "From: <a href="mailto:webmaster@sourcecodester.com" rel="nofollow">webmaster@sourcecodester.com</a>". "\r\n" .
  51. "CC: <a href="mailto:ndevierte@gmail.com" rel="nofollow">ndevierte@gmail.com</a>";
  52. mail($to,$subject,$message,$headers);
  53. $_SESSION['sign_msg'] = "Verification code sent to your email.";
  54. header('location:signup.php');
  55. }
  56. }
  57. }
  58. ?>

activate.php

The goto page from registered email. This is our verification page.
  1. <?php
  2. include('conn.php');
  3. if(isset($_GET['code'])){
  4. $user=$_GET['uid'];
  5. $code=$_GET['code'];
  6. $query=mysqli_query($conn,"select * from user where userid='$user'");
  7. $row=mysqli_fetch_array($query);
  8. if($row['code']==$code){
  9. //activate account
  10. mysqli_query($conn,"update user set verify='1' where userid='$user'");
  11. ?>
  12. <p>Account Verified!</p>
  13. <p><a href="index.php">Login Now</a></p>
  14. <?php
  15. }
  16. else{
  17. $_SESSION['sign_msg'] = "Something went wrong. Please sign up again.";
  18. header('location:signup.php');
  19. }
  20. }
  21. else{
  22. header('location:index.php');
  23. }
  24. ?>

login.php

Our login code that will check if the user has successfully sign up and verified its account.
  1. <?php
  2. include('conn.php');
  3. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  4. function check_input($data) {
  5. $data = trim($data);
  6. $data = stripslashes($data);
  7. $data = htmlspecialchars($data);
  8. return $data;
  9. }
  10. $email=check_input($_POST['email']);
  11. $password=md5(check_input($_POST['password']));
  12. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  13. $_SESSION['log_msg'] = "Invalid email format";
  14. header('location:index.php');
  15. }
  16. else{
  17. $query=mysqli_query($conn,"select * from user where email='$email' and password='$password'");
  18. if(mysqli_num_rows($query)==0){
  19. $_SESSION['log_msg'] = "User not found";
  20. header('location:index.php');
  21. }
  22. else{
  23. $row=mysqli_fetch_array($query);
  24. if($row['verify']==0){
  25. $_SESSION['log_msg'] = "User not verified. Please activate account";
  26. header('location:index.php');
  27. }
  28. else{
  29. $_SESSION['id']=$row['userid'];
  30. header('location:index.php');
  31. }
  32. }
  33. }
  34. }
  35. ?>

home.php

This page will show if the user that login is verified.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Sign up Form with Email Verification in PHP/MySQLi</title>
  5. <script src="<a href="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  6. " rel="nofollow">https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></scri...</a> <script src="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  7. " rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></s...</a> <link href="<a href="https://fonts.googleapis.com/css?family=Open+San">
  8. " rel="nofollow">https://fonts.googleapis.com/css?family=Open+San">
  9. </a> <link href="<a href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  10. " rel="nofollow">https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min....</a> <link rel="stylesheet" href="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></a>
  11. </head>
  12. <body>
  13. <div class="container">
  14. <h2>Login Successful</h2>
  15. <p><a href="logout.php">Logout</a></p>
  16. </div>
  17. </body>
  18. </html>

logout.php

Lastly, our logout that will destroy our login session.
  1. <?php
  2. header('location:index.php');
  3. ?>
That ends this tutorial. If you have any questions or comment, feel free to write it below or message me. Happy Coding :)