Connect Bootstrap Login Form and Mysql with PDO

bootstrap login form

Lately I playing around with the bootstrap framework. I will share how to create simple login form with bootstrap and connect it with mysql using PDO. Bootstrap has an elegant look. combined with ajax and jquery , will make your application run faster.

 Why we must use PDO to create connection with mysql ? 

PDO – PHP Data Objects – is a database access layer providing a uniform method of access to multiple databases.

PDO stands for PHP Data Objects which is a database access layer in PHP version 5.xx. In addition to having fast performance, PDO also supports a wide range of RDBMS including MySQL, CUBRID, MS SQL Server, Firebird, IBM, Informix, MS SQL Server, Oracle, ODBC and DB2, Postegre SQL, SQLite, and 4D. Just a reminder, to use PDO to connect to the database, you need to be a little familiar with OOP.

Requirement :

Download AdminLTE Bootstrap template here

 Lets begin tutorial

1. Create Login form as php file (login.php)

<!DOCTYPE html>
<html class="bg-black">
        <meta charset="UTF-8">
        <title>Your title</title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
        <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
        <link href="css/AdminLTE.css" rel="stylesheet" type="text/css" />
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src=""></script>
          <script src=""></script>
    <body class="bg-black inverse">
        <div class="form-box" id="login-box">
            <div class="header">Login Form</div>
            <form action="login_val.php" method="post">
                <div class="body bg-gray">
                    <div class="form-group">
                        <input type="text" name="username" required autofocus class="form-control" placeholder="Username"/>
                    <div class="form-group">
                        <input type="password" name="password" required class="form-control" placeholder="Password"/>
                <div class="footer">
                    <button type="submit" class="btn bg-olive btn-block">Login</button>
								echo ' <div class="box-body"> <div class="callout callout-warning">';
										echo "Please fill username or password.";
										echo "The username and password you entered did not match our records. Please double-check and try again.";

								echo '</div></div>';

        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js" type="text/javascript"></script>

2. Create PDO class (pdo_db.php).

class core_db extends PDO{
    private $engine;
    private $host;
    private $database;
    private $user;
    private $pass;

    private $result;    

    public function __construct()
        $this->engine   = 'mysql';
        $this->host     = 'localhost';
        $this->database = 'yourdatabase';
        $this->user     = 'root';
        $this->pass     = '';

        $dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
        parent::__construct( $dns, $this->user, $this->pass );

3. Create validation form (validation.php)

$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) && empty($password)) {
try {
$database= new core_db;
$hasil=$database->prepare('select a.id_user,a.nama_user,a.level from t_user a where a.nama_user="'.$username.'" and a.pass_user='.$password);
$database->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
catch(Exception $e) {
    echo 'Exception -> ';
if ($jum >=1)
	while($data = $hasil->fetch(PDO::FETCH_ASSOC))
		$_SESSION['ses_nama_user'] = $data['nama_user'];
		$_SESSION['ses_id'] = $data['id_user'];
		$_SESSION['ses_level'] = $data['level'];

Thus tutorials Connect Bootstrap Login Form and MySQL with PDO, please read my other bootstrap tutorial here

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: