<?php

require 'browserhawk.php';

	 // This sample is provided under the terms of the BrowserHawk End User
	 // License Agreement.  Copyright (c) 2003-2011 cyScape, Inc. All rights
	 // reserved.

	 // This example demonstrates how to use BrowserHawk to detect the user's
	 // connection speed throughput in bits per second.

	 // This sample uses an "extended" property check to detect connection
	 // speed.  For a tutorial on extended property checks please see the
	 // popular.php sample.

   // To avoid an ugly exception, we check that the system executing this
   // example has a Professional or Enterprise Edition license as required
   // for this test.
   // Note: This is NOT needed in your pages, only for this sample.
  if (strpos($BrowserHawk->getVersion(), "Pro") === false &&
      strpos($BrowserHawk->getVersion(), "Ent") === false) {
    echo "Professional or Enterprise License Required.  ";
    echo "You are using " . $BrowserHawk->getVersion() . ".  ";
    echo "See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.";
    return;
  }

   $options->setPageTitle("Please wait...");
   $options->setPageMessage("Please wait while your connection speed is detected...");
   $options->addProperties("ConnectionSpeed");

   $einfo = bhextended($options);
?>

<html>
	<head>
		<title>BrowserHawk Sample - Detect Connection Speed (PHP)</title>
	</head>
	<body><font face="arial">
	<blockquote>
	Your connection speed is: 
  <?php echo $einfo->getConnectionSpeed() ?> <P>

	Connection speed for display purposes: 
  <?php
    // The translate() method is a convenient way to display the speed in
    // bits/sec and kbytes per seocnd to end users so we demonstrate that here.
    echo $einfo->translate("ConnectionSpeed")
   ?> <P>

	<?php if ($einfo->getConnectionSpeed() == 0) { ?>
		Why is the result 0? The connection speed test is designed to detect 
    speeds generally T3 and lower.  Therefore if testing on a local machine,
    LAN, or WAN you may receive a return result of 0 which indicates that a
    valid reading was not available. The best way to test this sample is 
    over an internet connection such as cable modem, DSL, other broadband
    connections, or dial up.
	<?php } ?>

<?php

// Although this script does not demonstrate it, below is an example of 
// how you would get the result and do something with it.  For example, 
// assume you wanted to handle dial up and broadband users differently.
// To get the speed in bits per second just check the connectionspeed 
// property like this:
//    $speed = $einfo->getConnectionSpeed();
// If you want to calculate kbytes per second use this formula:
//    kbytesPerSec = speed / 8 / 1024;

// If you wanted to redirect users with slow connections here's how you can
// do it - in this example we redirect anyone slower than 128 kbits per 
// second to a more basic page
//   if ($speed > 0 && $speed < 131000) {
//      header("Location: http://yoursite.com/basicpage.php");
//   else if ($speed < 1)
//      header("Location: http://yoursite.com/detection_na.php");
//   else
//      header("Location: http://yoursite.com/highspeed.php");
//   }
// (The speed may be equal to 0 in the event that you are testing over a 
// local LAN/WAN since the speed is too fast to measure accurately in that 
// case.)
?>
<p>
Note: The ConnectionSpeed property should be used when you want to know the
vistor's actual connection speed, such as the case if you are estimating a
download time.  If you mainly just want to detect broadband vs. dial-up users,
the Broadband property is better suited for that purpose.  See the topic
"Detecting Connection Speeds and Dialup Users" in the BrowserHawk
documentation for more information. Also see the 
<a href="broadband.php">broadband sample</a>.

<P>
<HR>
<P>
<b><a href="speed.php">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other PHP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font></blockquote>
</body>
</html>
