index.php




<?php  require_once("resources/config.php") ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <title>formLogin</title>
</head>
<body>
    <div class="top_center" align="center">
        <div class="center">
            <h1>សូមស្វាគមន៍</h1>
            <img src="img/loog.png" height="200px" alt=""><br>
            <?php display_message();?>
            <form action="" method="post">
                <div class="txt_field">
                    <input type="text" name="username" placeholder="Username" required>
                </div>
                <div class="txt_field">
                    <input type="password" name="password" placeholder="Password" required>
                </div>
                <input type="submit" name="submit" value="ចូលប្រើ">
            </form>
        </div>

    </div>
<?php login_user();?>
    <div class="container">
        <footer>
            <div class="row">
                <div class="col-lg-12">
                    <p>Deverlop By: Tongheng-T</p>
                </div>
            </div>
        </footer>
    </div>
   
</body>
</html>





style.css


body{
    background: linear-gradient(270deg,#71b7e6,#1350f8);
}
.row .col-lg-12{
    padding: 20px;
}
.center{
    max-width: 380px;
    padding: 10px 0 30px;
    border-bottom: 1px solid rgba(58,58,54);
    background: white;
    border-radius: 10px;
    box-shadow: 10px 10px 15px rgba(05, 0, 0,0.05);
}
.center h1{
    text-align: center;
    padding: 20px 0;
    font-size: 18px;
    font-family: khmer OS Battambang;
    border-bottom: 1px solid silver;
}
.top_center{
    max-width: 1000px;
    margin: auto;
}
.center form{
    padding: 0 40px;
    box-sizing: border-box;
}
form .txt_field{
    position: relative;
    margin: 30px 0;
}
.txt_field input{
    width: 100%;
    padding: 0 5px;
    height: 41px;
    font-size: 16px;
    outline: none;
}
input[type="submit"]{
    width: 100%;
    height: 50px;
    border: 1px solid;
    border-radius: 25px;
    font-family: khmer OS Battambang;
    background: #2691d9;
    font-size: 18px;
    color: white;
    font-weight: 500;
    cursor: pointer;
    outline: none;
}
input[type="submit"]:hover{
    border-color: #2691d9;
    transition: .5s;
}



config.php


<?php

session_start();

defined("DB_HOST")? null : define("DB_HOST","localhost");
defined("DB_USER")? null : define("DB_USER","root");
defined("DB_PASS")? null : define("DB_PASS","");
defined("DB_NAME")? null : define("DB_NAME","tes_login");

$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);      

require_once("functiones.php")

?>



functiones.php

<?php

function set_message($msg){
    if(!empty($msg)){
        $_SESSION['message']=$msg;
    }else{
        $msg = "";
    }
}

function display_message(){
    if(isset($_SESSION['message'])){
        echo $_SESSION['message'];
        unset($_SESSION['message']);
    }
}
function confirm($result){
    global $connection;
    if(!$result){
        die("QUERY FAILED" . mysqli_error($connection));
    }
}
function query($sql){
    global $connection;
    return mysqli_query($connection, $sql);
}
function redirect($location){
    header("Location: $location");
}
function login_user(){
    if(isset($_POST['submit'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
       
        $query = query("SELECT * FROM users WHERE name = '{$username}' AND password = '{$password}' ");
        confirm($query);
        if(mysqli_num_rows($query)== 0){
            set_message("Your password or username are wrong!");
            redirect("index.php"); //or header("Location: index.php");
        }else{
            $_SESSION['username'] = $username;
            redirect("client");
        }

    }
}

?>