Page 1 of 1
Avoid Simultaneous access of a php page
Posted: Wed Jul 13, 2005 5:27 am
by dhanasekaran1980
Can anybody please help me how to avoid simultaneous access of a php page Just like synchronize in JAVA. Only one user should enter the page at a time.
Posted: Wed Jul 13, 2005 5:50 am
by onion2k
There isn't a function to do it for you. Write a locking system.
what is locking system
Posted: Wed Jul 13, 2005 5:54 am
by dhanasekaran1980
Thank you for your immediate reply. Can you please give an idea how to write a locking system.
Posted: Wed Jul 13, 2005 6:11 am
by shiznatix
flock(); //i think
Posted: Wed Jul 13, 2005 11:16 am
by onion2k
flock() locks files, that's not what you want.
A very basic locking mechanism would be simply to create a file on the server when your script runs, and to delete it again when the script finishes. When the script starts it checks for the file, and if it exists then you know that someone else is running it.
Code: Select all
if (file_exists("lock.txt") {
exit;
} else {
$lockfile = fopen("lock.txt","w");
fclose($lockfile);
}
// Do some stuff;
unlink("lock.txt");
(Untested code)
Posted: Wed Jul 13, 2005 2:43 pm
by hawleyjr
You could also create a flag in a table.
Code: Select all
//START OF FILE
$qry = "SELECT ID FROM TABLE WHERE ID = 1";
if(//RESULT WAS RETURNED){
$qry = "UPDATE TABLE set ID = 0";
}
//END OF FILE
$qry = "UPDATE TABLE set ID = 1";
Posted: Wed Jul 13, 2005 2:45 pm
by timvw
A websearch on mutual exclusion is also very informative
fe:
http://en.wikipedia.org/wiki/Mutual_exclusion