使用 AJAX 和 PHP 验证简单密码

Using AJAX and PHP to validate a simple password

我有一个希望很简单的问题需要解决。我们正在尝试使用一个简单的密码来保护我们网站安全部分内的 link。基本上,在我们传统的安全站点内,我们有一个动态驱动的 table 记录,带有一个小挂锁图标,可以在锁定和解锁之间来回切换。网站的这一部分传统上已经受到保护,只有授权用户才能访问。因此,我们只想实现这种不太安全的静态 PHP 密码身份验证系统,允许用户在此 table 中锁定和解锁记录。基本上一旦他们在 table 中创建记录,他们就可以选择单击锁定图标(这显然会锁定记录),但只有那些具有简单硬编码 pin 的人才能在记录被锁定后解锁(即所有用户可以锁定,但并非所有用户都可以解锁)。因此,我尝试编写一个简单的 PHP + AJAX 引脚系统。我的其他管理员坚持使用服务器端解决方案。他拒绝为此使用基本的 javascript,我同意。它仍然需要一定程度的安全性。到目前为止,这是我所拥有的,但我对 AJAX 很陌生(几分钟),显然有些东西不起作用。这是一个名为 pin.php 的文件(所以基本上页面提交给自己)

UNLOCKlink模拟用户点击解锁记录的锁图标

<html>
<script src="js/jquery.js" type="text/javascript"></script>
<?php
    $static_password = "1234";
    if(isset($_POST['data'])){
        $submit_password = $_POST['data'];
        if($submit_password == $static_password){
            echo "Do the unlock stuff";
        }
        else{
            echo "Sorry try again";
        }
    }
?>
<body>
<h2>Simple AJAX PHP Example</h2>
<a href="javascript:Unlock();">UNLOCK</a>
<script>
function Unlock() {
    var pin=prompt("You must enter pin to unlock");
    $.ajax(
    {
        url: 'pin.php',
        type: 'POST',
        dataType: 'text',
        data: {data : pin},
        success: function(response)
        { 
            console.log(response);
        }
    });
}
</script>
</body>
</html>

非常感谢任何帮助。谢谢

这符合我的预期:

<?php
    $static_password = "1234";
    if(isset($_POST['data'])){
        $submit_password = $_POST['data'];
        if($submit_password == $static_password){
            die("Do the unlock stuff");
        }
        else{
            die("Sorry try again");
        }
    }
?><!DOCTYPE html><html>
<head>
    <script src="jquery-3.1.0.min.js" type="text/javascript"></script>
</head>
<body>
<h2>Simple AJAX PHP Example</h2>
<a href="javascript:Unlock();">UNLOCK</a>
<script>
function Unlock() {
    var pin=prompt("You must enter pin to unlock");
    $.ajax(
    {
        url: 'pin.php',
        type: 'POST',
        dataType: 'text',
        data: {data : pin},
        success: function(response)
        { 
            console.log(response);
        }
    });
}
</script>
</body>
</html>