2014-05-13 42 views
0

我使用javascript保護我的html表單,因爲我的用戶只能更改輸入值,而且我想檢查輸入值是否等於隨機生成的數字。我正在使用if/else語句,而if語句不起作用。保護html表格不起作用

var pokeid = '+res.id+'; //generates random id 
var html = ''; 
html += '<form id="myForm" action="myurl.net" method="post">'; 
html += ' <input type="hidden" id="goid" name="goid" value="'+res.id+'" >'; 
html += ' <input type="hidden" name="level" value="'+res.level+'" />'; 
html += ' <input value="Battle!" class="button" onclick="myFunction()" />'; 

</script> 

<script type= "text/javascript"> 

function myFunction() 
{ 

if (pokeid != document.getElementById("goid").value;) { 

window.location = '("http://www.myurl.net/index.php")'; 

}else{ 
document.getElementById("myForm").submit(); 

} 

} 

</script> 
+0

你有沒有注意到報道雜散分號的語法錯誤? – Pointy

+1

你意識到用戶可以在他們的控制檯中輕鬆設置'pokeid = 12345',對吧? –

回答

2

JavaScript沒有安全措施。所有驗證必須在服務器上完成。沒有其他東西可以信任。

除此之外,在if聲明中有一個雜散分號,並且您的window.location分配應該只是一個URL。不是那些奇怪的括號,引號......事情。

1

這是一篇非常相關的文章,您應該在繼續進行任何客戶端驗證之前閱讀這篇文章。

「不要依靠客戶端驗證來驗證用戶輸入,客戶端驗證非常適合幫助用戶輸入正確的數據,但惡意用戶不會使用它並可能繞過客戶端驗證。不應該被認爲是一個安全修補程序,不應該使用javascript來驗證輸入,正如你所看到的,javascript很容易在任何html頁面上修改和修改。

源:「http://www.testingsecurity.com/how-to-test/injection-vulnerabilities/Javascript-Injection