Fighting spam in a website

Wire Article Published by thuvalpakshi on 04, Wed Sep 2024
Stars Rating : no stars yet..!
Comments Total : no comments yet..!
Created on : 04, Wed Sep 2024

Hi frineds, after a long time I'm posting a blog again to explain how i am tackling the spam registrations in ANN website. Every 10-15 min a spam registration happens in ann website. I had taken these meassures to ensure the spam registration is minimum in ANN website

1. white list few email domains so that only those emails ids of whitelisted domains can be registred.

2. block the webpage in various other countries using the stopforumspam api keys.

3. Deny the ip address in htaccess

I just wrote a simple code to log the IP address of all visitors who are visiting the ANN (https://anushaktinagar.net) website and log all unique ips to seperate file. which I can have a look with IP2Geolocation services and then again block in .htaccess. Here is the simple code

<?php

/**
*******************************
* @author Satheesh PM
* @copyright 2024
* @Email admin@annlabs.in
* @Website www.annlabs.in
*******************************
*/

date_default_timezone_set('Asia/Kolkata');

$unique_ip_log_file = 'file name changed for safty';
$visitor_ip_log_file = 'file name changed for safty';
$server_ip_log_allow = 'file name changed for safty';

$unique_ips = [];

// Function to check for unique IPs
function isUniqueIP($ip_address, $unique_ip_log_file){
global $unique_ips;

// Check if the IP is already in the cache
if (isset($unique_ips[$ip_address])){
return true;
}

// Check if the unique IP file exists
if (file_exists($unique_ip_log_file)){
$handle = fopen($unique_ip_log_file, 'r');
while(($line = fgets($handle)) !== false){
if (trim($line) === $ip_address){
// IP already exists, skip adding it
fclose($handle);
return true;
}
}
fclose($handle);
}
$unique_ips[$ip_address] = true;
// Add the unique IP to the file
file_put_contents($unique_ip_log_file, $ip_address . PHP_EOL, FILE_APPEND);
return false; // IP is unique
}

// Function for asynchronous logging
function asyncLog($log_data, $visitor_ip_log_file){
// Use a background process or a message queue for asynchronous logging
// This example simulates asynchronous behavior using a sleep function
sleep(1); // Simulate asynchronous processing time
file_put_contents($visitor_ip_log_file, $log_data, FILE_APPEND);
}

// Get the visitor's IP address
$date = new DateTime();
$ip_address = $_SERVER['REMOTE_ADDR'];
$timestamp = $date->format('j F Y @ h:i:s A');

// Check if the IP is unique
if (!isUniqueIP($ip_address, $unique_ip_log_file)){
// Format the log entry
$log_data = "Date/Time: $timestamp : IP : $ip_address" . PHP_EOL;
// Start asynchronous logging
asyncLog($log_data, $visitor_ip_log_file);
}

// Optional: Rotate the visitor log file if it exceeds a certain size
$max_log_size = 2 * 1024 * 1024; // 2 MB
if (filesize($visitor_ip_log_file) > $max_log_size) {
// Create a new log file with a timestamp
$new_log_file = 'file name changed for safty' . date('YmdHis') . '.txt';
rename($visitor_ip_log_file, $new_log_file);
file_put_contents($visitor_ip_log_file, ''); // Create a new empty log file
}

?>


Wire Gallery

Comments on this Post

No comments posted yet..! Be the first one to post comment.


Your Comments on this post