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?
-
if the users are saved in your db then save the page visit on a table for that user, if the users anonymous then use cookie. – Abhik Chakraborty Jan 10 '14 at 14:25
-
You can use sessions to store the relationships users/bannercolor – zeflex Jan 10 '14 at 14:27
-
3How do you define when a page "is already in use"? How do you define when "use is done"? Is there some sort of action being taken on the page? – Patrick Q Jan 10 '14 at 14:28
-
1@zeflex that would not work as sessions are per user and not global. She apparently wants to restrict access to all but one user – thpl Jan 10 '14 at 14:28
-
1Thanks @ThomasDavidPlat . In this case why not use a kind of lockfile ? – zeflex Jan 10 '14 at 14:31
-
@zeflex made a little example – thpl Jan 10 '14 at 15:18
2 Answers
<?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 ;)

- 5,810
- 3
- 29
- 43
-
-
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
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:
To have period requests via Ajax from the client currently on the page (initial trigger is page load, Ajax thereafter).
Make each request restart a short timer that gives that person additional time before someone else can jump in.
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.