我有這樣的代碼對某些圖像,像這樣:更新數據庫按鏈接
<img src="../images/woods/oak.png" onclick="loadXMLDoc(this), add_items_oak(this), add_skills_oak(this)" class="a" name="oak"/>
<img src="../images/woods/oak.png" onclick="loadXMLDoc(this), add_items_oak(this), add_skills_oak(this)" class="b" name="oak" />
當按下其中的一個,一個Ajax調用運行:
function loadXMLDoc(h)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var getEle = document.getElementsByClassName('woods1')[0];
var imagePath ="../images/woods/oak_cut.png";
h.src = imagePath + xmlhttp.responseText;
}
}
xmlhttp.open("POST","../database/update.php",true);
xmlhttp.send();
}
,然後更新該數據庫使用此文件:
<?php
require('../includes/db_connect.php');
/* Register a prepared statement */
if ($stmt = $mysqli->prepare('
UPDATE items_woods t1 JOIN skills_woodcutting t2
ON t1.id = t2.id
SET t1.oak = oak+1,
t2.exp = exp+13
WHERE t1.id = ?;
')) {
/* Bind parametres */
$stmt->bind_param('i', $id);
/* Insert the parameter values */
$id = 1;
/* Execute the query */
$stmt->execute();
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terrible wrong' . $mysqli->error;
}
?>
現在的問題是,人們可以使用網址來模擬點擊一個點擊e圖像。就像寫這樣的東西:www.examplepage.com/database/update.php
我需要update.php
來檢查是否一個圖像被點擊。
我試過一些代碼,它的<img>
標記的值設置爲true,並且只有在update.php
處的值爲真時,它纔會執行代碼。這並沒有幫助,因爲我試圖找到一個不能被欺騙的解決方案,所以我想在這裏問。任何建議或建議?提前致謝。
謝謝!通過使用這個,以及Collin Grady的回答,我可以成功地製作一個非常安全的樹形圖像:) – user3287771
但是請注意,對於真正想欺騙虛假帖子的人來說,這是微不足道的。你可以加載jquery到一個頁面,只需要$ .post(「someurl」)' –