필자는 관리자가 페이지를 다시로드하지 않고도 포인트를 원활하게 제공 할 수 있도록 PHP와 javascript를 조합하여 작성했습니다. 이것은 Ajax를 사용하면서 처음으로 시도한 것이므로 코드 개선에 대한 조언이 필요합니다. 어떻게하면보다 안전하고 효율적으로 만들 수 있습니까? Btw,이 코드는 html 소스에서 볼 수 있습니다.이 Ajax 스크립트를 개선하려면 어떻게해야합니까?
P.S : 이와 관련하여 모범 사례가있는 경우 해당 주제에 대한 링크를 알려주고 게시하십시오.
는// First pass: add the points
function addBonus()
{
document.getElementById('response').innerHTML = '<img src=images/loading2.gif></img>'; // Show that process is taking place
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
verifyPoints(); // Check if points have actually been given
}
}
// Generated by templating system
// Triggers call to PHP page that gives bonus points to user
var url="page.php?id=2&a=bonus";
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
// Second pass: check the points have been given
function verifyPoints(){
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
url="GetPoints.php?i=2"; // Generated by templating system
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function HandleResponse(response)
{
var oldPoints = parseInt(document.getElementById('numPoints').innerHTML);
var currentPoints = parseInt(response);
if(currentPoints == (oldPoints + 150)){
document.getElementById('response').innerHTML = '<img src=images/tick.png></img>'; // Bonus points added: show tick icon
} else {
document.getElementById('response').innerHTML = '<img src=images/cross.png></img>'; // Bonus points not added: show red cross icon
}
document.getElementById('numPoints').innerHTML = currentPoints; // Update points display
}
위대한 답변 마이크. 계속할 많은 정보. 현재 page.php는 관리자가 보너스를 제공하고 있는지 확인하지 않습니다. ID를 전달하거나 페이지를 템플릿으로 만들고 거기에서 체크를 할 수 있습니다. GET 대신 POST를 사용하는 것이 옳습니다. 다음은 교차 사이트 공격에 대한 유용한 링크입니다. http://shiflett.org/articles/foiling-cross-site-attacks –