Posted By

jretamal on 06/15/10


Tagged

mysql php ip Poll restriction vote


Versions (?)

IP Poll Restriction


 / Published in: PHP
 

URL: http://jretamal.cl

Restricts the vote to one vote per ip per day

  1. <?php
  2.  
  3. /**
  4.  * PHP+MySQL
  5.  * Restricts the vote to one vote per ip per day
  6.  * @name IP Poll Restriction
  7.  * @author Julio César Retamal Rojas - http://jretamal.cl
  8.  * @version 0.1
  9.  * @date June 15, 2010
  10.  * @copyright (c) 2010 Julio César Retamal (jretamal.cl)
  11.  * @usage
  12.  *
  13.  * //Your code
  14.  * if(checkVote())
  15.  * {
  16.  * //Vote successful
  17.  * }
  18.  * //Your code
  19.  *
  20.  * @installation
  21.  *
  22.  * CREATE TABLE IF NOT EXISTS `ip_poll` (
  23.  * `ip` varchar(15) NOT NULL,
  24.  * `date` datetime NOT NULL
  25.  * ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  26.  *
  27.  */
  28.  
  29. function getIP() {
  30. $ip="";
  31. if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
  32. else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
  33. else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
  34. else $ip = "";
  35. return $ip;
  36. }
  37.  
  38. function checkVote()
  39. {
  40. $ip= getIP();
  41. $sql= "select count(*) from ip_poll where ip = '".$ip."' and SUBSTR(date,1,10) = '".date("Y-m-d")."'";
  42. if($res= mysql_query($sql))
  43. {
  44. $row= mysql_num_rows($res);
  45. if($row == 0)
  46. {
  47. $sqlIns= "insert into ip_poll values ('".$ip."',now());";
  48. if(mysql_query($sqlIns))
  49. return true;
  50. else
  51. return false;
  52. }else{
  53. return false;
  54. }
  55. }else{
  56. return false;
  57. }
  58. }
  59.  
  60. ?>

Report this snippet  

You need to login to post a comment.