Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AVOID CONCURRENT PROCESS ???

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
0
0
FR
Hello,

Does anyone know how I could possibly prevent a script from being run by more than one user at the same time on PHP4?

I have a script that has to do some maintenance on a web site.
It is called on every page because cronjobs aren't permitted on the server.

Of course, the script was designed in such a way that the maintenance is skipped when it's already done.

The problem is that when I refresh a page from two different computers at the same time, it disrupts the maintenance process. It seems that the script doesn't have enough time to find out whether or not the maintenance has been already completed.

I could call the script only from the home page but there is a possibility that two users hit that page at the same time.

Any solution?
 
you should create a text file when the script starts and delete it when it ends. this will avoid the situation:

e.g.:
User1 requests the link - Text file created
User2 requests the link - If text file created then script will not be executed...

Known is handfull, Unknown is worldfull
 
Thanks vbkris,

But do you think the creation of a file on the server is fast enough to prevent both sides from "thinking" that the file isn't created yet?

The security I am using is a check on the database but it doesn't seem to be fast enough. A file creation would be faster?
 
if it is really a concern with timing, maybe you should be doing it another way?

i guess you are setting value in a database, running maintenance script, and if value is already set, not running script. then when maintenance script is finished, unsetting value in the database.

dont see how it could be any faster than that...

only one suggestion - get a real host with crontab support! there are so many good ones out there... i would suggest dreamhost.com.
 

Thanks miahmiah900,

I have a great host but the one that causes the problem is the one a client is using. So, there is nothing I can do about it :(
 
then you should allow only one user to access such important functions on the server...

Known is handfull, Unknown is worldfull
 
yes, creating text file is faster than connecting a database (should)



Regards,

Namida
 

Thanks for that piece of information, Namida :)
 
Just a little update.

Vbkris and Namida gave me the idea of using file creations to emulate cronjobs on a server.

This simple little script can run some code once a day :
Code:
$temp_var = $dir_root . "dir_conf/dir_chrono/" . date("dmY");

    if(file_exists($temp_var)) {
    
    } else {

    // - ! - do some crazy stuff here
    do_crazy_stuff();
    
    // - ! - delete dir
    cleardir(str_replace(date("dmY"), "", $temp_var));
    
    // - ! - recreate file with proper date
    $temp_var = fopen($temp_var, "w");
    fclose($temp_var);
    
    }

Thanks to you all :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top