开发者

Check bans function

I call this function on top of every page. How would I do so it removes expired bans also? If current time() is more than expire field. Would really appricate help.

bans

  • id
  • ip
  • expire

(if ex开发者_StackOverflow社区pire is NULL its permanent)

function

function check_bans()
{

$user_ip = $_SERVER['REMOTE_ADDR'];

// Se if users ip is banned
$query = mysql_query("SELECT ip FROM bans WHERE ip = '$user_ip'");

// If they are, kill script and show message
if (mysql_num_rows($query))
    die('You have been IP banished. Expires: <b>Never</b>');

}


Assuming that expire is a DATETIME data type, use:

SELECT ip, expire
  FROM bans 
 WHERE ip = '$user_ip'
   AND expire >= CURRENT_TIMESTAMP

NOW() works just as well - they're synonyms for the current date and time.


function check_bans()
{
  $user_ip = $_SERVER['REMOTE_ADDR'];
  $query = mysql_query("SELECT expire FROM bans WHERE ip='$user_ip' AND expire >= NOW()");
  if (mysql_num_rows($query) && $row = mysql_fetch_array($query)) {
    die("You have been IP banished. Expires: <b>".$row['expire']?$row['expire']:'Never'."</b>");
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜