Hello Developers, For Previous session we learned only login form using with session and database, Here We are going to full Registration form with sample coding.
Php registration form with email verification
Step 1:
First Create Config file for database connection named as config.php
Please Check previous link for creating DB and Table
Step 2 :
Create register page named as register.php with verification code in php
<?php if(isset($_POST['submit'])) // Check form submit action { $fname = $_POST['firstname']; // get form values $lname = $_POST['lastname']; $email = $_POST['email']; $phone = $_POST['mobile']; $gender = $_POST['gender']; $pass ="Not verify"; // initially insert the password as "Not verify" $link = md5(uniqid(rand())); // randomly generate the Activation link rand(); include "config.php"; // configuration the database $query = mysql_query("INSERT INTO `admin`(`User`,`email`,`Password`,`phone`,`Aid`,`Activationlink`) VALUES ('$fname$lname','$email','$pass','$phone','','$link')"); //insert query if($query) { $to = $email; // to address $subject = "Verification Link"; // subject $message ="Welcome to Techinibit Soluitons Please Click Activation link Below To active you Accounts<br/> <a href='www.ourwebsite.com/activation.php?activationlink='$link''>Click Here</a>"; // Message with verification link $header = "From . your name <your email>"; // your website address (From) $mail = mail($to,$subject,$message,$header); // mail function send the verification code to mail echo "Please Check You mail"; } } //echo $link = md5(uniqid(rand())); ?> <html> <head> <title> Registration Form </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" /> <!-- css external link --> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'/> </head> <body> <div class="wrapper"> <div class="clearfix"></div> <div class="headline"> <h1>Register Form</h1> </div> <!-- form here --> <form class="form" name="Register" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <div class="tb-row"> <label for="first-name">First Name</label> <input type="text" name="firstname" autocomplete="off" required="required"/> </div> <div class="tb-row"> <label for="last-name">Last Name</label> <input type="text" name="lastname" autocomplete="off" required="required"/> </div> <div class="tb-row"> <label for="email">Email</label> <input type="email" name="email" autocomplete="off" required="required"/> </div> <div class="tb-row"> <label for="mobile">Mobile number</label> <input type="number" name="mobile"/> </div> <div class="tb -row"> <label for="gender">Gender</label> <select name="gender"> <option>Male</option> <option>Female</option> </select> </div> <input id="submit" type="submit" name="submit" class="but submit" value="Register"/> <div class="tb-row"> <label for="password"><br/><a href="login.php">Login Here</a></label> </div> <div class="clearfix"></div> </form> </div> <div class="clearfix"></div> </body> </html>
Step 3:
Create as Css File for design named as style.css
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, img,strong, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } body { font-family: 'Lato', sans-serif; font-weight: 300; color: #838383; } h1, h2, h3, h4, h5, h6{ color: #9C9C9C; margin-bottom:5px; margin-top:0; } h2,p { color:#fff; } a { text-decoration:none; color:blue; } form { margin-bottom: 0; } /* ---------------------------------------------------------------------- */ /* Classes /* ---------------------------------------------------------------------- */ .wrapper { width: 450px; margin: 0 auto; margin-top:40px; font-family: 'Lato', sans-serif; font-weight: 300; padding: 40px; background-color: darkgray; } /* ---------------------------------------------------------------------- */ /* Headline /* ---------------------------------------------------------------------- */ .headline { text-align:center; margin-bottom:30px; } .headline h1 { color:#ffffff; font-size: 30px; font-weight: 700; line-height: 40px; letter-spacing: -1px; } .but { padding: 6px 12px; border-radius: 4px; font-size: 16px; color: #ffffff; text-decoration: none; } .submit{ background-color: #1875F7 !important; float: right !important; color: #ffffff !important; margin-bottom: 20px; margin-top: 8px; } .tb-row { margin-bottom: 30px; } .form label { margin-bottom: 6px; line-height: 19px; width: 40%; float: left; color:#fff; } .form input, .form select, .form textarea, .form button { position: relative; display: block; } .form input, .form select, .form textarea { display: block; box-sizing: border-box; -moz-box-sizing: border-box; width: 60%; height: 39px; padding: 4px 10px; outline: none; border-width: 2px; border-style: solid; border-radius: 0; background: #fff; font: 15px/19px 'Open Sans', Helvetica, Arial, sans-serif; color: #B1AAAA; appearance: normal; -moz-appearance: none; -webkit-appearance: none; } .clearfix { margin-top:20px; }
Step 4: Create email activation page named as activation.php
<!-- Password Update here --> <?php if(isset($_POST['submit'])) { $password = md5($_POST['password']); //password get from form $email = $_POST['email']; // email $link = $_POST['link']; // verification link include "config.php"; $query = mysql_query("UPDATE `admin` SET `Password`='".$password."' WHERE `email`='".$email."' AND `Activationlink`='".$link."'"); // updated your password if($query) { echo "Your username and password is updated succesfully <br/> Wait this page redirect to login page"; header( "refresh:5;url=login.php" ); } } ?> <!-- password update ends here --> <!-- Activation Verify Here --> <?php if(isset($_GET['activationlink'])) { $link = $_GET['activationlink']; // get your verification code sent to your email include "config.php"; $query = mysql_query("SELECT * FROM `admin` WHERE `Activationlink`='".$link."'"); // check the condition if(mysql_num_rows($query)>0) { while($row=mysql_fetch_array($query)) { $mail = $row['email']; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" /> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'/> </head> <body> <div class="wrapper"> <div class="clearfix"></div> <div class="headline"> <h3> YOUR ACCOUNT IS ACTIVATED SUCCESSFULLY. PLEASE UPDATE YOUR PASSWORD </h3> </div> <form class="form" name="update" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <div class="tb-row"> <label for="first-name">Email</label> <input type="text" name="email" value="<?php echo "$mail";?>" readonly /> <!-- username --> </div> <div class="tb-row"> <label for="password">Password</label> <input type="password" name="password" autocomplete="off" required="required"/> <!-- password --> </div> <div class="tb-row"> <label for="password">Password</label> <input type="password" name="password" autocomplete="off" required="required"/> </div> <input type="hidden" name="link" value="<?php echo $link; ?>" /> <input id="submit" type="submit" name="submit" class="but submit" value="Update"/> </form> <br/> </div> </body> <?php } } else { echo "Your email id is nor register properly. please check again."; } } else { header( "location: login.php" ); } ?> <!-- Activation Verify Ends Here -->
Step 5: Create login.php page
<?php session_start(); // Session start include ‘config.php'; // Config for database connection if(isset($_POST[‘validate’])) // Check form submit with IF Isset function { $username =$_POST[‘username’]; // set username to variable $password =$_POST[‘password’]; //set password to variable $result = mysql_query(“SELECT * FROM `admin` WHERE `User`=’$username’ AND Password=’$password'”); //check from database if(mysql_num_rows ($result) > 0 ) { $_SESSION[‘username’] = $username; // set session name $_SESSION[‘password’] = $password; // set session password header(‘location:maine.php’); //redirect to main page } else { $err=”Authentication Failed Try again!”; } } ?><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" /> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'/> </head> <body> <div class="wrapper"> <div class="clearfix"></div> <div class="headline"> <h1>Login Form</h1> </div> <form class="form" name="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <div class="tb-row"> <label for="first-name">First Name</label> <input type="text" name="username" autocomplete="off" required="required"/> </div> <div class="tb-row"> <label for="password">Password</label> <input type="password" name="password" autocomplete="off" required="required"/> </div> <div class="tb-row"> <label for="password"><a href="forgot-password.php">Forgot Password</a> <br/><a href="register.php">Register</a></label> </div> <input id="submit" type="submit" name="validate" class="but submit" value="Login"/> </form> <br/> </div> </body> </html>
Step 6: Create logout.php page
<?php session_start(); if(!isset($_SESSION[‘username’])) { header(‘location:index.php’); } unset($_SESSION[‘username’]); session_destroy(); header(‘location:login.php’); ?>
Step 7: Create main.php for home page
<?php session_start(); if((!isset ( $_SESSION['userr']) == true ) and (!isset ($_SESSION['passs'] ) == true)) { unset($_SESSION['userr']); unset($_SESSION['passs']); session_destroy(); header("location: login.php"); } else { $user = $_SESSION['userr']; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" /> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'/> </head> <body> <div class="wrapper"> <div class="clearfix"></div> <div class="headline"> <h2>Welcome to main page </h2> </div> <p> The session details are <?php echo $user; ?></p> <p><a href="logout.php"> Logout </a> here.</p> </div> </body> </html> Step 9: Create forgot-password.php page <?php if(isset($_POST['submit'])) { $email = $_POST['email']; include 'config.php'; $query = mysql_query("select * from admin where email = '".$email."'"); if(mysql_num_rows($query)>0) { while($row=mysql_fetch_array($query)) { $pass = $row['Password']; $to = $email; $subject = "Password Reset"; $message = "This is you password please login using this password <b>$pass</b>"; $header = "from. username <admin@yourwebsite.com>"; $mail = mail($to,$subject,$message,$header); echo "PLease check you mail, We have send your password through mail"; } } else { echo "Sorry You mail id is not in our database. please check and give valid id"; } } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" /> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'/> </head> <body> <div class="wrapper"> <div class="clearfix"></div> <div class="headline"> <h1>Forgot Password</h1> </div> <form class="form" name="password" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <div class="tb-row"> <label for="email">Email</label> <input type="email" name="email" autocomplete="off" required="required"/> </div> <input id="submit" type="submit" name="submit" class="but submit" value="submit"/> </form> <br/> </div> </body> </html>
Great You learned it easily, download the full source code and enjoy the coding.
Click here to download full code – Php coding for registration with emial verifiaction
Alternative download link