I am assuming here that you are familiar with HTML and CSS. I have commented the code with the necessary information in the major part of the code. So you can understand easily. I have also attached the source code so you can download it and use it. Let's start.
Note
I have posted this PHP Registration Script on my personal blog.You can also check it.
The following are the points we will be learn in this article,
- Creating Database in phpmyadmin.
- Create connection with MySQL database.
- Insert, delete and view data from MySQL database.
- Some Bootstrap components.
- Sessions in PHP.
Files and IDE
- Registration
- Login
- Logout
- Welcome
- Admin_login
- View_users
- Db_conection
- Delete.php
I am using PHPStorm for the coding and Bootstrap framework for front end development.
Create Database
Create Tables
Create columns in Users table
Crate Admin Table
Create Admin Columns
Registration.php
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
- <title>Registration</title>
- </head>
- <style>
- .login-panel {
- margin-top: 150px;
-
- </style>
- <body>
-
- <div class="container"><!-- container class is used to centered the body of the browser with some decent width-->
- <div class="row"><!-- row class is used for grid system in Bootstrap-->
- <div class="col-md-4 col-md-offset-4"><!--col-md-4 is used to create the no of colums in the grid also use for medimum and large devices-->
- <div class="login-panel panel panel-success">
- <div class="panel-heading">
- <h3 class="panel-title">Registration</h3>
- </div>
- <div class="panel-body">
- <form role="form" method="post" action="registration.php">
- <fieldset>
- <div class="form-group">
- <input class="form-control" placeholder="Username" name="name" type="text" autofocus>
- </div>
-
- <div class="form-group">
- <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Password" name="pass" type="password" value="">
- </div>
-
-
- <input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
-
- </fieldset>
- </form>
- <center><b>Already registered ?</b> <br></b><a href="login.php">Login here</a></center><!--for centered text-->
- </div>
- </div>
- </div>
- </div>
- </div>
-
- </body>
-
- </html>
-
- <?php
-
- include("database/db_conection.php");
- if(isset($_POST['register']))
- {
- $user_name=$_POST['name'];
- $user_pass=$_POST['pass'];
- $user_email=$_POST['email'];
-
-
- if($user_name=='')
- {
-
- echo"<script>alert('Please enter the name')</script>";
- exit();
- }
-
- if($user_pass=='')
- {
- echo"<script>alert('Please enter the password')</script>";
- exit();
- }
-
- if($user_email=='')
- {
- echo"<script>alert('Please enter the email')</script>";
- exit();
- }
-
- $check_email_query="select * from users WHERE user_email='$user_email'";
- $run_query=mysqli_query($dbcon,$check_email_query);
-
- if(mysqli_num_rows($run_query)>0)
- {
- echo "<script>alert('Email $user_email is already exist in our database, Please try another one!')</script>";
- exit();
- }
-
- $insert_user="insert into users (user_name,user_pass,user_email) VALUE ('$user_name','$user_pass','$user_email')";
- if(mysqli_query($dbcon,$insert_user))
- {
- echo"<script>window.open('welcome.php','_self')</script>";
- }
-
-
-
- }
-
- ?>
Login.php
- <?php
- session_start();
-
- ?>
-
-
-
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
- <title>Login</title>
- </head>
- <style>
- .login-panel {
- margin-top: 150px;
-
- </style>
-
- <body>
-
-
- <div class="container">
- <div class="row">
- <div class="col-md-4 col-md-offset-4">
- <div class="login-panel panel panel-success">
- <div class="panel-heading">
- <h3 class="panel-title">Sign In</h3>
- </div>
- <div class="panel-body">
- <form role="form" method="post" action="login.php">
- <fieldset>
- <div class="form-group" >
- <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Password" name="pass" type="password" value="">
- </div>
-
-
- <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="login" >
-
- <!-- Change this to a button or input when using this as a form -->
- <!-- <a href="index.html" class="btn btn-lg btn-success btn-block">Login</a> -->
- </fieldset>
- </form>
- </div>
- </div>
- </div>
- </div>
- </div>
-
-
- </body>
-
- </html>
-
- <?php
-
- include("database/db_conection.php");
-
- if(isset($_POST['login']))
- {
- $user_email=$_POST['email'];
- $user_pass=$_POST['pass'];
-
- $check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";
-
- $run=mysqli_query($dbcon,$check_user);
-
- if(mysqli_num_rows($run))
- {
- echo "<script>window.open('welcome.php','_self')</script>";
-
- $_SESSION['email']=$user_email;
-
- }
- else
- {
- echo "<script>alert('Email or password is incorrect!')</script>";
- }
- }
- ?>
Logout.php
- <?php
-
-
-
-
-
-
-
- session_start();
- session_destroy();
- header("Location: login.php");
- ?>
Welcome.php
- <?php
- session_start();
-
- if(!$_SESSION['email'])
- {
-
- header("Location: login.php");
- }
-
- ?>
-
- <html>
- <head>
-
- <title>
- Registration
- </title>
- </head>
-
- <body>
- <h1>Welcome</h1><br>
- <?php
- echo $_SESSION['email'];
- ?>
-
-
- <h1><a href="logout.php">Logout here</a> </h1>
-
-
- </body>
-
- </html>
Admin_login.php
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
- <title>Admin Login</title>
- </head>
- <style>
- .login-panel {
- margin-top: 150px;
-
- </style>
-
- <body>
-
- <div class="container">
- <div class="row">
- <div class="col-md-4 col-md-offset-4">
- <div class="login-panel panel panel-success">
- <div class="panel-heading">
- <h3 class="panel-title">Sign In</h3>
- </div>
- <div class="panel-body">
- <form role="form" method="post" action="admin_login.php">
- <fieldset>
- <div class="form-group" >
- <input class="form-control" placeholder="Name" name="admin_name" type="text" autofocus>
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Password" name="admin_pass" type="password" value="">
- </div>
-
-
- <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="admin_login" >
-
-
- </fieldset>
- </form>
- </div>
- </div>
- </div>
- </div>
- </div>
-
-
- </body>
-
- </html>
-
- <?php
-
-
-
-
-
-
- include("database/db_conection.php");
-
- if(isset($_POST['admin_login']))
- {
- $admin_name=$_POST['admin_name'];
- $admin_pass=$_POST['admin_pass'];
-
- $admin_query="select * from admin where admin_name='$admin_name' AND admin_pass='$admin_pass'";
-
- $run_query=mysqli_query($dbcon,$admin_query);
-
- if(mysqli_num_rows($run_query)>0)
- {
-
- echo "<script>window.open('view_users.php','_self')</script>";
- }
- else {echo"<script>alert('Admin Details are incorrect..!')</script>";}
-
- }
-
- ?>
View_users.php
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css"> <!--css file link in bootstrap folder-->
- <title>View Users</title>
- </head>
- <style>
- .login-panel {
- margin-top: 150px;
- }
- .table {
- margin-top: 50px;
-
- }
-
- </style>
-
- <body>
-
- <div class="table-scrol">
- <h1 align="center">All the Users</h1>
-
- <div class="table-responsive"><!--this is used for responsive display in mobile and other devices-->
-
-
- <table class="table table-bordered table-hover table-striped" style="table-layout: fixed">
- <thead>
-
- <tr>
-
- <th>User Id</th>
- <th>User Name</th>
- <th>User E-mail</th>
- <th>User Pass</th>
- <th>Delete User</th>
- </tr>
- </thead>
-
- <?php
- include("database/db_conection.php");
- $view_users_query="select * from users";
- $run=mysqli_query($dbcon,$view_users_query);
-
- while($row=mysqli_fetch_array($run))
- {
- $user_id=$row[0];
- $user_name=$row[1];
- $user_email=$row[2];
- $user_pass=$row[3];
-
-
-
- ?>
-
- <tr>
- <!--here showing results in the table -->
- <td><?php echo $user_id; ?></td>
- <td><?php echo $user_name; ?></td>
- <td><?php echo $user_email; ?></td>
- <td><?php echo $user_pass; ?></td>
- <td><a href="delete.php?del=<?php echo $user_id ?>"><button class="btn btn-danger">Delete</button></a></td> <!--btn btn-danger is a bootstrap button to show danger-->
- </tr>
-
- <?php } ?>
-
- </table>
- </div>
- </div>
-
-
- </body>
-
- </html>
Db_conection.php
- <?php
-
-
-
-
-
-
- $dbcon=mysqli_connect("localhost","root","");
- mysqli_select_db($dbcon,"users");
- ?>
Delete.php
- <?php
-
-
-
-
-
-
- include("database/db_conection.php");
- $delete_id=$_GET['del'];
- $delete_query="delete from users WHERE id='$delete_id'";
- $run=mysqli_query($dbcon,$delete_query);
- if($run)
- {
-
- echo "<script>window.open('view_users.php?deleted=user has been deleted','_self')</script>";
- }
-
- ?>