php Login And Registration Example
In this article, we will explain to you how to create a User Login logout and register example in PHP (PHP Login And Registration Example). Sometimes when creating an admin panel or user panel that time we need to Registration, Login, and logout in PHP.
Create database table in MySQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CREATE TABLE `user` ( `id` int(11) NOT NULL, `first_name` varchar(255) NOT NULL, `last_name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ALTER TABLE `user` ADD PRIMARY KEY (`id`); ALTER TABLE `user` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; COMMIT; |
Connect to Database
Create a new connection.php file. The code below is to provide how to connect with the database.
The following code is used to connect MySQL from PHP. It requires hostname, database username, database password, and database name.
connection.php
1 2 3 4 5 6 7 8 9 10 | <?php $hostname="localhost"; $username="root"; $password="test"; $database="php_login"; $conn = mysqli_connect($hostname,$username,$password,$database); ?> |
Login form in PHP and MySQL
Create a PHP file index.php in this file in add the below code. in this example, we will create the login form using PHP. After the submit the login button then we will check user credentials are valid or not. if credentials are valid then we will store them in the session and redirect the user dashboard 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 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 | <?php session_start(); if(isset($_POST['btnLogin'])) { include('connection.php'); $email = $_POST['email']; $password = md5($_POST['password']); if(!empty($email) && !empty($password)){ $sql=mysqli_query($conn,"SELECT * FROM user where email='$email' and password='$password'"); $row = mysqli_fetch_array($sql); if(is_array($row)) { $_SESSION["id"] = $row['id']; $_SESSION["email"]=$row['email']; $_SESSION["first_name"]=$row['first_name']; $_SESSION["last_name"]=$row['last_name']; header("Location: dashboard.php"); } else { $msg="invalid login credentials"; header ("Location: index.php?error=".$msg); exit; } } else{ $msg="All fields are mandatory"; header ("Location: index.php?error=".$msg); exit; } } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title></title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="register.php">Register</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <?php $id= $_SESSION["id"]; if ($id): ?> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="dashboard.php">Dashboard</a> </li> </ul> <ul class="navbar-nav my-2 my-lg-0"> <li class="nav-item"> <a class="nav-link" href="logout.php">Logout</a> </li> </ul> <?php else: ?> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="index.php">Login</a> </li> <li class="nav-item"> <a class="nav-link" href="register.php">Register</a> </li> </ul> <?php endif; ?> </div> </div> </nav> <div class="container"> <div class="row"> <div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white from-wrapper"> <div class="container"> <h3>Login</h3> <hr> <?php if (isset($_REQUEST['success'])): ?> <div class="alert alert-success" role="alert"> <?php echo $_REQUEST['success']; ?> </div> <?php endif; ?> <form class="" method="post"> <div class="form-group"> <label for="email">Email address</label> <input type="text" class="form-control" name="email" id="email" value="<?php //set_value('email') ?>"> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" name="password" id="password" value=""> </div> <?php if (isset($_REQUEST['error'])): ?> <div class="col-12"> <div class="alert alert-danger" role="alert"> <?php echo $_REQUEST['error']; ?> </div> </div> <?php endif; ?> <div class="row"> <div class="col-12 col-sm-4"> <button type="submit" name="btnLogin" class="btn btn-primary">Login</button> </div> <div class="col-12 col-sm-8 text-right"> <a href="register.php">Don't have an account yet?</a> </div> </div> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |
Registration form in PHP and MySQL
Create a new PHP file register.php in this file in add the below code. in this example, we will create the user registration form using PHP. After the submit the register button then we will check all fields are mandatory or not. if all fields are not empty then we will store them in the database and redirect the user login 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 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | <?php if(isset($_POST['btnRegister'])) { include('connection.php'); $email = $_POST['email']; $sql=mysqli_query($conn,"SELECT * FROM user where email='$email'"); if(mysqli_num_rows($sql)>0) { $msg="Email Id Already Exists"; header ("Location: register.php?error=".$msg); exit; } else { $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $password = md5($_POST['password']); if(!empty($first_name) && !empty($last_name) && !empty($email) && !empty($password)){ $insert="insert into user (first_name,last_name,email,password) values('$first_name','$last_name','$email','$password')"; mysqli_query($conn,$insert); $msg="your account has been registered successfully"; header('location:index.php?success='.$msg); exit; } else{ $msg="All fields are mandatory"; header ("Location: register.php?error=".$msg); exit; } } } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title></title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="register.php">Register</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <?php $id= $_SESSION["id"]; if ($id): ?> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="dashboard.php">Dashboard</a> </li> </ul> <ul class="navbar-nav my-2 my-lg-0"> <li class="nav-item"> <a class="nav-link" href="logout.php">Logout</a> </li> </ul> <?php else: ?> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="index.php">Login</a> </li> <li class="nav-item"> <a class="nav-link" href="register.php">Register</a> </li> </ul> <?php endif; ?> </div> </div> </nav> <div class="container"> <div class="row"> <div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white from-wrapper"> <div class="container"> <h3>Register</h3> <hr> <form class="" method="post"> <div class="row"> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="firstname">First Name</label> <input type="text" class="form-control" name="firstname" id="firstname" value=""> </div> </div> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="lastname">Last Name</label> <input type="text" class="form-control" name="lastname" id="lastname" value=""> </div> </div> <div class="col-12"> <div class="form-group"> <label for="email">Email address</label> <input type="text" class="form-control" name="email" id="email" value=""> </div> </div> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" name="password" id="password" value=""> </div> </div> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="password_confirm">Confirm Password</label> <input type="password" class="form-control" name="password_confirm" id="password_confirm" value=""> </div> </div> <?php if (isset($_REQUEST['error'])): ?> <div class="col-12"> <div class="alert alert-danger" role="alert"> <?php echo $_REQUEST['error']; ?> </div> </div> <?php endif; ?> </div> <div class="row"> <div class="col-12 col-sm-4"> <button type="submit" name="btnRegister" class="btn btn-primary">Register</button> </div> <div class="col-12 col-sm-8 text-right"> <a href="index.php">Already have an account</a> </div> </div> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |
Logout example in PHP
Create a new logout.php file in this file to add the below code. in this example, when the user clicks on the logout link then we will clear all sessions and redirect the login page.
1 2 3 4 5 6 7 8 | <?php session_start(); unset($_SESSION["id"]); unset($_SESSION["email"]); unset($_SESSION["first_name"]); unset($_SESSION["last_name"]); header("Location:index.php"); ?> |
Create dashboard using PHP
in this example, we are displaying the current user’s first name and last name on the dashboard page. so users can understand and check login.
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 | <?php session_start(); $id= $_SESSION["id"]; if(empty($id)){ header ("Location: index.php"); exit; } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title></title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="register.php">Register</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <?php $id= $_SESSION["id"]; if ($id): ?> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="dashboard.php">Dashboard</a> </li> </ul> <ul class="navbar-nav my-2 my-lg-0"> <li class="nav-item"> <a class="nav-link" href="logout.php">Logout</a> </li> </ul> <?php else: ?> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="index.php">Login</a> </li> <li class="nav-item"> <a class="nav-link" href="register.php">Register</a> </li> </ul> <?php endif; ?> </div> </div> </nav> <div class="container"> <div class="row"> <div class="col-12"> <h1>Hello, <?= ucwords($_SESSION['first_name'].' '.$_SESSION['last_name']) ?></h1> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |
Read Also
PHP CRUD operation with MySQL
Create Rest Api Using Php And Mysql
CRUD Operation Using JSON File In PHP