livewirerules Posted July 21, 2008 Share Posted July 21, 2008 Im coding a small rpg game script and i need to limit the users chances to play the game per ip. Eg: certain IP can play only 5 times a day Which means that the user can click on a certain link only 5 times. Im bit confused how to do this. Can someone please help me Link to comment https://forums.phpfreaks.com/topic/115883-limit-per-ip/ Share on other sites More sharing options...
craygo Posted July 21, 2008 Share Posted July 21, 2008 You will have to keep track of the visits either in some kind of text file of in a mysql database. but this will give you the ip address of the computer $ip = $_SERVER['REMOTE_ADDR']; then you will have to write the address to the preferred file or database Ray Link to comment https://forums.phpfreaks.com/topic/115883-limit-per-ip/#findComment-595827 Share on other sites More sharing options...
marcus Posted July 21, 2008 Share Posted July 21, 2008 on the page with the clicking <?php $ip = $_SERVER['REMOTE_ADDR']; $sql = "SELECT * FROM `ip_log` WHERE `ip`='".$ip."'"; $res = mysql_query($sql) or die(mysql_error()); $err = false; if(mysql_num_rows($res) == 0){ $sql2 = "INSERT INTO `ip_log` (`ip`,`clicks`) VALUES('".$ip."','1')"; $res2 = mysql_query($sql2) or die(mysql_error()); $err = false; }else { $row = mysql_fetch_assoc($res); if($row['clicks'] >= 5){ $err = true; }else { $sql3 = "UPDATE `ip_log` SET `clicks`=`clicks`+1 WHERE `ip`='".$ip."'"; $res3 = mysql_query($sql3) or die(mysql_error()); $err = false; } } if($err){ echo "You have already clicked this page 5 times!"; }else { // whatever } ?> then i guess you can set a cron to run every 24 hours to reset em Link to comment https://forums.phpfreaks.com/topic/115883-limit-per-ip/#findComment-595836 Share on other sites More sharing options...
natbob Posted July 21, 2008 Share Posted July 21, 2008 The simplest way is to use cookies, but it is very insecure, however if this is a low security situation it would do the job. <?php if (!isset($_COOKIE['plays'])) setcookie('plays', 0, time()+86400); else if ($_COOKIE['plays'] <= 5) { The code of the restricted page... $_COOKIE['plays']++; } else { echo "You are not allowed to play any more today, come back tomorrow."; } ?> Link to comment https://forums.phpfreaks.com/topic/115883-limit-per-ip/#findComment-595838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.