View Full Version : Ban\Unban and show remaining time on ban

I Juztyn I
08-06-2011, 03:06 PM

I am trying to create a web based control panel for a game, staff have the option to ban\unban a player, or set a set timed ban (1 day, 2 days ect)

Here is the code for banning a player

if($action == "changeBan"){
$query = mysql_query("SELECT * FROM user WHERE userid = '".$_GET['dbid']."'") or die(mysql_error());
$player = mysql_fetch_assoc($query);
if($player['banned'] == 0){
$banning = 1;
} else {
$banning = 0;
$durations = array("1", "2", "3", "4", "5", "7", "10", "Forever");
echo "<center><b>". $player['name'] ."</b><br/>";
if($banning == 1){
<form method="get" action="index.php"><input type="hidden" name="page" value="modcp">
<input type="hidden" name="action" value="banUser">
<input type="hidden" name="dbid" value="<?php echo $_GET['dbid']; ?>">
<textarea name="reason" COLS=40 ROWS=6>Ban reason</textarea><br />
<select name="duration">
foreach($durations as $duration){
?><option value="<?php echo $duration;?>"><? echo $duration; ?> days</option><?
<input type="submit" value="Ban user"></center></form>
} else {
if($level < $player['ban_level']){
echo "<center><b>You cannot unban this player</b></center>";
} else {
mysql_query("UPDATE user SET ban_level = 0, banned = 0, ban_reason='', ban_expire = '', ban_by = '' WHERE userid='".$_GET['dbid']."'") or die(mysql_error());
<center><b>User unbanned!</b> (<a href="index.php?page=modcp">Back to modcp</a>)</center>

Here is where the banning is processed

if($action =="banUser"){
$expire = time() + (3600000 * 24 * $_GET['duration']);
$duration = $_GET['duration'] ." days";
$reason1 = mysql_real_escape_string(trim($_GET['reason']));
mysql_query("UPDATE user SET ban_level = $level, banned = 1, ban_expire='$expire', ban_reason='". $reason1."', ban_by='". $name."' WHERE userid='".$_GET['dbid']."'") or die(mysql_error());
<center><b>User banned!</b> (<a href="index.php?page=modcp">Back to modcp</a>)</center>


When the player is searched, it checks to see if the player is banned, if the player is banned it will then display how many hours is left on the ban.

Here is the code for that

if($player['banned'] == 1){
$banStatus = "Banned";
$banStatus .="<br/>Banned by: ". $player['ban_by'] . "<br />";
$banStatus .="Ban reason: ". $player['ban_reason'] . "<br />";
$now = time();
$diff = $player['ban_expire'] - $now;
$hours = round($diff / 3600000, 2);
$banStatus .="Ban Expires: ". $hours . " hours";
} else {
$banStatus = "Not banned";

The problem here is that it will always show the initial ban time, (24 hours, 72 hours) and it will not count down.

Please let me know if this should be posted in the PHP section, I just thought because it has mysql for saving data it would go here.

If anyone needs any more information please feel free to ask.


Old Pedant
08-07-2011, 06:03 AM
I don't know why the PHP code doesn't work, but you could certainly move the calculation into MySQL instead.

SELECT TIMESTAMPDIFF( MINUTE, ban_expire, NOW() ) AS minutesToWait,
other, fields
FROM user WHERE userid = XXX

You could even make sure that you never return a negative value (that is, the ban is now lifted) quite easily:

SELECT IF( ban_expire > NOW(), 0, TIMESTAMPDIFF( MINUTE, ban_expire, NOW() ) ) AS minutesToWait,
other, fields
FROM user WHERE userid = XXX

Old Pedant
08-07-2011, 06:09 AM
I'd also suggest doing the add in MySQL, instead of PHP:

$sql = "UPDATE user SET ban_expire = DATE_ADD( NOW(), INTERVAL $daysToBan DAY ) ... ";

Where you have presumably done

$daysToBan = $_GET["duration"];

Forgot to say that you probably noticed that my code will return number of minutes before the ban expires. You can easily convert that to hours and minutes in the PHP code, of course.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum