This is probably a very simple answer that I'm missing.

I have index.php and qeury.php in the same directory. Users visit index.php, which prints some info. It then needs to call qeury.php and exit, without waiting for query to finish running. Query just pings a server and then exits.

How does one accomplish this?

Recommended Answers

All 2 Replies

Member Avatar for Zagga

Hi Xcelled194,

It depends what you mean by 'call query.php'.

You could include("query.php"); or you could redirect, header("location:query.php"); as a few examples depending on whether you want to keep control in index.php or pass control to query.php.

What I was looking for is to basically spawn another process, then exit immediately. I don't care about the output (there is none), I just want the PHP file to run. I solved it like this:

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$lm = substr($url, 0, strlen($url)-strlen(strrchr($url, '/'))+1);
curl_post_async($lm . 'query.php');
die();

function curl_post_async($url)
{
   $post_string = '';

   $parts=parse_url($url);

   $fp = fsockopen($parts['host'],
       isset($parts['port'])?$parts['port']:80,
       $errno, $errstr, 30);

   $out = "POST ".$parts['path']." HTTP/1.1\r\n";
   $out.= "Host: ".$parts['host']."\r\n";
   $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
   $out.= "Content-Length: ".strlen($post_string)."\r\n";
   $out.= "Connection: Close\r\n\r\n";
   if (isset($post_string)) $out.= $post_string;

   fwrite($fp, $out);
   fclose($fp);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.