1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Running a php script in the background...

Discussion in 'PHP' started by novasoft, Apr 23, 2004.

  1. #1
    Hi,

    I have quite a bit of experience in ASP and have just started on PHP.

    I have a script that will take a long time to process, let me call it form2.php

    Now i post from form1.php to form2.php.

    On form1.php I ask the user to enter his email address and tell him that once the process is complete he will be mailed

    Now what i need is that when i post from form1.php to form2.php, the browser should show a message that says processing.. mail will be sent to your address.

    In the background the script must do what its supposed to do

    In form2.php,I have put the message as plain html on top of the page.Below that i have put the php code. I have used flush(), but it doesnt show the html part till the whole script is finished.

    Any idea how to accomplish this??

    The keyword tool at digitalpoint runs as a background process, perhaps i can do the same??

    Thanks
    SEMrush
     
    novasoft, Apr 23, 2004 IP
    SEMrush
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,283
    Likes Received:
    2,599
    Best Answers:
    460
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    There is better threading support in PHP 5. For the keyword tracker, I'm making calls to the shell and letting it run in it's own thread for background processing.

    - Shawn
     
    digitalpoint, Apr 23, 2004 IP
  3. er2er

    er2er Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In some of my tools I use the flush() function - it works fine with: server - Apache, PHP as module, clients - Mozilla, NS, Opera, MSIE.

    I can think also about few things to consider:
    - is this text contained within a table or near the very beginning of the document? some browsers cache the text sent from the server. (then you might pad it with some blanks or comments)
    - do you use output buffering or server-side modules like mod_gzip (it will cache te data untill php finishes)
     
    er2er, Apr 23, 2004 IP
  4. novasoft

    novasoft Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Shawn,

    I dont need multithreading in the true sense.

    All i need is to be a able to make a asynchronous call to a say script2.php from script1.php.

    Script1.php posts to itself, calls script2.php asynchronously and shows the user a message that says "ill mail you when im done"

    Nothing fancy.

    Currently the user is left hanging till the process by script2.php is completed and only then the mail is sent which defeats the purpose.

    Would posting to script2.php using sockets be a solution?

    er2er: flush() doesnt seem to help, ive tried that out :(
     
    novasoft, Apr 26, 2004 IP
  5. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,283
    Likes Received:
    2,599
    Best Answers:
    460
    Trophy Points:
    710
    Digital Goods:
    29
    #5
    The easiest way I know of would be to call your scripts from a shell as a background process like I do in the KW tracker.

    - Shawn
     
    digitalpoint, Apr 26, 2004 IP
  6. novasoft

    novasoft Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Pardon my ignorance, but like is said im new to php. In asp all i would do was use the xmlhttp in async mode and that would solve my problem.

    So is e.g. exec("/usr/local/bin/php /path/to/script.php > /dev/null &"); what you mean by "call your scripts from a shell as a background process"

    Also since the script has to be multiplatform, in case the server is a windows server /dev/null wont work will it?

    Thanks
     
    novasoft, Apr 26, 2004 IP
  7. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,283
    Likes Received:
    2,599
    Best Answers:
    460
    Trophy Points:
    710
    Digital Goods:
    29
    #7
    No, I don't think it would work on Windows...

    - Shawn
     
    digitalpoint, Apr 26, 2004 IP
  8. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Can someone supply a bit of example code of how to 'call your scripts from a shell as a background process'. I'm looking to pretty much what Shawn does with the keyword tracker.

    PS. Shawn...great forum and thanks for the useful tools!
     
    sharpweb, Aug 17, 2005 IP
  9. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #9
    What you mean is essentially Ajax, right? There should be some Ajax libraries available for PHP: here's one, for instance. Another framework I've been hearing good things about is Cake (a Ruby on Rails clone for PHP) which might have Ajax bits you could use.
     
    Willy, Aug 17, 2005 IP
  10. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,283
    Likes Received:
    2,599
    Best Answers:
    460
    Trophy Points:
    710
    Digital Goods:
    29
    #10
    The line of code that does it for the keyword tracker looks like this:

    exec ("/usr/bin/php yourscript.php >/dev/null &");
    PHP:
    The part on the end is the important part... >/dev/null sends the output if your script to nowhere and the & spins it into a background process.
     
    digitalpoint, Aug 17, 2005 IP
  11. JoeO

    JoeO Peon

    Messages:
    431
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'm looking into implementing this on a site this weekend. I have been reading up on it and I think I have found my answer, but can someone confirm this for me?

    To pass a variable to the file can I use the below code, then use a standard GET in the php file I am calling?

    
    exec ("/usr/bin/php yourscript.php \"id=$whatever\" >/dev/null &");
    
    PHP:
    Thanks in advance.
     
    JoeO, Sep 16, 2005 IP
  12. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,283
    Likes Received:
    2,599
    Best Answers:
    460
    Trophy Points:
    710
    Digital Goods:
    29
    #12
    digitalpoint, Sep 16, 2005 IP
  13. JoeO

    JoeO Peon

    Messages:
    431
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #13
    oh, I was way off in my reading.... that looks easy enough, thanks a lot Shawn.
     
    JoeO, Sep 16, 2005 IP
  14. sandersky

    sandersky Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I am wondering if I were to create a web crawler for a search engine using php/curl could i use this method to run a script on the server that does indexing and in that file use a redirect header to create a continuous loop that continues to index constantly? and if so how would i initially execute it?
     
    sandersky, Sep 28, 2007 IP
  15. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,562
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #15
    Ummm...this thread is 3 years old, start a new one
     
    blueparukia, Sep 28, 2007 IP
  16. msaqibansari

    msaqibansari Peon

    Messages:
    84
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Why don't you use AJAX? Its quite simple and solve your problem.
     
    msaqibansari, Sep 28, 2007 IP