What are some best practices for securely handling user data like IP addresses in PHP applications?

When handling user data like IP addresses in PHP applications, it is important to follow best practices to ensure the security and privacy of this sensitive information. One way to securely handle IP addresses is by sanitizing and validating user input to prevent SQL injection attacks and other vulnerabilities. Additionally, consider storing IP addresses in a hashed or encrypted format to protect them from unauthorized access.

// Sanitize and validate user input for IP address
$user_ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);

// Hash the IP address before storing it
$hashed_ip = password_hash($user_ip, PASSWORD_DEFAULT);

// Store the hashed IP address in the database
// Example query: $sql = "INSERT INTO user_ips (hashed_ip) VALUES ('$hashed_ip')";