<?php
//Start session
        session_start();

        //Include database connection details
        require_once('connection.php');

        //Array to store validation errors
        $errmsg_arr = array();

        //Validation error flag
        $errflag = false;

        //Function to sanitize values received from the form. Prevents SQL injection
        function clean($str) {
                echo "str: ".$str;
                $str = @trim($str);
                if(get_magic_quotes_gpc()) {
                        $str = stripslashes($str);
                }
                return mysqli_real_escape_string($str);
        }

        //Sanitize the POST values
        $email = $_POST['email'];
        $password = $_POST['password'];

        //Input Validations
        if($email == '') {
                $errmsg_arr[] = 'email is missing';
                $errflag = true;
        }
        if($password == '') {
                $errmsg_arr[] = 'Password is missing';
                $errflag = true;
        }

        //If there are input validations, redirect back to the account form
        if($errflag) {
                $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
                session_write_close();
                header("location: login.php");
                exit();
        }




        //Create query
        $qry="SELECT * FROM users WHERE email='$email' AND password='$password'";
        $result=mysqli_query($bd, $qry);

        //Check whether the query was successful or not
        if($result) {
                if(mysqli_num_rows($result) > 0) {
                        //account Successful
                        session_regenerate_id();
                        $member = mysqli_fetch_assoc($result);
                        $_SESSION['SESS_MEMBER_ID'] = $member['sno'];
                        $_SESSION['SESS_FIRST_NAME'] = $member['email'];
                        $_SESSION['SESS_LAST_NAME'] = $member['password'];
                       
                        

                        $_SESSION['username'] = $member['username'];
                    

            
                        $_SESSION['password'] = $member['password'];
                        $_SESSION['email'] = $member['email'];
                   

                       


                        session_write_close();
                        header("location: admin.php");
                        exit();







                }else {
                        //account failed
                        $errmsg_arr[] = 'email  and Password not found';
                        $errflag = true;
                        if($errflag) {
                                $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
                                session_write_close();
                                header("location: login.php");
                                exit();
                        }
                }
        }else {
                die("Query failed");
        }
?>
Creating Our Authentication File
Next step is to create our authentication file and save it as
auth.php. this code is use to disallow the user to go back as if it
still logged in.

<?php
        //Start session
        session_start();
        //Check whether the session variable SESS_MEMBER_ID is present or not
        if(!isset($_SESSION['SESS_MEMBER_ID']) ||
(trim($_SESSION['SESS_MEMBER_ID']) == '')) {
                header("location: login.php");
                exit();
        }
?>