ajax请求php省略后缀,如何在PHP中更安全地处理Ajax请求?
在谷歌搜索了很多次后,回答結束了!
步驟1:為所有Web服務生成令牌系統:
生成令牌:
session_start();
$token = md5(rand(1000,9999)); //you can use any encryption
$_SESSION['token'] = $token; //store it as session variable
?>
步驟2:發送Ajax調用時使用它:
var form_data = {
data: $("#data").val(), //your data being sent with ajax
token:'<?php echo $token; ?>', //used token here.
is_ajax: 1
};
$.ajax({
type: "POST",
url: 'yourajax_url_here',
data: form_data,
success: function(response)
{
//do further
}
});
步驟3:現在,讓我們用
session_start(); //most of people forget this while copy pasting code ;)
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
//Request identified as ajax request
if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']=="http://yourdomain/ajaxurl")
{
//HTTP_REFERER verification
if($_POST['token'] == $_SESSION['token']) {
//do your ajax task
//don't forget to use sql injection prevention here.
}
else {
header('Location: http://yourdomain.com');
}
}
else {
header('Location: http://yourdomain.com');
}
}
else {
header('Location: http://yourdomain.com');
}
注意:對不起,如果…否則嵌套,但它增加了可理解性。
如果不是這樣的話,你可以把這三個簡化為一個。85%的安全性增強!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ajax请求php省略后缀,如何在PHP中更安全地处理Ajax请求?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python列表转换成数字_python
- 下一篇: php theexcerpt,wordp