3

I'm running apache, php, mysql on windows 7. I'm using windows task schedule to open a 'cron job' (php script) on my localhost server. In php I would like to close that tab in the window after 30 minutes (to ensure the script has operated). I know how to do it in javascript using setTimeout() and window.close() functions, but I'm avoiding javascript in this code. Is there a way to do it in php? Or if anyone knows how to do it in windows task scheduler, that would be good too.

Anytime I searched for it on google, all that popped up was the javascript way of closing a window.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vince
  • 2,596
  • 11
  • 43
  • 76
  • 4
    your cron job should be calling the php script via the command line, no browser window(tab) should exist. –  Apr 30 '13 at 19:38
  • PHP is a server side script which can't do anything on the browser of the client. But if the server is on the same PC as the client you may take a look at the PHP `exec()` function and `taskkill` in windows, however this will close the whole browser instead of just the tab. – HamZa Apr 30 '13 at 19:40
  • @Dagon How would I do that? – Vince Apr 30 '13 at 19:42
  • 2
    the most basic example: `php.exe phpfile.php` –  Apr 30 '13 at 19:47
  • Ah, So I run php.exe through task scheduler with the file to be executed? – Vince Apr 30 '13 at 19:48
  • Using PHP to close a browser tab is quite similar to using a chainsaw to make a lemonade... but on a serious note, I suppose you could flush the needed HTML output, program your script to do its thing, and when it's done, echo the JavaScript (yes) code to close the window. – Shomz Apr 30 '13 at 19:52
  • 1
    See here: http://stackoverflow.com/questions/2833451/windows-server-task-scheduler-close-after – Patrick Moore Apr 30 '13 at 20:05
  • http://php.net/manual/en/book.gearman.php take a look at this one – Mehdi Karamosly Apr 30 '13 at 23:41
  • @Dagon It's still not working for me :( So I tried running php.exe without task scheduler and I'm getting a bunch of cannot find module at line 0. 17 of them! ugh! idk where to start – Vince May 02 '13 at 18:52

2 Answers2

0

If You don't need to see what You page displays, instead of using web browser, use some non-interactive tool that fetches websites, like wget or cURL. Both of them should be available as executable windows binaries. This will keep same behaviour for PHP script like normal webbrowser visit, except javascript which, you mentioned, wish to avoid, and will be not executed. Wget or cURL will only fetch content of generated page.

Wget - http://gnuwin32.sourceforge.net/packages/wget.htm

cURL - http://curl.haxx.se/download.html (on the bottom of page You have several version of cURL for windows).

Lucas
  • 69
  • 6
0

The following code would open the IE browser with the URL mentioned ($url) and in the sleep parameters pass the time upto which you want the window to remain opened (parameter is in seconds).

//The code starts from here:
$browser = new COM("InternetExplorer.Application");
$handle = $browser -> HWND;
$browser -> Visible = true;
$browser -> Width = 1100;
$browser -> Height = 700;
//$url=your url you want to open
print_r($browser -> Navigate($url));
while ($browser -> Busy) 
{
      com_message_pump(5000);
}
//The time you think is sufficient to open your web page
sleep(10);
$browser -> Quit();
War10ck
  • 12,387
  • 7
  • 41
  • 54
Amit
  • 195
  • 1
  • 1
  • 10