Today, we will explain to you how to create a Login, logout, and register example in PHP using PDO (login and registration example using PDO). Sometimes when creating an admin panel or user panel that time we need to Registration, Login, and log out in PHP(pdo register and login).
We will give you a simple example of Signup and Login operation using PDO. so you can see our PHP PDO tutorial.
Create database table in MySQL
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
<?php // DB credentials. $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_name = 'php_login'; try { $db = new PDO("mysql:host=".$db_host.";dbname=".$db_name,$db_user, $db_pass); } catch (PDOException $e) { exit("Error: " . $e->getMessage()); } ?>
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.
<?php session_start(); if(isset($_POST['btnLogin'])) { include('connection.php'); $email = $_POST['email']; $password = md5($_POST['password']); if(!empty($email) && !empty($password)){ $slt="select * from user where email=:email and password=:password"; $query=$db->prepare($slt); $query->bindParam(':email',$email,PDO::PARAM_STR); $query->bindParam(':password',$password,PDO::PARAM_STR); $query->execute(); $row=$query->fetch(PDO::FETCH_ASSOC); 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 PDO registration form. 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.
<?php if(isset($_POST['btnRegister'])) { include('connection.php'); $email = $_POST['email']; $slt="select * from user where email=:email"; $query=$db->prepare($slt); $query->bindParam(':email',$email,PDO::PARAM_STR); $query->execute(); $row=$query->fetch(PDO::FETCH_ASSOC); if($query->rowCount() > 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)){ $sql="insert into user (first_name,last_name,email,password) values(:first_name,:last_name,:email,:password)"; $query = $db->prepare($sql); $query->bindParam(':first_name',$first_name,PDO::PARAM_STR); $query->bindParam(':last_name',$last_name,PDO::PARAM_STR); $query->bindParam(':email',$email,PDO::PARAM_STR); $query->bindParam(':password',$password,PDO::PARAM_STR); $query->execute(); $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 user clicks on logout link then we will clear all session and redirect the login page.
<?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.
<?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>