Fake Server Push PHP help


New here and a php beginner trying to learn. I recently ran into a file that does fake server push which according to the author it updates image, etc by sending file updates to a browser. what i want to do or tried is to have it update another php file every x seconds but yet to make it work. I have been at it for a few days and only been coming up with errors . What it does From the author:

This PHP script provides a simple function to enable server push. All you need to do is tell it what file to serve, and update that file whenever a new version is available. The script also provides a JavaScript-based workaround for Internet Explorer (it may have limited success with other browsers), that uses more bandwidth, but at least it works, and avoids the need for the user to install a plugin - a common failed solution to this problem.

The file is in it’s original state so I can see if anyone can help me achieve what I want . Thanks in advance for any help…


Library to provide server push functionality
Version 1.0 - Written by Mark Wilton-Jones 16/7/2008

Please see http://www.howtocreate.co.uk/php/ for details
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms and conditions of use

PHP/server requirements:

  • Requires PHP 4.2.0+
  • Cannot be used on servers where PHP flush is disabled, such as those running mod_security, mod_gzip, suPHP, some win32 installs
  • PHP needs to be allowed to set max_execution_time (PHP must not be running in safe mode)
  • PHP needs to be allowed to disable compression, or compression must be disabled by default
  • Server push typically uses a child thread per connection - ensure your server allows enough to cope with all your visitors

void doServerPush( string: file_path, string: MIME_type, int: poll_time_in_ms )
Serves the specified file, along with any updates, using server push.
Does not return at all unless ignore_user_abort is forced.
void enableFakeServerPush()
For use on the page where the pushed content is used
Creates a JavaScript that can simulate server push in browsers that do not support it (specifically Internet Explorer).
Reloads the file even if it has not changed, so uses more bandwidth than true server push.

JavaScript API:
void fakeServerPush( object: element, string: property, string: newsrc, int: poll_time_in_ms )
Every poll, the specified property on the element is set to the newsrc, along with a random query string to prevent caching.
Waits for the next file to load before waiting for the next poll interval.

Using server push is as simple as this:
Every time the some_image.jpg file is changed on the filesystem, the update will be sent to the browser.
Recommended poll is 50+ with about 1000 (1 second) or more being normal - poll needs to be long enough to send the file to the
user, and will be affected by the user’s network speed.
doServerPush does not attempt to sanitise file paths - the script that calls the doServerPush function must ensure that the file
path it is serving is safe for the user to see.

Using the script to simulate server push in browsers that do not support it, relies on the user having JavaScript enabled:
Somewhere in the document head, include this file then call the enableFakeServerPush function to create the JavaScript.
Then add an onerror event handler to the image tag being used to display the pushed content, which then calls fakeServerPush.
Poll time is not affected by network speed.

Example 1:

Example 2 (better fallback - static image - if IE has JS disabled):



function doServerPush($file,$type,$poll) {

if( !file_exists($file) ) {
	//on first load, the file must exist, or it ends up sending a broken file
	header('Content-type: text/html',true,404);

@set_time_limit(0); //PHP must not terminate this script
@ignore_user_abort(false); //do not ignore user aborts (may not be allowed, so status is checked in the loop)
@ini_set( 'zlib.output_compression', 'off' );
$poll *= 1000;

//need a unique boundary
//while it is possible to get a unique boundary for the current image data, it is possible that it will conflict
//with future image data - can't help this, just have to live with it - might as well use a static string
$separator = 'MTWJ_serverpush_boundary_';
for( $i = 0, $randChars = Array('A','B'); $i < 20; $i++ ) {
	$separator .= $randChars[rand(0,1)];

header('Cache-Control: no-cache');
header('Pragma: no-cache');
//the header that makes server push work
header("Content-Type: multipart/x-mixed-replace;boundary=$separator",true);
$extrabreaks = '';

do {
	//send one file starting with a multipart boundary
	$filelen = filesize($file);
	print "$extrabreaks--$separator\n";
	if( !$extrabreaks ) { $extrabreaks = "\n\n"; } //Safari needs exactly one blank line, no extra linebreaks on the first one
	print "Content-Type: $type\n";
	print "Content-Length: $filelen\n\n";
	//Safari needs 200+ bytes between headers - IE needs 256+ per flush, and this will also take care of that, when IE gets server push
	print str_pad('',200-$filelen); //whitespace here is ignored because of content-length
	$lastupdsate = filemtime($file);
	$looptime = time();
	do {
		clearstatcache(); //need updated file info
		if( time() - $looptime >= 10 ) {
			//every 10 seconds, force it to re-evaluate if the user has disconnected (connection_aborted does not know until this happens)
			//most use-cases will not need this protection because they will keep trying to send updates, but it is here just in case
			$looptime = time();
			print ' '; //whitespace is ignored at this specific point in the stream
		//poll the filesystem until the file changes, then send the update
		//the file may not always exist for the instant it is being replaced on the filesystem
		//nested loop and filemtime check inspired by http://web.they.org/software/php-push.php
	} while( !connection_aborted() && ( !file_exists($file) || ( $lastupdsate == filemtime($file) ) ) );
} while( !connection_aborted() ); //if aborts are ignored, exit anyway to avoid endless threads


function enableFakeServerPush() {
//in theory, it would be possible to reduce bandwidth by making this script use XMLHttpRequest HEAD requests to check if-modified-since
//(fetching the last update date from JavaScript), but that has numerous flaws:
// * it only works within the same domain, but most server push is done with images from other domains
// * not all servers use if-modified-since correctly for dynamic content
// * not all servers respond correctly to head requests containing if-modified-since
// * it would rely on the user’s computer clock being correct, and a disturbingly large number are not
// * it over-complicates matters and adds more points of failure, when all that is needed is something that just works




Sponsor our Newsletter | Privacy Policy | Terms of Service