Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ban\Unban and show remaining time on ban

    Hello.

    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

    Code:
    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">
    		<?php
    			foreach($durations as $duration){
    				?><option value="<?php echo $duration;?>"><? echo $duration; ?> days</option><?
    			}
    		?>
    		</select>
    		<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>
    			<?php
    		}
    	}
    }
    Here is where the banning is processed

    Code:
    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>
    	<?php
    
    }
    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

    Code:
    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.

    Justin

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    I don't know why the PHP code doesn't work, but you could certainly move the calculation into MySQL instead.

    Code:
    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:
    Code:
    SELECT IF( ban_expire > NOW(), 0, TIMESTAMPDIFF( MINUTE, ban_expire, NOW() ) ) AS minutesToWait, 
           other, fields
    FROM user WHERE userid = XXX

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    I'd also suggest doing the add in MySQL, instead of PHP:
    Code:
    $sql = "UPDATE user SET ban_expire = DATE_ADD( NOW(), INTERVAL $daysToBan DAY ) ... ";
    Where you have presumably done
    Code:
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •