Jump to content

[SOLVED] Automated code


ninedoors

Recommended Posts

I am currently running a php script every night on a desktop computer that retrieves data from a history server.  The computer had xampp 1.6 I think installed.

 

The script runs at midnight and is opened by the windows task scheduler.  I would like the script to be able to close the browser window when it is finished running because eventually I will have a couple different aumated scripts running at various times. 

 

Right now when I get to work I have to close the windows manually.  I googled it and saw that it can't be done with php but javascript has a window.close() function. 

 

I have tried a couple different ways to run this but with not luck.  Here is the last piece of code I tried.

 

<?php
function hello()	{

echo "hello world";
}

hello();
?>
<script>
action="javascript::window.close()"
</script>

 

Which I have all inside proper html tags.  Anyone have any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/104539-solved-automated-code/
Share on other sites

Im sure there are better solutions, but what i can think is that u run the php before the body (which shouldnt be a great way) and execute the window.close() on body load:

 

<body onload="window.close();">

 

Or just make it automatic, like after 5 seconds so ure sure your script has been executed. For the delayed close take a look at this tutorial.

GuiltyGear,

I was thinking the same thing because I have used it before in similar circumstances. I just pieced a small script together to test this before I told him but it's not working. I read the error console and I get a

Warning: Scripts may not close windows that were not opened by script.

So I am thinking that this won't work.

An alternate solution would be using:

<?php
header('Refresh: 10');
?>

With this it will automatically refresh every 10 seconds. So at the end of the day you can start this page and minimize it. It will run every ten seconds. Throw a test for time in there to execute your retrieval of data. In the morning when you come in just exit out.

 

Other than that I am stuck looking for better ways.

While there is probably a parameter you can add to end of the command line you are using in the task scheduler to close the application when it finishes (Google will probably give the result), it is better to avoid using the browser at all.

 

Use a .vbs script.

 

Sample task scheduler command line (replace with actual path on your system) -

 

"C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\scheduled.vbs"

 

scheduled.vbs -

 

'************************************************
'* schedule this VB Script to fetch/execute a web page
'************************************************

url = "http://yourdomain.com/anyfile.php"


goURL url, "GET", ""


sub goURL(url, method, data)
'uses the MSXML COM for http request
set objReq = CreateObject("Microsoft.XMLHTTP.1")
    If method = "GET" Then
        objReq.open "GET", url & "?" & data, False
        objReq.Send ""
    Else
        objReq.open "POST", url, False
        objReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        objReq.Send data
    End If
    set objReq = nothing
end sub

Thanks guys.  Got it working with that tutorial.  The only problem was that it was poping up a warning saying "Are you sure you want to close this browser?"

 

I found a work around through google though.  Here's what I ended up using:

 

<?php

//some php code that I need to run

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
<!--
var sec = 0;
function closewindow() {
sec++;
if (sec == 10) {
window.opener='x';
window.parent.close();
}
window.setTimeout("closewindow();", 1000);
}
//-->
</script>
</head>

<body onLoad="closewindow();">

</body>
</html>

 

Thanks again for the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.