In this tutorial I will show how to write a sign up form with email verification or confirmation in php. If your website use a registration form, you need to use email verification to reduce the spam and to make sure the email supplied belongs to that member.
In this tutorial I create a 7 file like below.
1. index.php - I write a registration form in this file.
2. configdb.php - to connect the database.
3. register.php - In this file, we will do form validation, saving user data to database and sending email to user for confirmation.
4. confirm.php - In this file, we will set the confirmation code to null if the user click the link from his email.
5. login.php - In this file, we will test whether the email and password is correct and confirmation code is null.
6. member.php - In this file, we will test whether the member is or not.
7. logout.php - In this file, we will unset the user session data.
Creating database tableWe need to create a user table before writing our script. Import following SQL statement via phpMyAdmin or any other MySQL tool.
CREATE TABLE `user` ( `id` INT( 50 ) NOT NULL AUTO_INCREMENT , `username` VARCHAR( 50 ) NOT NULL , `email` VARCHAR( 100 ) NOT NULL , `password` VARCHAR( 20 ) NOT NULL , `com_code` VARCHAR( 255 ) default NULL, PRIMARY KEY ( `id` ) ) ENGINE = InnoDB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sing Up</title> <style> label{ width:100px; float:left; } </style> </head> <body> <?php session_start(); if(isset($_SESSION['error'])) { echo '<p>'.$_SESSION['error']['username'].'</p>'; echo '<p>'.$_SESSION['error']['email'].'</p>'; echo '<p>'.$_SESSION['error']['password'].'</p>'; unset($_SESSION['error']); } ?> <div class="signup_form"> <form action="register.php" method="post" > <p> <label for="username">User Name:</label> <input name="username" type="text" id="username" size="30"/> </p> <p> <label for="email">E-mail:</label> <input name="email" type="text" id="email" size="30"/> </p> <p> <label for="password">Password:</label> <input name="password" type="password" id="password" size="30 "/> </p> <p> <input name="submit" type="submit" value="Submit"/> </p> </form> </div> <p><a href="login.php">Login</a></p> </body> </html> configdb.php
<?php $mysqli=mysqli_connect('localhost','dbusername','dbpassword','databasename') or die("Database Error"); ?> <?php session_start(); include('configdb.php'); if(isset($_POST['submit'])) { //whether the username is blank if($_POST['username'] == '') { $_SESSION['error']['username'] = "User Name is required."; } //whether the email is blank if($_POST['email'] == '') { $_SESSION['error']['email'] = "E-mail is required."; } else { //whether the email format is correct if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email'])) { //if it has the correct format whether the email has already exist $email= $_POST['email']; $sql1 = "SELECT * FROM user WHERE email = '$email'"; $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error()); if (mysqli_num_rows($result1) > 0) { $_SESSION['error']['email'] = "This Email is already used."; } } else { //this error will set if the email format is not correct $_SESSION['error']['email'] = "Your email is not valid."; } } //whether the password is blank if($_POST['password'] == '') { $_SESSION['error']['password'] = "Password is required."; } //if the error exist, we will go to registration form if(isset($_SESSION['error'])) { header("Location: index.php"); exit; } else { $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; $com_code = md5(uniqid(rand())); $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')"; $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error()); if($result2) { $to = $email; $subject = "Confirmation from TutsforWeb to $username"; $header = "TutsforWeb: Confirmation from TutsforWeb"; $message = "Please click the link below to verify and activate your account. rn"; $message .= "http://www.yourname.com/confirm.php?passkey=$com_code"; $sentmail = mail($to,$subject,$message,$header); if($sentmail) { echo "Your Confirmation link Has Been Sent To Your Email Address."; } else { echo "Cannot send Confirmation link to your e-mail address"; } } } } ?> confirm.php
<?php include('configdb.php'); $passkey = $_GET['passkey']; $sql = "UPDATE user SET com_code=NULL WHERE com_code='$passkey'"; $result = mysqli_query($mysqli,$sql) or die(mysqli_error()); if($result) { echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>'; } else { echo "Some error occur."; } ?> login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> <style> label{ width:100px; float:left; } </style> </head> <body> <?php session_start(); include('configdb.php'); if(isset($_POST['submit'])) { $email = trim($_POST['email']); $password = trim($_POST['password']); $query = "SELECT * FROM user WHERE email='$email' AND password='$password' AND com_code IS NULL"; $result = mysqli_query($mysqli,$query)or die(mysqli_error()); $num_row = mysqli_num_rows($result); $row=mysqli_fetch_array($result); if( $num_row ==1 ) { $_SESSION['user_name']=$row['username']; header("Location: member.php"); exit; } else { echo 'false'; } } ?> <div class="login_form"> <form action="login.php" method="post" > <p> <label for="email">E-mail:</label> <input name="email" type="text" id="email" size="30"/> </p> <p> <label for="password">Password:</label> <input name="password" type="password" id="password" size="30"/> </p> <p> <input name="submit" type="submit" value="Submit"/> </p> </form> </div> </body> </html> member.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Member page</title> </head> <body> <?php session_start(); if($_SESSION['user_name'] == '') { header("Location: index.php"); exit; } echo "Hi ".$_SESSION['user_name']; ?> <a href="logout.php">Logout</a> </body> </html> logout.php
<?php session_start(); unset($_SESSION['user_name']); header('Location: index.php'); ?>
This article is good . For more detail in briefly click here
BalasHapusHOW TO CREATE A SESSION IN PHP