阻止用户访问浏览器导航栏

时间:2016-08-21 20:32:30

标签: php jquery ajax

<?php
if(isset($_SESSION['id'])==null){


    header("Location: login.html");
}
?>
<script>
$(function(){
$(document).on("click", ".button", function(e) {
e.preventDefault();
{       
//This will capture all the inputs from the form
var infom = $("#myform").serialize();
        //$("#showresult").addClass('loader');                
        $.ajax({            
            beforeSend: function() { },         
            type: "POST",
            url: "login.php",
            data:infom,          
            success: function(result){      
            //$("#showresult").removeClass('loader');
            $('#showresult').html(result);
            }
        });     
        e.preventDefault(); 
}
}); 
});
</script>

在login.php文件中我放置但是它没有做什么,用户仍然可以在浏览器导航中放入login.php并仍然可以访问它。我想阻止他们这样做。我希望该文件仅在我尝试登录登录html页面时调用。上面是用于显示错误消息的ajax代码,如果有任何来自php。当我将它与if post或http reffer结合使用时,它会重定向到一个连续加载的空白页面。

2 个答案:

答案 0 :(得分:0)

您可以查看

if($_SERVER['HTTP_REFERER']=="complete URL/login.html")

网址必须与http://www.example.com/login.html

类似

如果您要从登录状态提交表单,则必须检查$ _POST。

答案 1 :(得分:0)

的login.php

// check if form has been submitted
IF (!isset($_POST)) { 
    Header('Location: login.html');
    die();

}

使用表单令牌会更明智。