tags:

views:

31

answers:

2

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 expire 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>');

}
+2  A: 

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.

OMG Ponies
You should query for the ban expiry date rather than the IP. That way it can be used in the output to the user...
Brendan Bullen
+1  A: 
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>");
  }
}
jigfox