Jump to content

Counter LOCK~~~


jimmyyami

Recommended Posts

 

 

Application.lock in ASP used widely in website's visitor Counter to prevent two people enter the site at the same time. 

 

MY question is ...  how to prevent two person operate the MYSQL database exactly at the same time when the counter is based on PHP????~~

Link to comment
Share on other sites

flock()  might is what i need.    I hope...

 

first create a lock file as the guard dog for MySQL database.  use flock() made it able to open once simultaneously. and use it as the switch of this lock file.  the code is something like that listed as below...   

 

// ----------------------------------------------------------------------------

$lockfile  = "vclock.lock";

 

.....

 

$vclock = fopen($lockfile, "w");

if (!$vclock) {

echo " lockfile $lockfile not able to open";

exit;

}

if (!flock($vclock, LOCK_EX)) {

echo "lock $lockfile has fail";

exit;

}

// ----------------------------------------------------------------------------

if (!($fp=fopen($logfile,"r"))) {

echo "file $logfile not able to open";

exit;

}

 

flock($fp, LOCK_SH);

flock($fp, LOCK_UN);

fclose($fp);

 

.......

 

if (!($vcct=fopen($logfile,"w"))) {

echo " lockfile $lockfile not able to open";

exit;

}

if (!flock($vcct, LOCK_EX)) {

echo "lock file $logfile failure";

exit;

}

 

.........

 

}

// ----------------------------------------------------------------------------

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.