Jump to content

Recommended Posts

I have an heavy script to be executed.

 

If I put the code as main script the user will see a loading blank page for a while, until he can keep to navigate the site.

 

I'd like that the main script throw a background script that execute the heave code, while the user can keep to navigate the site.

Link to comment
https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208298
Share on other sites

you could launch a background process

function launchBackgroundProcess($call) {
     // Windows
    if(is_windows()){
        pclose(popen(’start /b ‘.$call.”, ‘r’));
    }
     // Some sort of UNIX
    else {
        pclose(popen($call.‘ /dev/null &’, ‘r’));
    }
    return true;
}

function is_windows(){
    if(PHP_OS == ‘WINNT’ || PHP_OS == ‘WIN32′){
        return true;
    }
    return false;
}

 

Link to comment
https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208311
Share on other sites

this function will submit an asynchronous POST query to the specified url

function backgroundPost($url){
  $parts=parse_url($url);

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

  if (!$fp) {
      return false;
  } else {
      $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($parts['query'])."\r\n";
      $out.= "Connection: Close\r\n\r\n";
      if (isset($parts['query'])) $out.= $parts['query'];

      fwrite($fp, $out);
      fclose($fp);
      return true;
  }
}

//Example of use
backgroundPost('http://example.com/slow.php?file='.
                       urlencode('some file.dat'));

 

should work for ya

 

Link to comment
https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208320
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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