IDIOT DEVELOPER

User Registration System in PHP Using PDO
User Registration System in PHP Using PDO

In this post we are going to learn about creating a user registration system in PHP using PDO for database connection. As we new PDO is a new technology, and is more secure than MySql and MySqli.

 

User Table Structures

 


CREATE TABLE `users` (
`uid` int NOT NULL PRIMARY KEY AUTO_INCREMENT ,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(200) NOT NULL 
);

 

Enable PDO extension for PHP, find this in php.ini configuration file.

 

Config.php

This file contains the configrations for the database connection, and has a class User, which has three methods in it – get_db(), user_registration(), and, get_name().

 

The get_db() method helps in database connectivity. The user_registration() helps the user in registeration. This function insert user details in the database. The get_name() method returns the user full name when provided with user id.

 


<?php 
@session_start();
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DBNAME', 'tut');
define("BASE_URL", "http://localhost/tut/pdo-login-registration/");

class User{

  function get_db(){
    try{
      $db = new PDO( 'mysql:host=' . HOST  . '; dbname=' . DBNAME, USERNAME, PASSWORD );
      $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      return $db;
    }catch(PDOExecption $e){
      echo 'Connection Failed: ' . $e->getMessage();
    }
  }

  function user_register($name, $email, $password){
    try{
      $db = $this->get_db();
      $query=$db->prepare("SELECT uid FROM users WHERE email=:email");
      $query->bindParam("email", $email, PDO::PARAM_STR);
      $query->execute();
      $row_count = $query->rowCount();

      if(row_count < 1){ 
        //Email Not found in the Database 
        //Insert Regsistration data 
        $query=$db->prepare("INSERT INTO users(name, email, password) VALUES(:name, :email, :password)");
        $query->bindparam("name", $name, PDO::PARAM_STR);
        $query->bindParam("email", $email, PDO::PARAM_STR);
        $encrypt_password = hash('sha256', $password);
        $query->bindParam("password", $encrypt_password, PDO::PARAM_STR);
        $query->execute();
        $db = null;
        return true;
      }else{
        return false; //Email found in the database
      }
    }catch(PDOExecption $e){
      echo "Error: " . $e->getMessage();
    }
  }

  function get_name($uid){
    //Get the username from the userid of a user.
    $db=$this->get_db();
    try{
      $query=$db->prepare("SELECT name FROM users WHERE uid=:uid");
      $query->bindParam("uid", $uid);
      $query->execute();
      $data=$query->fetch();
      $db=null;
      return $data['name'];
    }catch(PDOException $e){
      echo "Error: " . $e->getMessage();
    }
  }
}
?>

 

index.php

The index file contains the HTML and CSS code for the registration page, and it also contain some PHP codes.

 


<!DOCTYPE html>
<html>
<head>
<title> PDO - Registration and Login in PHP </title>
<link rel="stylesheet" href="../../bootstrap/css/bootstrap.css">

<style type="text/css">
body{
background: #f2f2f2;
}
.main{
background:none;
width:800px;
margin-left: auto;
margin-right: auto;
margin-top:30px;
padding:0px;
}

.register{
background:#fff;
width:350px;
display: inline-block;
margin-left:395px;
box-shadow: 1px 5px 10px #ccc;
padding:10px;
}
</style>
</head>

<body>

<?php
$err = 0;
if(isset($_GET['err'])){
$err = ($_GET['err']);
}
?>
<h2 align="center"> PDO -  Registration </h2>
<div class="main">

<!-- register starts -->
<div class="register">
<h3 align="center"> Register </h3>
<h4 align="center" style="color:olive;">
<?php if($err == 3){ echo 'Registration Success'; } ?>
</h4>
<form action="register.php" method="post">

<div class="form-group">
<label for="name"> Name </label>
<input type="text" class="form-control" id="name" name="name" required>
</div>

<div class="form-group">
<label for="email"> Email </label>
<input type="email" class="form-control" id="email" name="email" required>
<?php
if($err == 2){
echo '<span style="color:red"> Email already registered</span>';
}
?>
</div>

<div class="form-group">
<label for="password"> Password </label>
<input type="password" class="form-control" id="password" name="password" required>
</div>

<button type="submit" class="btn btn-primary"> Register </button>

</form>
</div>
<!-- register ends -->

</div>

</body>
</html>

 

register.php

This files helps a user in registration, it helps user to insert his details in the database.

 


<?php
include_once('config.php');

if(isset($_POST['email'])){
$email = ($_POST['email']);
$name = ($_POST['name']);
$password = ($_POST['password']);

$user = new User();
if(strlen($name) > 1 && strlen($email) > 1 && strlen($password) > 1){
//Checking if $name, $email, $password are not empty

$register = $user->user_register($name, $email, $password);

if($register == false){
header('location:index.php?err=2');
//index.php?err=2 ---- email already registered
}else{
header('location:index.php?err=3');
//index.php?err=3 ---- registeration success
}
}else{

}
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *