Home  >  Article  >  php教程  >  php实现每个ip每天投票一次的方法

php实现每个ip每天投票一次的方法

WBOY
WBOYOriginal
2016-06-13 11:36:161575browse

 下面是实现的代码:

  1.  
  2. /**
  3.  * PHP+MySQL
  4.  * CREATE TABLE IF NOT EXISTS `ip_poll` (
  5.  * `ip` varchar(15) NOT NULL,
  6.  * `date` datetime NOT NULL
  7.  * ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  8.  *
  9.  */
  10.  
  11. function getIP() {
  12. $ip=”";
  13. if (getenv(“HTTP_CLIENT_IP”)) $ip = getenv(“HTTP_CLIENT_IP”);
  14. else if(getenv(“HTTP_X_FORWARDED_FOR”)) $ip = getenv(“HTTP_X_FORWARDED_FOR”);
  15. else if(getenv(“REMOTE_ADDR”)) $ip = getenv(“REMOTE_ADDR”);
  16. else $ip = “”;
  17. return $ip;
  18. }
  19.  
  20. function checkVote()
  21. {
  22. $ip= getIP();
  23. $sql= “select count(*) from ip_poll where ip = ‘”.$ip.”‘ and SUBSTR(date,1,10) = ‘”.date(“Y-m-d”).”‘”;
  24. if($res= mysql_query($sql))
  25. {
  26. $row= mysql_num_rows($res);
  27. if($row == 0)
  28. {
  29. $sqlIns= “insert into ip_poll values (‘”.$ip.”‘,now());”;
  30. if(mysql_query($sqlIns))
  31. return true;
  32. else
  33. return false;
  34. }else{
  35. return false;
  36. }
  37. }else{
  38. return false;
  39. }
  40. }
  41.  
  42. ?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn