What are some best practices for handling and storing IP address information in PHP applications?

When handling and storing IP address information in PHP applications, it is important to validate and sanitize the input to prevent injection attacks and ensure data integrity. One best practice is to store IP addresses in a standardized format, such as using the INET_ATON and INET_NTOA functions to convert IP addresses to and from integers for efficient storage and retrieval.

// Validate and sanitize IP address input
$ip_address = filter_var($_POST['ip_address'], FILTER_VALIDATE_IP);

// Convert IP address to integer for storage
$ip_int = ip2long($ip_address);

// Store IP address in database
$query = "INSERT INTO ip_addresses (ip_address) VALUES ('$ip_int')";
mysqli_query($connection, $query);