这个脚本很好用。我在我的脚本顶部调用它。但是,如果未被禁止的用户进入该网站,他们将获得:
Notice: Trying to get property of non-object in index.php on line 20
Run Code Online (Sandbox Code Playgroud)
功能:
// Check if conecting user is banned. If ban has expired delete row.
function check_bans()
{
// IP address
$user_ip = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("SELECT ip, expire FROM bans WHERE ip = '$user_ip'");
$row = mysql_fetch_object($query);
$expire = $row->expire ? date('M d Y H:i', $row->expire) : 'Never';
// Did we find a match?
if (mysql_num_rows($query)) {
// Has this ban expired? Then delete and let user inside.
if ($row->expire != '' && $row->expire <= time())
mysql_query("DELETE FROM bans WHERE ip = '$user_ip'") or die (mysql_error());
else
die("<h1 align=\"center\" style=\"color:maroon\">You have been IP banished. Ban will be lifted: $expire</h1>");
}
}
Run Code Online (Sandbox Code Playgroud)
因为如果用户未被禁止,则查询产生零结果并且$row分配被分配false(而不是对象)。因此,发生该错误是因为您试图在布尔值上调用方法。尝试:
function check_bans() {
// IP address
$user_ip = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("SELECT ip, expire FROM bans WHERE ip = '$user_ip'");
$row = mysql_fetch_object($query);
if($row) {
$expire = $row->expire ? date('M d Y H:i', $row->expire) : 'Never';
// Has this ban expired? Then delete and let user inside.
if ($row->expire != '' && $row->expire <= time())
mysql_query("DELETE FROM bans WHERE ip = '$user_ip'") or die (mysql_error());
else
die("<h1 align=\"center\" style=\"color:maroon\">You have been IP banished. Ban will be lifted: $expire</h1>");
}
}
Run Code Online (Sandbox Code Playgroud)