I am making a simple PHP and MySQL IP banning script and I am at the stage of checking to see if the IP address is already banned. I have created a test file called test.php
and this is its contents:
<?php
require_once('../includes/db_connect.php');
session_start();
$ip = '1.1.1';
$stmt = $db->prepare("SELECT `ip`, `reason` FROM `banned_ips` WHERE `ip` = ?");
$stmt->bind_param('s', $ip);
$stmt->execute();
if ($stmt->num_rows > 0) {
header('Location: banned.php');
}
$stmt->close();
?>
<html>
<h1>This is a test page</h1>
</html>
As you can see I have set the $ip
variable equal to 1.1.1
which is a test value that does exist in the database, therefore should be seen as banned, however it doesn't see it as being banned and therefore doesn't redirect to the banned page.
Am I doing something wrong when checking to see if the IP address already exists in the database?
My database structure is as follows:
Table: banned_ips
column 1: id
Column 2: ip
column 3: reason
EDIT: Sorry for the typo, I do have an IP column and it is a varchar.