开发者

comparing random number?

开发者 https://www.devze.com 2023-02-20 02:13 出处:网络
how do I compare a random number or is it possible? the scenario is I have a function using $_GET method and it is visible on URL. so any user can store this link to use it later. this is a PHP game

how do I compare a random number or is it possible?

the scenario is I have a function using $_GET method and it is visible on URL. so any user can store this link to use it later. this is a PHP game so any one can cheat the game very easily. so what will be the best w开发者_开发百科ork around to prevent the same? I was thinking about a random number to use each time when user calls the link and compare it so user cant use the same link to access the game later.


Do you have access to a database for the site? If so, you can log the user's IP address and the random number and perform a check each time the page is accessed.

If not, you can store the numbers in the $_SESSION variable and check to see if it was already used:

session_start();
if(!isset($_SESSION['codes'])) $_SESSION['codes'] = array();

if(in_array($_GET['code'], $_SESSION['codes']) {
   // code to execute if this code was already used by the user
} else {
  $_SESSION['codes'][] = $_GET['code'];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消