User Registration and Login using PHP Prepared Statements
In previous tutorial, we learned crud operation using PHP prepared statements. Now In this tutorial, we will learn user registration and login using prepared statements.
File structure for this tutorial
- config.php (Database connection file)
- index.php (Signup page)
- login.php (login Page)
- welcome.php (After successful login user will redirect to this page)
- logout.php (Logout page)
- stmt.sql (SQl file)
MySQL Table(tblusers
) structure used in this tutorial
1 2 3 4 5 6 7 8 |
CREATE TABLE `tblusers` ( `id` int(11) NOT NULL, `FullName` varchar(255) DEFAULT NULL, `EmailId` varchar(255) DEFAULT NULL, `MobileNumber` bigint(10) DEFAULT NULL, `Password` varchar(255) DEFAULT NULL, `RegDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Database connection file (Config.php
)
In this file, you can change the database name according to your database. We are using stmt
database.
1 2 3 4 5 6 7 |
<?php $dbuser="root"; $dbpass=""; $host="localhost"; $dbname = "stmt"; $mysqli = new mysqli($host, $dbuser, $dbpass, $dbname); ?> |
Now Create a User registration HTML form (index.php
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<form method="post"> <h2>User Sign Up </h2> <p>Please fill in this form to create an account!</p> <hr> <div class="form-group"> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <span class="fa fa-user"></span> </span> </div> <input type="text" class="form-control" name="fullname" placeholder="Full Name" required="required" pattern="[A-Za-z ]+" title="Letters only"> </div> </div> <div class="form-group"> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <i class="fa fa-paper-plane"></i> </span> </div> <input type="email" class="form-control" name="email" placeholder="Email Address" required="required" title="Valid Email id"> </div> </div> <div class="form-group"> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <i class="fa fa-mobile"></i> </span> </div> <input type="tel" class="form-control" name="mobilenumber" placeholder="Mobile Number" required="required" pattern="[0-9]{10}" title="10 numeric characters only"> </div> </div> <div class="form-group"> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <i class="fa fa-lock"></i> </span> </div> <input type="password" class="form-control" name="password" placeholder="Password" required="required" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="At least one number and one uppercase and lowercase letter, and at least 6 or more characters"> </div> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-lg" name="signup">Sign Up</button> </div> </form> |
PHP Code for user registration . Put this code at the top of the index.php
page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php include_once('config.php'); //Coding For Signup if(isset($_POST['signup'])) { //Getting Psot Values $fname=$_POST['fullname']; $email=$_POST['email']; $mobile=$_POST['mobilenumber']; $pass=$_POST['password']; //Checking email id exist for not $result ="SELECT count(*) FROM tblusers WHERE EmailId=?"; $stmt = $mysqli->prepare($result); $stmt->bind_param('s',$email);$stmt->execute(); $stmt->bind_result($count); $stmt->fetch(); $stmt->close(); //if email already exist if($count>0) { echo "<script>alert('Email id already associated with another account. Please try with diffrent EmailId.');</script>"; } // If email not exist else { $sql="INSERT into tblusers(FullName,EmailId,MobileNumber,Password)VALUES(?,?,?,?)"; $stmti = $mysqli->prepare($sql); $stmti->bind_param('ssis',$fname,$email,$mobile,$pass); $stmti->execute(); $stmti->close(); echo "<script>alert('User registration successful');</script>"; } } ?> |
After user registration, We create a HTML form for login (login.php
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<form method="post"> <h2>User Login</h2> <hr> <div class="form-group"> <div class="form-group"> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <i class="fa fa-paper-plane"></i> </span> </div> <input type="email" class="form-control" name="email" placeholder="Email Address" required="required" title="Valid Email id"> </div> </div> <div class="form-group"> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <i class="fa fa-lock"></i> </span> </div> <input type="password" class="form-control" name="password" placeholder="Password" required="required"> </div> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-lg" name="login">Login</button> </div> </form> |
PHP Code for user login. Put this code at the top of login.php
page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php session_start(); include_once('config.php'); //Coding For Signup if(isset($_POST['login'])) { //Getting Psot Values $email=$_POST['email']; $pass=$_POST['password']; $stmt = $mysqli->prepare( "SELECT FullName,id FROM tblusers WHERE (EmailId=? || Password=?)"); $stmt->bind_param('ss',$email,$pass); $stmt->execute(); $stmt->bind_result($FullName,$id); $rs= $stmt->fetch (); $stmt->close(); if (!$rs) { echo "<script>alert('Invalid Details. Please try again.')</script>"; } else { $_SESSION['fname']=$FullName; $_SESSION['uid']=$id; header('location:welcome.php'); } } ?> |
After successfull login, user we redirect to welcome.php
page. Validate this page using user session if anyone want to access this page without login user will redirect to login.php automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?php session_start(); //Validation login Session if(strlen($_SESSION['uid'])==0) { header("Location:logout.php"); } else{ ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700"> <title>Welcome</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <style> body { color: #fff; background: #19aa8d; font-family: 'Roboto', sans-serif; } .form-control { font-size: 15px; } .form-control, .form-control:focus, .input-group-text { border-color: #e1e1e1; } .form-control, .btn { border-radius: 3px; } .signup-form { width: 400px; margin: 0 auto; padding: 30px 0; } .signup-form form { color: #999; border-radius: 3px; margin-bottom: 15px; background: #fff; box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); padding: 30px; } .signup-form h2 { color: #333; font-weight: bold; margin-top: 0; } .signup-form hr { margin: 0 -30px 20px; } .signup-form .form-group { margin-bottom: 20px; } .signup-form label { font-weight: normal; font-size: 15px; } .signup-form .form-control { min-height: 38px; box-shadow: none !important; } .signup-form .input-group-addon { max-width: 42px; text-align: center; } .signup-form .btn, .signup-form .btn:active { font-size: 16px; font-weight: bold; background: #19aa8d !important; border: none; min-width: 140px; } .signup-form .btn:hover, .signup-form .btn:focus { background: #179b81 !important; } .signup-form a { color: #fff; text-decoration: underline; } .signup-form a:hover { text-decoration: none; } .signup-form form a { color: #19aa8d; text-decoration: none; } .signup-form form a:hover { text-decoration: underline; } .signup-form .fa { font-size: 21px; } .signup-form .fa-paper-plane { font-size: 18px; } .signup-form .fa-check { color: #fff; left: 17px; top: 18px; font-size: 7px; position: absolute; } </style> </head> <body> <div class="signup-form"> <form method="post"> <h2>Welcome <?php echo $_SESSION['fname'];?></h2> <hr> <div class="form-group"> <div class="form-group" align="center"> <a class="btn btn-primary btn-lg" href="logout.php" style="color:#fff">Logout</a> </div> </form> </div> </body> </html> <?php } ?> |
Now we will create a page for logout (logout.php
)
Logout page used for destroy the session.
1 2 3 4 5 6 |
<?php session_start(); session_destroy(); header('location:login.php') ?> |
View Demo+++++++++++++++++++++++++++++
How to run the User Registration and login Using PHP Prepared Statements
- Download the zip file
- Extract the file and copy
pstmt-signup
folder - Paste inside root directory(for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/html)
- Open PHPMyAdmin (http://localhost/phpmyadmin)
- Create a database with name
stmt
- Import
stmt.sql
file(given inside the zip package in SQL file folder) - Run the script
http://localhost/pstmt-signup