简体   繁体   中英

Can local javascript variables be read by user console?

I am trying to create an online lottery system in which user has to guess a number between 1 to 10. If the number matches with the javascript generate random number he will win the lottery. The method I used is:

 !function(){ var x = Math.floor((Math.random() * 10)); var btn = document.getElementById("btn"); btn.addEventListener("click", function(){ var user_input = document.getElementById("text").value; if( user_input == x) { alert("Won Lottery", user_input, x); } else { alert(user_input + x + "sorry"); } }); }(); 
 <h2>Guess a number between 1 and 10 </h2> <input id="text" type="text"> <button id="btn" type="submit">Submit</button> 

My question is, can a hacker open the js console and somehow find the value of local x variable?

在此处输入图片说明

At this point in the code execution, x is within scope. You can see in the right column that x = 0 so it's just a case of changing user_input to 0 and then finishing the execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM