PHP Login script

Firstly you need to start the session and include your database connection

                
                    session_start();
                    include '../includes/connect.php';
                
            

Next: Check username and password fields are submitted in the form and select username and password from database

                
                if(isset($_POST['submit']))
                    {
                        if(!empty($username = $_POST['username']) AND !empty($password = $_POST['password']))
                            {
                                $sql = "SELECT username, password FROM users WHERE username = '$username'";
                                $statement = $db->prepare($sql);
                                $statement->execute(array($username, $password));
                                $username = $statement->fetch();
                
            

If username and password are correct direct user to admin area or any area you decide for your project. If this is not correct return to login page with error message

                
                if($username && password_verify($_POST['password'], $username['password']))
                    {
                        $_SESSION['username'] = $_POST['username'];
                        header("location: ../admin/");
                    }
                else
                    {
                        header("location: ../login/?error=error");
                    }
                
            

Completed php login script

                
                    session_start();
                    include '../includes/connect.php';

                    if(isset($_POST['submit']))
                        {
                            if(!empty($username = $_POST['username']) AND !empty($password = $_POST['password']))
                                {
                                    $sql = "SELECT username, password FROM users WHERE username = '$username'";
                                    $statement = $db->prepare($sql);
                                    $statement->execute(array($username, $password));
                                    $username = $statement->fetch();
                                    if($username && password_verify($_POST['password'], $username['password']))
                                        {
                                            $_SESSION['username'] = $_POST['username'];
                                            header("location: ../admin/");
                                        }
                                    else
                                        {
                                            header("location: ../login/?error=error");
                                        }
                                }
                            else
                                {
                                    header("location: ../login/?error=required");
                                }
                        }
                
            

HTML form with error messages

            
                <?php
                    if(isset($_GET['error']))
                        {
                            if($_GET['error'] == "error")
                                {
                                    echo "<div class='alert alert-danger'>Password or username/email is incorrect>/div>";
                                }
                            elseif($_GET['error'] == "required")
                                {
                                    echo "<div class='alert alert-danger'>Userername and password both required>/div>";
                                }
                        }
                ?>
                <form action="../includes/login-script" method="post">
                    <div class="col-md-12 mb-2">
                        <div class="form-floating mb-2">
                            <input type="text" name="username" class="form-control" placeholder="usrename">
                            <label for="floatingInputGrid">Username</label>
                        </div>
                        <div class="form-floating mb-2">
                            <input type="password" name="password" class="form-control" placeholder="Password" id="password">
                            <span class="fa fa-fw field-icon toggle-password fa-eye"></span>
                            <label for="floatingInputGrid">Password</label>
                        </div>
                        <div class="form-floating mb-2">
                            <button class="btn btn-block btn-primary" type="submit" name="submit">Sign in</button>
                        </div>
                    </div>
                </form>
                
            

Simple Boostrap4 html login form.