How can I store UNIX time in a text file when a user closes a window in PHP?

To store UNIX time in a text file when a user closes a window in PHP, you can use JavaScript to send an AJAX request to a PHP script that writes the UNIX time to a text file. You can trigger this AJAX request when the window is closed by using the window.onbeforeunload event.

// JavaScript code to send AJAX request when window is closed
<script>
window.onbeforeunload = function() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "store_unix_time.php", true);
    xhr.send();
};
</script>

// PHP code in store_unix_time.php to write UNIX time to a text file
<?php
$unix_time = time();
$file = fopen("unix_time.txt", "w");
fwrite($file, $unix_time);
fclose($file);
?>