Avoid Simultaneous access of a php page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dhanasekaran1980
Forum Newbie
Posts: 3
Joined: Wed Jul 13, 2005 5:10 am

Avoid Simultaneous access of a php page

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

There isn't a function to do it for you. Write a locking system.
dhanasekaran1980
Forum Newbie
Posts: 3
Joined: Wed Jul 13, 2005 5:10 am

what is locking system

Post by dhanasekaran1980 »

Thank you for your immediate reply. Can you please give an idea how to write a locking system.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

flock(); //i think
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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";
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

A websearch on mutual exclusion is also very informative ;)

fe: http://en.wikipedia.org/wiki/Mutual_exclusion
Post Reply