MEMBUAT WEBSITE - FORM LOGIN DI PHP

CARA MEMBUAT FORM LOGIN DI PHP.sangat lah mudah cukup copykan kan coding di bawah ini

di Editor anda .saya biasa pakai Visual code




 <?php 

session_start();
require 'functions.php';

// cek cookie
ifisset($_COOKIE['id']) && isset($_COOKIE['key']) ) {
    $id = $_COOKIE['id'];
    $key = $_COOKIE['key'];

    // ambil username berdasarkan id
    $result = mysqli_query($conn"SELECT username FROM user WHERE id = $id");
    $row = mysqli_fetch_assoc($result);

    // cek cookie dan username
    if$key === hash('sha256'$row['username']) ) {
        $_SESSION['login'] = true;
    }


}

ifisset($_SESSION["login"]) ) {
    header("Location: index.php");
    exit;
}


ifisset($_POST["login"]) ) {

    $username = $_POST["username"];
    $password = $_POST["password"];

    $result = mysqli_query($conn"SELECT * FROM user WHERE username = '$username'");

    // cek username
    ifmysqli_num_rows($result) === 1 ) {

        // cek password
        $row = mysqli_fetch_assoc($result);
        ifpassword_verify($password$row["password"]) ) {
            // set session
            $_SESSION["login"] = true;

            // cek remember me
            ifisset($_POST['remember']) ) {
                // buat cookie
                setcookie('id'$row['id'], time()+60);
                setcookie('key'hash('sha256'$row['username']), time()+60);
            }

            header("Location: index.php");
            exit;
        }
    }

    $error = true;

}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Halaman Login</title>
</head>
<body>

<h1>Halaman Login</h1>

<?php ifisset($error) ) : ?>
    <p style="color: red; font-style: italic;">username / password salah</p>
<?php endif?>

<form action="" method="post">

    <ul>
        <li>
            <label for="username">Username :</label>
            <input type="text" name="username" id="username">
        </li>
        <li>
            <label for="password">Password :</label>
            <input type="password" name="password" id="password">
        </li>
        <li>
            <input type="checkbox" name="remember" id="remember">
            <label for="remember">Remember me</label>
        </li>
        <li>
            <button type="submit" name="login">Login</button>
        </li>
    </ul>
    
</form>
</body>
</html>




UNTUK LENGKAPNYA BISA DI DOWNLOAD DI LINK GDRIVE SAYA :https://drive.google.com/file/d/1k5m0dox7FpLWEp6CSdTIpTg4JjJRV6AW/view?us

Komentar

Posting Komentar