Tag Archives: sockets

2011 links no. 2

Stack Overflow profiler open sourced: Profiling your website like a true Ninja.

Smaller is Faster (and Safer Too). Chrome team have just started using a new compression algorithm called Courgette to make Google Chrome updates small. Read more.

Ask Amir Taaki About Bitcoin. From Slashdot.

The Four Stages of NTFS File Growth from Windows Server Core Team.

Some articles from CodeProject:

Rosa Golijan writes about what happens on the Internet every 60 seconds.

The Biggest Changes in C++11 (and Why You Should Care) by Danny Kalev.

Are You Too Good For Code Reviews? Articles and discussion in Slashdot.

Google’s Swiffy converts Flash SWF files to HTML5.

Have you wondered whether WordPress can handle high-volume traffic? High Traffic Tips For WordPress.

The most detailed story about Stuxnet: How Digital Detectives Deciphered Stuxnet, the Most Menacing Malware in History.

How to build your own 135TB RAID6 storage pod for $7,384 from extremetech.com.

Investigation: Is Your SSD More Reliable Than A Hard Drive? from tomshardware.com.

pdf.js is a technology demonstrator prototype to explore whether the HTML5 platform is complete enough to faithfully and efficiently render the ISO 32000-1:2008 Portable Document Format (PDF) without native code assistance. From Dr. Andreas Gal.

Make asynchronous call in PHP using PHP socket and custom User agent string

Recently I wrote about async calls in PHP. In the provided example from w-shadow.com there is one thing missing – User agent string.

Changing User agent string to something unique is handy, when analyzing log file. You can see, if someone is messing with your hidden asynchronous PHP script.

Here is an example, that uses PHP socket to make async call:

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.= "User-Agent: WishMesh.com custom user agent. Not Gecko!\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_async_script.php?id=’.
                urlencode(‘file.txt’));

Don’t forget to change “User-Agent” to something more meaningful for you.

The function backgroundPost opens TCP/IP connection to specified $url and makes HTTP POST. Right after data is submitted to the server, we close connection using fclose function, but the background script keeps running. If the background script is on the same server, we have two PHP scripts running simultaneously.

As suggested in the w-shadow.com I use ignore_user_abort(true); in the slow_async_script.php, so that PHP or Apache keep my script running even after we have aborted connection to it (using socket close command fclose).