Login & Signup Form with Email Verification using PHP and MySQL
Login & registration/signup form with email verification in PHP and MySQL. Through this tutorial, you will learn how to send user confirmation email after registration with activation link in PHP and MySQL.
As well as we provide the login and signup form with email verification using php mysql source code link at the end of this tutorial.
Suppose, any user registered with your PHP and MySQL web/app. And you need to verify user email address by send activation/verification link in email on user registration.
Login & Registration Form with Email Verification using PHP and MySQL
Step 1: Create Table In DB by Sql Query
Step 2: Create a Database Connection PHP File
Step 3: Create User Registration Page PHP
Step 4: Store User Registration Data and Send Email
Step 5: Create Email Verification PHP File
Step 6 – Create Login Form In PHP with MySQL
Step 7- Create User Profile and Fetch data From Database
First of all, open your phpmyadmin and run the following sql query. To create table into your selected database:
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `email` varchar(100) NOT NULL, `password` varchar(250) NOT NULL, `status` int(11) NOT NULL DEFAULT '0', `email_verification_link` varchar(255) NOT NULL, `email_verified_at` TIMESTAMP NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2: Create a Database Connection PHP File
In this step, create a file name db.php and add the following code into db.php file:
Note that, This code is used to create a MySQL database connection in PHP project.
Step 3: Create User Registration Page PHP
In this step, create a user registration php file that named index.php and add the below PHP and HTML code into index.php file.
Note that, This HTML code shows the user registration form.
Now, add the following html form into index.php file:
User Registration with Email Verification in PHP
Name
Email address We'll never share your email with anyone else.
Password
Confirm Password
Step 4: Store User Registration Data and Send Email
In this step, create new PHP file named store-registration-send-email.php. This file PHP code will store user registration data into db table. And As well as send an email verification link to user and store it into MySQL DB.
To add the following php code into store-registration-send-email.php file:
Click and Verify Email"; require_once('phpmail/PHPMailerAutoload.php'); $mail = new PHPMailer(); $mail->CharSet = "utf-8"; $mail->IsSMTP(); // enable SMTP authentication $mail->SMTPAuth = true; // GMAIL username $mail->Username = "[email protected]"; // GMAIL password $mail->Password = "your_gmail_password"; $mail->SMTPSecure = "ssl"; // sets GMAIL as the SMTP server $mail->Host = "smtp.gmail.com"; // set the SMTP port for the GMAIL server $mail->Port = "465"; $mail->From='[email protected]'; $mail->FromName='your_name'; $mail->AddAddress('reciever_email_id', 'reciever_name'); $mail->Subject = 'Reset Password'; $mail->IsHTML(true); $mail->Body = 'Click On This Link to Verify Email '.$link.''; if($mail->Send()) < echo "Check Your Email box and Click on the email verification link."; >else < echo "Mail Error - >".$mail->ErrorInfo; > > else < echo "You have already registered with us. Check Your email box and verify email."; >> ?>
Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.
Once less secure apps are enabled; now you can use your Gmail for sending the emails.
Step 5: Create Email Verification PHP File
In this step, create email verification php file that named verify-email.php. This php file code verify user registration email from mysql database.
So, add the following php and html code into verify-email.php file:
0) < $row= mysqli_fetch_array($query); if($row['email_verified_at'] == NULL)< mysqli_query($conn,"UPDATE users set email_verified_at ='" . $d . "' WHERE email='" . $email . "'"); $msg = "Congratulations! Your email has been verified."; >else < $msg = "You have already verified your account with us"; >> else < $msg = "This email has been not registered with us"; >> else < $msg = "Danger! Your something goes to wrong."; >?>
User Account Activation by Email Verification using PHP
Login
Step 6 – Create Login Form In PHP with MySQL
Create login form, where you accept user email id and password and authenticate users from database. So you can create a login.php file and add the below code into your file:
if (isset($_POST['login'])) < $email = mysqli_real_escape_string($conn, $_POST['email']); $password = mysqli_real_escape_string($conn, $_POST['password']); if(!filter_var($email,FILTER_VALIDATE_EMAIL)) < $email_error = "Please Enter Valid Email ID"; >if(strlen($password) < 6) < $password_error = "Password must be minimum of 6 characters"; >$result = mysqli_query($conn, "SELECT * FROM users WHERE email = '" . $email. "' and pass = '" . md5($password). "'"); if(!empty($result)) < if ($row = mysqli_fetch_array($result)) < $_SESSION['user_id'] = $row['uid']; $_SESSION['user_name'] = $row['name']; $_SESSION['user_email'] = $row['email']; $_SESSION['user_mobile'] = $row['mobile']; header("Location: dashboard.php"); >>else < $error_message = "Incorrect Email or Password. "; >> ?>
Please fill all fields in the form
Step 7- Create User Profile and Fetch data From Database
Create a new file name dashboard.php and add the below code into your file.
The below code used to show logged in user data.
?> Logout
Step 8 – Create Logout.php file
Create logout.php file and update the below code into your file.
The below code is used to destroy login your data and logout.
Conclusion
Login and Registration form with email verification using PHP MySQL tutorial, you have learned how to send activation/verfication link after user login and registration in PHP and MySQL.
Each line should be separated with a CRLF (\r\n). Lines should not be larger than 70 characters.
(Windows only) When PHP is talking to a SMTP server directly, if a full stop is found on the start of a line, it is removed. To counter-act this, replace these occurrences with a double dot.
String or array to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.
If an array is passed, its keys are the header names and its values are the respective header values.
Note:
Before PHP 5.4.42 and 5.5.27, repectively, additional_headers did not have mail header injection protection. Therefore, users must make sure specified headers are safe and contains headers only. i.e. Never start mail body by putting multiple newlines.
Note:
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini .
Failing to do this will result in an error message similar to Warning: mail(): «sendmail_from» not set in php.ini or custom «From:» header missing . The From header sets also Return-Path when sending directly via SMTP (Windows only).
Note:
If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.
The additional_params parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.
This parameter is escaped by escapeshellcmd() internally to prevent command execution. escapeshellcmd() prevents command execution, but allows to add additional parameters. For security reasons, it is recommended for the user to sanitize this parameter to avoid adding unwanted parameters to the shell command.
Since escapeshellcmd() is applied automatically, some characters that are allowed as email addresses by internet RFCs cannot be used. mail() can not allow such characters, so in programs where the use of such characters is required, alternative means of sending emails (such as using a framework or a library) is recommended.
The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a ‘X-Warning’ header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users .
Return Values
Returns true if the mail was successfully accepted for delivery, false otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
Changelog
Version
Description
7.2.0
The additional_headers parameter now also accepts an array .
Examples
Example #1 Sending mail.
Using mail() to send a simple email:
// The message $message = «Line 1\r\nLine 2\r\nLine 3» ;
?php
// In case any of our lines are larger than 70 characters, we should use wordwrap() $message = wordwrap ( $message , 70 , «\r\n» );
Example #4 Sending mail with an additional command line parameter.
The additional_params parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path .
Example #5 Sending HTML email
It is also possible to send HTML email with mail() .
// Subject $subject = ‘Birthday Reminders for August’ ;
// Message $message = ‘
Here are the birthdays upcoming in August!
Person
Day
Month
Year
Johny
10th
August
1970
Sally
17th
August
1973
‘ ;
// To send HTML mail, the Content-type header must be set $headers [] = ‘MIME-Version: 1.0’ ; $headers [] = ‘Content-type: text/html; charset=iso-8859-1’ ;
// Mail it mail ( $to , $subject , $message , implode ( «\r\n» , $headers )); ?>
Note:
If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package » PEAR::Mail_Mime.
Notes
Note:
The SMTP implementation (Windows only) of mail() differs in many ways from the sendmail implementation. First, it doesn’t use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Second, the custom headers like From: , Cc: , Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.
As such, the to parameter should not be an address in the form of «Something «. The mail command may not parse this properly while talking with the MTA.
Note:
It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.