0

I have a web application running on server that allows one user per page only. I want to check if the page is already in use. If ts is, I will change the collor of the banner to red and not allow acess until the use is done. But I don`t know how can I do this. Is a PHP application and I am usin some javascript too. The application is already done but I have to implement it now. How can check if page is being already accessed?

Jaqueline Passos
  • 1,301
  • 2
  • 24
  • 37

2 Answers2

2
<?php

session_start();

$lockfile = 'lock.txt';
$idleSeconds = 300;



if(file_exists($lockfile))
{
    // read the file. 
    // $file[0] = timestamp of last page visit
    // $file[1] = session id
    $file = file_get_contents($lockfile);
    $file = explode('|', $file);

    // If the same user is visiting the site he shall pass and refresh the timestamp
    // Do the same for a new user when the idletime ran out
    if($file[1] == session_id() OR $file[0] + $idleSeconds <= time())
        file_put_contents($lockfile, time() .'|'. session_id());
    else
        die("You are not allowed to view the page. Wait until some other motherfucker is done here.");      
}
else
{
    // When there's no lockfile create one for the current user
    file_put_contents($lockfile, time() .'|'. session_id());
}

?>

<html>
<head>
    <title>My locked Page</title>
</head>
<body>
    Yeah some cool shit man
</body>
</html>

I quickly pieced together something for you, in case you want a only-php approach. We are working with a lockfile here. The first user that is visiting the page will generate a lockfile with his session id and the current timestamp.

Every request we check if the lockfile exists and if yes we check if the user is permitted to view the page by checking his session id. We then update the timestamp.

If he's not we check if the idle time ran out. If that's the case we can overwrite the logfile since the other user has not acted for $idleSeconds

That's a quick and dirty solution but shows the basic idea.

Happy coding ;)

thpl
  • 5,810
  • 3
  • 29
  • 43
  • I was thinking about something like that yes. Good job ! – zeflex Jan 10 '14 at 15:27
  • That cannot be avoided a little delay is always present. You can reduce the $idleSeconds to like 20 or 10 and see if that feels good to you – thpl Jan 10 '14 at 16:35
1

Regardless of what the context of your problem is, I personally would never create a system that lead to this kind of requirement. However, I will assume you have no choice.

One approach is:

  1. To have period requests via Ajax from the client currently on the page (initial trigger is page load, Ajax thereafter).

  2. Make each request restart a short timer that gives that person additional time before someone else can jump in.

  3. When the timer expires, assume they have left the page, because the Ajax requests have stopped.

There are tidier solutions: Best way to detect when a user leaves a web page?

However they take the responsibility of deciding when a user is finished away from the server, which ideally where you want to keep it.

Community
  • 1
  • 1
Flosculus
  • 6,880
  • 3
  • 18
  • 42