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);
?>
Keywords
Related Questions
- What are the potential issues with setting the background image of a webpage based on the current hour using PHP?
- What are some best practices for debugging PHP code that involves querying data from multiple tables and outputting the results?
- What are some potential pitfalls when converting strings to integers in PHP?