Avoid Simultaneous access of a php page
Moderator: General Moderators
-
dhanasekaran1980
- Forum Newbie
- Posts: 3
- Joined: Wed Jul 13, 2005 5:10 am
Avoid Simultaneous access of a php page
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.
-
dhanasekaran1980
- Forum Newbie
- Posts: 3
- Joined: Wed Jul 13, 2005 5:10 am
what is locking system
Thank you for your immediate reply. Can you please give an idea how to write a locking system.
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.
(Untested code)
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");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";A websearch on mutual exclusion is also very informative 
fe: http://en.wikipedia.org/wiki/Mutual_exclusion
fe: http://en.wikipedia.org/wiki/Mutual_exclusion