User's Account Activate/Deactivate

User's Account Activate/Deactivate System will teach you how to disable and enable user's account. This simple system gives the information for the user's if their account has been deactivated by the admin. The admin side is the only one can manage the account of every users to activate or deactivate it. And this is compose of PHP, Bootsrap and MySQL, this programs are written in a way that any one can understand and customize for their projects.

Sample Code

Index.php - This script is for the login of the user sessions and for the authenticating if the user is active or deactivate.
  1. <?php
  2.         if(isset($_POST['submit'])) {
  3.                 $username = $_POST['username'];
  4.                 $password = $_POST['password'];
  5.                 $result = $dbh->prepare("SELECT * FROM tb_users WHERE username= :username AND password= :password");
  6.                 $result->bindParam(':username', $username);
  7.                 $result->bindParam(':password', $password);
  8.                 $result->execute();
  9.                 $rows = $result->fetch(PDO::FETCH_NUM);
  10.                 if($rows > 0) {
  11.                         $result=$dbh->prepare("SELECT * FROM tb_users WHERE username=:username");
  12.                         $result->bindParam(':username', $username);
  13.                         $result->execute();
  14.                         while($row = $result->fetch(PDO::FETCH_ASSOC)){
  15.                                 $res_id = $row['id'];
  16.                                 $curr_status = $row['user_status'];
  17.                         }
  18.                                 if($curr_status=='Deactive') {
  19.                                         $message = "Sorry <b>$username</b>, your account is temporarily deactivated by the admin.";
  20.                                 }else{
  21.                                         $_SESSION['id'] = $res_id;
  22.                                         header("location: admin/index.php?logid=$res_id");
  23.                                 }
  24.                 }
  25.                 else{
  26.                         $message = 'Username and Password are not exists.';
  27.                 }
  28.         }
  29. ?>
Result UpdateStatus.php - And for the updating of users account to call and to active or deactivate.
  1. <?php
  2.         session_start();
  3.         if(!isset($_SESSION['id'])) {
  4.                 header('Location:../index.php');
  5.         }
  6.        
  7.         require_once('../config.php');
  8.        
  9.         $get_userid = $_GET['userid'];
  10.        
  11.         $result=$dbh->prepare("Select * From tb_users Where id='$get_userid'");
  12.         $result->execute();
  13.         while($row = $result->fetch(PDO::FETCH_ASSOC)){
  14.                 echo $curr_status = $row['user_status'];
  15.         }
  16.                
  17.         if($curr_status == "Active") {
  18.                 $sql = "UPDATE tb_users
  19.        SET user_status=?
  20.                 WHERE id=?";
  21.                
  22.                 $this_status = "Deactive";
  23.                 $q = $dbh->prepare($sql);
  24.                 $q->execute(array($this_status, $get_userid));
  25.                 header("location: index.php");
  26.         } else {
  27.                 $sql = "UPDATE tb_users
  28.        SET user_status=?
  29.                 WHERE id=?";
  30.                
  31.                 $this_status = "Active";
  32.                 $q = $dbh->prepare($sql);
  33.                 $q->execute(array($this_status, $get_userid));
  34.                 header("location: index.php");
  35.         }
  36. ?>
Config.php - For the configuration of the system to the database using PDO.
  1. <?php
  2. class Database extends pdo {
  3.  
  4.     private $dbtype;
  5.     private $host;    
  6.     private $user;
  7.     private $pass;
  8.     private $database;
  9.  
  10.     public function __construct(){
  11.         $this->dbtype = 'mysql';
  12.         $this->host = 'localhost';
  13.         $this->user = 'root';
  14.         $this->pass = '';
  15.         $this->database = 'sqlact';
  16.         $dns = $this->dbtype.':dbname='.$this->database.";host=".$this->host;
  17.         parent::__construct( $dns, $this->user, $this->pass );
  18.     }    
  19. }
  20. $database = new Database();
  21. $dbh =& $database;
  22. ?>
Result Hope that you learn in this tutorial. And for more updates and programming tutorials don't hesitate to ask and we will answer your questions and suggestions. Don't forget to LIKE & SHARE this website.

Add new comment