Restrict Form Submissions Based on IP

Would you like to prevent certain IP addresses from submitting your Joomla forms? This can be useful in cases where you want to block spam, restrict access to specific users, or limit submissions from certain regions. The following guide will show you how to restrict form submissions based on IP with Convert Forms, ensuring only allowed users can complete your form.

Setup

To identify restricted IP addresses and notify the user with a relevant message, insert the following code into the PHP Scripts → Form Process section of your form.

// The list of not allowed IP addresses
$badIPs = [
	'1.2.3.4',
	'100.200.300',
	'70.50'
];

// The error message will appear if a bad IP is found
$error_message = 'Your submission has been blocked due to restricted content.';

// Detect the user's IP address from multiple sources
$ip = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '';

// Ensure a valid IP is detected
if (!filter_var($ip, FILTER_VALIDATE_IP))
{
	return;
}

foreach ($badIPs as $badIP)
{
    if ($ip === $badIP || strpos($ip, $badIP . '.') === 0)
	{
        throw new Exception($error_message);
    }
}
Last updated on Feb 7th 2025 09:02