LERAN PHP COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

PHP MYSQL Login


In this tutorials we will learn about login page using Mysql Database. Before going into code part, You must know about MYSQL syntax i.e create, delete and update mysql tables. Click here to learn MYSQL

Create Config.php

Config.php file helps us to make connection with our mysql database.

<?php
   define('DB_SERVER', 'localhost:3036');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'rootpassword');
   define('DB_DATABASE', 'database');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?<

Note: If you are using XAMPP or WAMPP server then your default username and password are 'root' and '   '(empty) respectively.

Login.php

Login PHP is having information about php script and HTML script to do login.

<?php
    include('config.php');
    session_start();
?>

<html lang = "en">

<head>
    <title>Adzetech Login</title>
    <link href = "css/bootstrap.min.css" rel = "stylesheet">
    <style>
        body {
            padding-top: 40px;
            padding-bottom: 40px;
        }

        .form-signin {
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
            color: #017572;
        }

        .form-signin .form-signin-heading,
        .form-signin .checkbox {
            margin-bottom: 10px;
        }

        .form-signin .checkbox {
            font-weight: normal;
        }

        .form-signin .form-control {
            position: relative;
            height: auto;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding: 10px;
            font-size: 16px;
            margin-bottom: 10px;
        }

        .form-signin .form-control:focus {
            z-index: 2;
        }

        .form-signin input[type="email"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
            border-color:#260de4;
        }

        .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
            border-color:#260de4;
        }

        h2{
            text-align: center;
            color: #260de4;
        }
    </style>

</head>

<body>

    <h2>Enter Email and Password</h2>
    <div class = "container form-signin">

    <?php
        $msg = '';

        if (isset($_POST['login']) && !empty($_POST['email']) && !empty($_POST['password'])) {

            $email=$_POST['email'];
            $password=$_POST['password'];

            if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM account WHERE email='$email' && password='$password'")))
            {
                echo 'Login success';
            }else {
                $msg = 'Wrong username or password';
            }
        }
    ?>
    </div> <!-- /container --><div class = "container">

    <form class = "form-signin" action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method = "post">
        <h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
        <input type = "text" class = "form-control" name = "email" placeholder = "contact@adzetech.com" required autofocus></br>
        <input type = "password" class = "form-control" name = "password" placeholder = "password = admin" required><br />
        <button class = "btn btn-lg btn-primary btn-block" type = "submit"  name = "login">Login</button>
    </form>

</div>

</body>

Output

PHP and mysql login form

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine