session_auth/cakey.php


Home Back


<?php
    // Valid key 
    $key[0] = '56ab89';
    $key[1] = '491ads';
    
    $login_error='<h2>Login error</h2><script>window.location = "index.php"; </script>';
    $htmlFormLogin='';
    
    
    function checkAuth($mkey)
    {
        global $key;
        if (in_array($mkey, $key) )
        {
            return true;
        }else{
            return false;
        }
    }
    
    function checkKey($mkey)
    {
        if (ctype_alnum($mkey)) {
           return true;
        }else{
            return false;
        }
    }
    
    
    
    // Not edit
    session_start(); 
    
    if (isset($_POST["login_btn"]))
    {
        
        if (!checkKey($_POST["mkey"]) )
        {
            echo "<p>Error...</p>";
            die();
        }
        
        
        $mkey=filter_var($_POST["mkey"], FILTER_SANITIZE_STRING);
        
        if (checkAuth($mkey))
        {
            //logged in
            $_SESSION["mkey"]=$mkey;
            return;
            
        }else{
            //login error
            echo $login_error;    
            die();
        }
    }else{
        if (isset($_SESSION["mkey"] ))
        {
            if (checkAuth($_SESSION["mkey"])) return ;
        }
        if (!empty($htmlFormLogin))
        {
            echo $htmlFormLogin;
        }else{    
            echo '
            <!DOCTYPE html >
            <html >
            <head>
                <title>Login</title>
                <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            </head>

            <body>
                
                <h2>Login</h2>
                <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
                <div><label> Key: <input id="mkey" name="mkey" type="text" placeholder="Insert your key"></label></div>
                <div><button id="login_btn" name="login_btn" >Login</button></div>
                </form>
            </body>';
        }
        
        
        die();
    
    
    }
?>

Powered by Code, a simple repository browser by Fabio Di Matteo