still need to work on the the section after the "exit;"
So that is why it is there.
function DB_ATTRIBUTES($input) {
$opt = array(
/* any occurring errors wil be thrown as PDOException */
//'PDO::ATTR_ERRMODE' => "PDO::ERRMODE_SILENT",
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
//PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
/* an SQL command to execute when connecting */
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
//PDO::ATTR_EMULATE_PREPARES => "false"
);
foreach ($opt as $key => $value) {
$input->setAttribute($key, $value);
}
}
function Get_DB_ATTRIBUTES($input) {
$attributes = array(
"AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS",
"ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION",
"TIMEOUT"
);
foreach ($attributes as $val) {
echo "PDO::ATTR_$val: <br>";
echo $input->getAttribute(constant("PDO::ATTR_$val")) . "<br>";
}
}
$PDO_setting = $config['db']['driver'].':host='.$config['db']['host'].';dbname='.$config['db']['dbname'].'; '.$config['db']['username'].', '.$config['db']['password'].', ';
try{
/* See if they are in the database */
$database_handler = new PDO($PDO_setting);
DB_ATTRIBUTES($database_handler);
//Get_DB_ATTRIBUTES($database_handler);
$stmt = $database_handler->prepare("
SELECT *
FROM ip_blocker
WHERE ip = :their_ip_address
AND user_agent NOT LIKE '%google%'
AND user_agent NOT LIKE '%yahoo%'
AND user_agent NOT LIKE '%bing%'
AND user_agent NOT LIKE '%msn%'
AND user_agent not like '%slurp%'"
);
//echo $their_ip_address.'<br>';
$stmt->bindValue(':their_ip_address', $their_ip_address, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchALL(PDO::FETCH_ASSOC);
echo '<pre>'.print_r($rows, true).'</pre>';
/* Check for any results. */
if (empty($rows)) {
/* They are not in the database, so add them. */
$stmt = $database_handler->prepare("
INSERT INTO ip_blocker
(
ip,
host,
count,
visits,
banned,
first,
user_agent,
referer
)
VALUES
(
:their_ip_address,
:their_host,
'1',
'1',
'0',
:time_now,
:their_user_agent,
:their_referer
)
");
$stmt->bindValue(":their_ip_address", $their_ip_address, PDO::PARAM_INT);
$stmt->bindValue(":their_host", $their_host, PDO::PARAM_STR);
$stmt->bindValue(":time_now", strtotime('now'), PDO::PARAM_INT);
$stmt->bindValue(":their_user_agent", $their_user_agent, PDO::PARAM_STR);
$stmt->bindValue(":their_referer", $their_referer, PDO::PARAM_STR);
$stmt->execute();
} else {
/* They are in the database, so start counting their page hits and add last time page was hit. */
echo '<pre>'.print_r($rows, true).'</pre>';
$last = strtotime('now');
echo $rows['0']['count'].'<br>';
$add_to_row_count = $rows['0']['count']+1;
echo $add_to_row_count.'<br>';
$stmt = $database_handler->prepare("
UPDATE ip_blocker
SET
count = :add_to_row_count,
last = :last
WHERE
id = ".$rows['0']['id'].""
);
//$stmt->bindValue(":add_to_row_count", $add_to_row_count, PDO::PARAM_INT);
$stmt->bindValue(":last", $last, PDO::PARAM_INT);
$stmt->execute();
echo '<pre>'.print_r($rows, true).'</pre>';
exit;
/* start check to see if they need to be banned */
$un_ban_in_seconds = 24; /* hours * min * secs * 60 * 60*/
$on_page_time_in_seconds = 60;
$max_page_requests = 20;
$bancount = 1;
$count = $rows['0']['count'];
$banned = $rows['0']['banned'];
$first = $rows['0']['first'];
$last_hit = $rows['0']['last'];
$host = $rows['0']['host'];
$current_time = strtotime('now');
$referer = trim($rows['0']['referer']);
$difference_from_first_hit = $last_hit - $first;
//echo $difference_from_first_hit;
/* check to see if they are banned */
if ($rows['0']['banned'] == 1) {
/* They are banned so check to see if time is up */
if ($un_ban_in_seconds < $difference_from_first_hit) {
$last = strtotime('now');
$stmt = $database_handler->prepare("
UPDATE ip_blocker
SET
ip = :their_ip_address,
host = :their_host,
count = 0,
visits = 0,
banned = 0,
first = :time_now,
user_agent = :their_user_agent,
referer = :their_referer,
diff = 0
WHERE id =".$rows['0']['id'].""
);
$stmt->bindValue(":their_ip_address", $their_ip_address, PDO::PARAM_INT);
$stmt->bindValue(":their_host", $their_host, PDO::PARAM_STR);
$stmt->bindValue(":time_now", strtotime('now'), PDO::PARAM_INT);
$stmt->bindValue(":their_user_agent", $their_user_agent, PDO::PARAM_STR);
$stmt->bindValue(":their_referer", $their_referer, PDO::PARAM_STR);
$stmt->execute();
}
} else {
/* They are not currently banned see if they need to be */
if ($count >= $max_page_requests) {
$last = strtotime('now');
$stmt = $database_handler->prepare("
UPDATE ip_blocker
SET banned = 1, diff = :difference_from_first_hit
WHERE id =".$rows['0']['id'].""
);
$stmt->bindValue(":difference_from_first_hit", $difference_from_first_hit, PDO::PARAM_INT);
$stmt->execute();
echo "you have been banned!";
}
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
/* Close the database connection */
$database_handler = null;