What is the best practice for storing a user's IP address during login in PHP?

Storing a user's IP address during login can be useful for security and tracking purposes. The best practice is to sanitize and validate the user's IP address before storing it in the database to prevent SQL injection attacks. Additionally, it's recommended to store the IP address in a secure way, such as hashing it before saving it to the database.

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

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

// Store the hashed IP address in the database
// Example query: INSERT INTO users (ip_address) VALUES ('$hashed_ip');