如何保护我的PHP和JavaScript jQuery的AJAX调用? [英] How to secure my jQuery AJAX calls in PHP and Javascript?
问题描述
我有以下PHP和JavaScript code片段中,我想提出一个jQuery的AJAX调用从PHP获取数据并显示在HTML中。
I have following PHP and Javascript code snippet in which I am making an jQuery AJAX call to get data from PHP and show it in the HTML.
PHP code
<?php
myfunction();
function myfunction()
{
$myvar = $_POST['q']." how are you?";
$myvar2 = $_POST['z'];
echo $myvar."\n".$myvar2;
}
?>
HTML code
<div id="mydiv"></div>
Javascript code
var data =" hello world";
var data2=" hello all";
function run()
{
$.ajax(
{
url: 'myscript.php',
data: {'q': data,'z':data2},
type: 'post',
success: function(output)
{
//alert(output);
document.getElementById("mydiv").innerHTML += output; //add output to div
}
}
);
}
以上code是工作的罚款。
Above code is working fine.
我要保护这个AJAX调用prevent从黑客,因为我在做一个AJAX调用这是大家有目共睹的。这code是容易受到黑客的攻击。我的问题是什么,我应该在哪里施加额外的检查,使我的AJAX调用安全吗?
I want to secure this AJAX call to prevent it from hackers because I am making an AJAX call which is visible to all. This code is vulnerable to hackers attack. My question is what and where should I impose extra checks to make my AJAX call secure?
有一件事我知道,我应该用以下code片段在我的PHP文件
One thing I know that I should use following code snippet in my PHP file
if (!$_POST['q'] && !$_POST['z'])
{
exit;
}|
else
{
myfunction(); //call myfunction only if page is posted
}
我应该在PHP和JavaScript文件用什么额外的检查?
What extra checks should I use in PHP and Javascript files?
推荐答案
您正在使用POST即好。
You are using POST that is good.
- 更多,您可以使用会话的安全性。
- 为code安全性,你可以把你的函数和其他重要code在一些不同的PHP文件,并把它们包含在主文件。为了让您的code可见的直接。
这篇关于如何保护我的PHP和JavaScript jQuery的AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!