MyBB Community Forums

Full Version: Ban User List Page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello MyBB!

Today I will showing you how to create a ban user list page the page will display the ban users username, user who banned them, the reason, ban date, unban date, the ban user’s IP address they registered with, the last IP they login from, and their email address.

Step 1) Create a new file in your forum root called “bans.php”
Step 2) Paste the code below in it and save.

<?php

// Author: Paul H. ( https://community.mybb.com/user-37431.html )
// Code Changes By: Brian. ( https://community.mybb.com/user-115119.html )
// bans.php created on 10-15-17
// Support: https://community.mybb.com/thread-213593.html

define('IN_MYBB',1);
require_once('global.php');
define("PAGINATION",20);
$lang->load("modcp");
add_breadcrumb("Banned Users");
if(isset($mybb->input['asc'])) {
	$options['order_dir'] = 'asc';
	$ascdesc = '&asc';
	$ascdesci = '&desc'; } else {
	$options['order_dir'] = 'desc';
	$ascdesc = '&desc';
	$ascdesci = '&asc'; }
switch($mybb->input['sortby']) {
	case 'issued':
	$options['order_by'] = 'dateline';
	break;
	case 'lifted':
	$options['order_by'] = 'lifted';
	break;
	default: 
	$options['order_by'] = 'dateline'; }
if(isset($mybb->input['page'])) {
$page = (int)$mybb->input['page']; } else {
$page = 1; }
$extra = "&orderby={$options['order_by']}{$ascdesc}";
$query = $db->simple_select("banned", "COUNT(uid) AS count");
$bannum = $db->fetch_field($query, "count");
$multipage = multipage($bannum,PAGINATION,$page,'bans.php?page={page}'.$extra);
$options['limit'] = PAGINATION;
$options['limit_start'] = ($page - 1) * PAGINATION;
$query = $db->simple_select('banned','*',null,$options);
$bans = '<tr>
<td class="tcat" align="center"><span class="smalltext"><strong>'.$lang->username.'</strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong>'.$lang->reason.'</strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong>'.$lang->ban_bannedby.'</strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong><a href="bans.php?sortby=issued'.$ascdesci.'">Ban Date</a></strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong><a href="bans.php?sortby=lifted'.$ascdesci.'">Unban Date</a></strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong>User Email</strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong>Registration IP</strong></span></td>
<td class="tcat" align="center"><span class="smalltext"><strong>Latest IP</strong></span></td>
</tr>';
$banlist = '';
$bantimes = fetch_ban_times();
while($banned = $db->fetch_array($query)) {
	$user = get_user($banned['uid']);
	$bannedby = get_user($banned['admin']);
	if($banned['lifted'] == 'perm' || $banned['lifted'] == '' || $banned['bantime'] == 'perm' || $banned['bantime'] == '---') {
	$banlength = $lang->permanent;
	$timeremaining = $lang->na; } else {
	$banlength = $bantimes[$banned['bantime']];
	$remaining = $banned['lifted']-TIME_NOW;
	$timeremaining = nice_time($remaining, array('short' => 1, 'seconds' => false))."";
	if($remaining < 3600) {
	$timeremaining = "<span style=\"color: red;\">({$timeremaining} {$lang->ban_remaining})</span>"; }
	else if($remaining < 86400) {
	$timeremaining = "<span style=\"color: maroon;\">({$timeremaining} {$lang->ban_remaining})</span>"; }
	else if($remaining < 604800) {
	$timeremaining = "<span style=\"color: green;\">({$timeremaining} {$lang->ban_remaining})</span>"; } else {
	$timeremaining = "({$timeremaining} {$lang->ban_remaining})"; }
	$timeremaining = my_date($mybb->settings['dateformat'],$banned['lifted'])." <br/ ><span class=\"smalltext\">{$timeremaining}</span>"; }
$banlist .= "<tr>
<td class=\"trow1\" align=\"center\">".build_profile_link($user['username'], $user['uid'])."</td>
<td class=\"trow1\" align=\"center\">{$banned['reason']}</td>
<td class=\"trow1\" align=\"center\">".build_profile_link($bannedby['username'], $bannedby['uid'])."</td>
<td class=\"trow1\" align=\"center\">".my_date($mybb->settings['dateformat'],$banned['dateline'])."</td>
<td class=\"trow1\" align=\"center\">{$timeremaining}</td>
<td class=\"trow1\" align=\"center\">".build_profile_link($user['email'], $user['uid'])."</td>
<td class=\"trow1\" align=\"center\">".my_inet_ntop($db->unescape_binary($user['regip']))."</td>
<td class=\"trow1\" align=\"center\">".build_profile_link($user['lastip'], $user['uid'])."</td>
</tr>";}
if($banlist == '') {
$banlist = '<tr><td colspan="8">'.$lang->no_banned.'</td></tr>';}
$bans .= $banlist;
$page = "<html>
<head>
<title>Banned Users</title>
{$headerinclude}
</head>
<body>
{$header}
<br />

<table border=\"0\" cellspacing=\"{$theme['borderwidth']}\" cellpadding=\"{$theme['tablespace']}\" class=\"tborder\">
<tr>
<td class=\"thead\" colspan=\"8\"><span class=\"smalltext\"><strong>Banned Users List</strong></span></td>
</tr>
{$bans}
</table>

{$multipage}
{$footer}
</body>
</html>";

output_page($page);
?>


This has been tested on MyBB 1.6 and MyBB 1.8 and works just fine so enjoy!
Screenshot:
[Image: 07_A95_E08_EA3_F_4205_AD81_AF99_EED34_ACF.jpg]

I hope you guys enjoy this!
Works but:

1. Try not to hard-code the HTML, use the template system.
2. get_user() function is not something I'd use as it generates lots of queries (1 query per user). Check below, that's a query I use for my ban list.

$query = $db->write_query(sprintf("
	SELECT b.*, u2.*, u.uid AS id, u.username AS user, u.displaygroup AS dispgroup, u.usergroup AS ugroup FROM %sbanned b
	LEFT JOIN %susers u ON(b.admin = u.uid)
	LEFT JOIN %susers u2 ON(b.uid = u2.uid)
	ORDER BY b.dateline DESC
	LIMIT {$start}, {$perpage}", TABLE_PREFIX, TABLE_PREFIX, TABLE_PREFIX
));
IP's are not showing Sad can you verify if it is me or the script.



[Image: 82bubLA.png]
i tried to figure it out but no idea.
Its because he is formatting it as a username. Change the following:
<td class=\"trow1\" align=\"center\">".build_profile_link($user['regip'], $user['uid'])."</td>

To:
<td class=\"trow1\" align=\"center\">".my_inet_ntop($db->unescape_binary($user['regip']))."</td>

You can use $post['lastip'] to display the last know IP as well.
<td class=\"trow1\" align=\"center\"><b>Registration IP:</b> ".my_inet_ntop($db->unescape_binary($user['regip']))."<br /><b>Last Known IP:</b> ".my_inet_ntop($db->unescape_binary($user['lastip']))."</td>

You should ideally use the template system instead as already mentioned.
@Omar G. - you proposed fixes are not working, IP reg and last used is not displayed at all.
If you did some modifications like the one Sazze suggested, you may need to do other changes as well for it to work.

Nevermind, I copy/pasted and didn't replace $post with $user in the code.
Thank you, seems to be fine, anyway I will test later this weekend.
how only the authorities can look at this file