Saturday, May 30, 2026

Set Cookie dan Remove Cookie dengan php vanilla.js

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button type="button" onclick="abc()">SetCookie</button><br/>
    <button type="button" onclick="def()">RemoveCookie</button><br/>
    <?php
        if (isset($_COOKIE['username'])) {
            echo "Username: " . $_COOKIE['username']."<br>";
            $decoded = base64_decode($_COOKIE['username']);
            if($decoded){
                echo $decoded;
            }else{
                echo "gagal";
            }
        } else {
            echo "Cookie tidak ditemukan";
        }
    ?>
</body>
<script>
    function abc(){
        const bbb = 'aqresdtcghxi@gmail.com';
        const ccc = btoa(bbb);
        if(document.cookie = `username=${ccc}`){
            alert('cookie set');
           window.location.reload();
        }else{
            alert('cookie not set');
            window.location.reload();
        }

    }
    function def(){
       document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
       alert('cookie remove');
       window.location.reload();
    }
</script>
</html>

No comments:

Post a Comment

Menjadi Programmer Bukan Hanya Tentang Coding, Tapi Tentang Mempermudah Kehidupan

Semenjak menjadi seorang programmer, ada satu cara pandang yang perlahan berubah dalam diri saya. Saya mulai berpikir bahwa ilmu pemrograman...