Email OTP Verification using PHP in Live Server
The task is to create and design a sign-up and login form. In the sign-up form, the user will sign-up with a custom username and password and a valid email then the user will receive an OTP through the email, and after successful verification of OTP user account will be created and data will be stored in MySQL database, and then the user will be redirected to the home page. In the login form, the user can login with the username and password that the user entered while creating the new account.
Note: We will implement this whole thing in a live server, anyone can implement this to their own local server like XAMPP, but the email verification part will not work in the local server.
Approach for Sign-up Form:
- The first task is to create a MySQL server Database and a Table according to our requirements.
- We connect our MySQL server Database using PHP mysqli_connect() function that takes four arguments, i.e. our “servername”, “username”, “password” and “database”.
- After entering all the details of the user, we will generate a 6 digit random number using PHP rand() function and store it to the local session variable then send it to the user email using PHP mailer function.
- When the user enters the OTP, we will verify it with the OTP stored in session if these match then store redirect the user to the home page.
- Create a new table with the name as username the user provided to store the email and password of that user.
Approach for Log-in Form:
- Connect to the database as described above, then check the credential provided by the user, if they match with the data stored in the database then redirect the user to the home page else show the related error.
PHP code for registration form: register.php
PHP code to send and verify OTP: verify-otp.php
PHP code to resend OTP in case of OTP not received: resend-otp.php
After successful OTP verification inserting user data to table 1_user:
Table schema, each row has these 4 columns:
id (int data type as primary key)
username (text data type)
email (text data type)
password (text data type)
PHP code for login form also the default page: index.php
PHP code of profile page: profile.php