mail@pastecode.io avatar
2 years ago
1.7 kB
So, here we need to find most accesses in the time range.
Explanationfor step 1
With this, we can give the giveaway for the persons who accessed the time more.
Step 2/3
Let us find the most accesses and the time using PHP:

How we should access user access time?

When a user visits your site, save the current time in the cookie as "visited," and if it was set, you can retrieve it on the next visit.

And a more expensive method: when the website loads, start a JavaScript timer, and when the page unloads, transmit the time that the user supplied to the server and save it to the database.

If window.unload does not function in Opera, you can transmit time to server every 5 seconds and save it in the database.
Step 3/3
Script for the above steps is as follows:

<!DOCTYPE html>
        <title>Collect time</title>
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
            var start = null;
            $(window).load(function(event) {
                start = event.timeStamp;
            $(window).unload(function(event) {
                var time = event.timeStamp - start;
                $.post('/collect-user-time/ajax-backend.php', {time: time});


Final answer
And the backend script is as follows:

$time = intval($_POST['time']);
if (!file_exists('data.txt')) {
    file_put_contents('data.txt', $time . "\n");
} else {
    file_put_contents('data.txt', $time . "\n", FILE_APPEND);
This gives you the time output in the given manner.