What is the purpose of writing the user's IP address to a file in PHP?

Writing the user's IP address to a file in PHP can be useful for tracking user activity or for security purposes. This can help in identifying potential threats or monitoring user behavior on a website. To implement this, you can use the $_SERVER['REMOTE_ADDR'] variable to get the user's IP address and then write it to a file using file handling functions in PHP.

// Get the user's IP address
$user_ip = $_SERVER['REMOTE_ADDR'];

// Open a file in append mode and write the IP address
$ip_file = fopen("user_ips.txt", "a");
fwrite($ip_file, $user_ip . "\n");
fclose($ip_file);