User Registration and Login using PHP and PostgreSQL
User Registration and Login using PHP and PostgreSQL tutorial, we learn how to create a user registration and login in using PHP and PostgreSQL.
File structure for user registration and login
index.php: For user signup or user registration
signin.php: User for user sign in
dashboard.php: After login user redirect to this page
logout.php: Logout page
Step 1– Create a database
includes/dbconnection.php: Database connection file
Database name: employeesdb
1 |
create database employeesdb |
After creating the database, run the SQL script or import the SQL file inside the package.
Structure for SQL table tbluser
1 2 3 4 5 6 7 8 |
CREATE TABLE tblemployees ( id SERIAL PRIMARY KEY, FullName VARCHAR(255), MobileNumber VARCHAR(255), Email VARCHAR(255), Password VARCHAR(255), RegDate TIMESTAMP DEFAULT NOW() ); |
Step 2– Create a database configuration file includes/dbconnection.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // Database configuration $host = 'localhost'; $db = 'employeesdb'; $user = 'postgres'; $pass = 'Test@123'; $port = '5432'; // Default port for PostgreSQL // Create connection string $conn_string = "host=$host port=$port dbname=$db user=$user password=$pass"; // Establish a connection to the PostgreSQL database $conn = pg_connect($conn_string); if (!$conn) { echo "Error: Unable to open database\n"; exit; } |
Step 3– Create an HTML form for User Signup / Registration.
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 |
<h2 class="title">Registration Form</h2> <form method="POST"> <div class="input-group"> <input class="input--style-1" type="text" placeholder="NAME" name="fname" required="true"> </div> <div class="row row-space"> <div class="col-2"> <div class="input-group"> <input class="input--style-1" type="text" placeholder="Mobile Number" name="contactno" required="true" maxlength="10" pattern="[0-9]+"> </div> </div> </div> <div class="input-group"> <div class="rs-select2 js-select-simple select--no-search"> <input class="input--style-1" type="email" placeholder="Email Address" name="email" required="true"> </div> </div> <div class="row row-space"> <div class="col-2"> <div class="input-group"> <input type="password" value="" class="input--style-1" name="password" required="true" placeholder="Password"> </div> </div> </div> <div class="p-t-20"> <button class="btn btn--radius btn--green" type="submit" name="submit">Submit</button> </div> <br> <a href="signin.php" style="color: red">Already have an account? Signin</a> </form> |
Step 4– Code for inserting User Signup / Registration values in the database
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 |
<?php include('includes/dbconnection.php'); if(isset($_POST['submit'])) { $fname=$_POST['fname']; $contno=$_POST['contactno']; $email=$_POST['email']; $password=md5($_POST['password']); $ret=pg_query($conn,"select Email from tblemployees where Email='$email' OR MobileNumber='$contno'"); $result=pg_num_rows($ret); if($result>0){ echo "<script>alert('This email or Contact Number already associated with another account');</script>"; } else{ $query = pg_query($conn, "INSERT INTO tblemployees (FullName, MobileNumber, Email, Password) VALUES ('$fname', '$contno', '$email', '$password')"); if ($query) { echo "<script>alert('You have successfully registered');</script>"; echo "<script>window.location.href ='signin.php'</script>"; } else { echo "<script>alert('Something Went Wrong. Please try again');</script>"; echo "<script>window.location.href ='index.php'</script>"; } } } ?> |
Here is the full code that we have written for signup/registration (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 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 |
<?php include('includes/dbconnection.php'); if(isset($_POST['submit'])) { $fname=$_POST['fname']; $contno=$_POST['contactno']; $email=$_POST['email']; $password=md5($_POST['password']); $ret=pg_query($conn,"select Email from tblemployees where Email='$email' OR MobileNumber='$contno'"); $result=pg_num_rows($ret); if($result>0){ echo "<script>alert('This email or Contact Number already associated with another account');</script>"; } else{ $query = pg_query($conn, "INSERT INTO tblemployees (FullName, MobileNumber, Email, Password) VALUES ('$fname', '$contno', '$email', '$password')"); if ($query) { echo "<script>alert('You have successfully registered');</script>"; echo "<script>window.location.href ='signin.php'</script>"; } else { echo "<script>alert('Something Went Wrong. Please try again');</script>"; echo "<script>window.location.href ='index.php'</script>"; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Sign Up Page</title> <!-- Icons font CSS--> <link href="vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all"> <link href="vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all"> <!-- Font special for pages--> <link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i" rel="stylesheet"> <!-- Vendor CSS--> <link href="vendor/select2/select2.min.css" rel="stylesheet" media="all"> <link href="vendor/datepicker/daterangepicker.css" rel="stylesheet" media="all"> <!-- Main CSS--> <link href="css/main.css" rel="stylesheet" media="all"> </head> <body> <div class="page-wrapper bg-blue p-t-100 p-b-100 font-robo"> <div class="wrapper wrapper--w680"> <div class="card card-1"> <div class="card-heading"></div> <div class="card-body"> <h2 class="title">Registration Form</h2> <form method="POST"> <div class="input-group"> <input class="input--style-1" type="text" placeholder="NAME" name="fname" required="true"> </div> <div class="row row-space"> <div class="col-2"> <div class="input-group"> <input class="input--style-1" type="text" placeholder="Mobile Number" name="contactno" required="true" maxlength="10" pattern="[0-9]+"> </div> </div> </div> <div class="input-group"> <div class="rs-select2 js-select-simple select--no-search"> <input class="input--style-1" type="email" placeholder="Email Address" name="email" required="true"> </div> </div> <div class="row row-space"> <div class="col-2"> <div class="input-group"> <input type="password" value="" class="input--style-1" name="password" required="true" placeholder="Password"> </div> </div> </div> <div class="p-t-20"> <button class="btn btn--radius btn--green" type="submit" name="submit">Submit</button> </div> <br> <a href="signin.php" style="color: red">Already have an account? Signin</a> </form> </div> </div> </div> </div> <!-- Jquery JS--> <script src="vendor/jquery/jquery.min.js"></script> <!-- Vendor JS--> <script src="vendor/select2/select2.min.js"></script> <script src="vendor/datepicker/moment.min.js"></script> <script src="vendor/datepicker/daterangepicker.js"></script> <!-- Main JS--> <script src="js/global.js"></script> </body><!-- This templates was made by Colorlib (https://colorlib.com) --> </html> <!-- end document--> |
Step 5– User Sign-in or Login
First create a HTML form with two filed username and Password
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<form method="POST"> <div class="input-group"> <input type="text" class="input--style-1" placeholder="Registered Email or Contact Number" required="true" name="emailcont"> </div> <div class="row row-space"> <div class="col-2"> <div class="input-group"> <input type="password" class="input--style-1" placeholder="Password" name="password" required="true"> </div> </div> </div> <div class="p-t-20"> <button class="btn btn--radius btn--green" type="submit" name="login">Sign IN</button> </div><br> <a href="index.php">Don't have an account ? CREATE AN ACCOUNT</a> </form> |
Users can log in using a valid email ID/contact number and password.
PHP Code for Sign-in or Login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php session_start(); error_reporting(0); include('includes/dbconnection.php'); if(isset($_POST['login'])) { $emailcon=$_POST['emailcont']; $password=md5($_POST['password']); $query=mysqli_query($con,"select ID from tbluser where (Email='$emailcon' || MobileNumber='$emailcon') && Password='$password' "); $ret=mysqli_fetch_array($query); if($ret>0){ $_SESSION['uid']=$ret['ID']; echo "<script type='text/javascript'> document.location ='dashboard.php'; </script>"; }else{ echo "<script>alert('Invalid Details');</script>"; }} ?> |
Step 6– Create a welcome page.
After logging in, the user will be redirected to this page. Validate this page using the user session. If anyone wants to access this page without logging in, they will be redirected to signin.php automatically.
Code for dashboard.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 53 54 55 |
<?php session_start(); error_reporting(0); include('includes/dbconnection.php'); if (strlen($_SESSION['uid']==0)) { header('location:logout.php'); } else{ ?> <!DOCTYPE html> <html lang="en"> <head> <title>Welcome Page</title> <!-- Icons font CSS--> <link href="vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all"> <link href="vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all"> <!-- Font special for pages--> <link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i" rel="stylesheet"> <!-- Vendor CSS--> <link href="vendor/select2/select2.min.css" rel="stylesheet" media="all"> <link href="vendor/datepicker/daterangepicker.css" rel="stylesheet" media="all"> <!-- Main CSS--> <link href="css/main.css" rel="stylesheet" media="all"> </head> <body> <div class="page-wrapper bg-blue p-t-100 p-b-100 font-robo"> <div class="wrapper wrapper--w680"> <div class="card card-1"> <div class="card-body"> <?php $uid=$_SESSION['uid']; $ret=mysqli_query($con,"select FullName from tbluser where ID='$uid'"); $row=mysqli_fetch_array($ret); $name=$row['FullName']; ?> <h4 style="color: blue; text-align: center;">Welcome !! <?php echo $name;?></h4> </div> </div> <a href="logout.php" class="btn btn-outline btn-default">Logout</a> </div> <!-- Jquery JS--> <script src="vendor/jquery/jquery.min.js"></script> <!-- Vendor JS--> <script src="vendor/select2/select2.min.js"></script> <script src="vendor/datepicker/moment.min.js"></script> <script src="vendor/datepicker/daterangepicker.js"></script> <!-- Main JS--> <script src="js/global.js"></script> </body> </html> <!-- end document--> <?php } ?> |
Step 7– logout Page.
The logout page is used to destroy the session or unset the session variable.
code for the logout.php page
1 2 3 4 5 6 |
<?php session_start(); session_unset();// unset session variable session_destroy(); // destroy session header('location:signin.php'); ?> |