I wrote a class called Timer that will attempt to time how long it takes to evaluate and load a page.
/**
* Class for implemention of various timers
*
* @author Phil Powell
* @version 1.0.0
* @package MU-SPIN
*/
class Timer extends View {
/**
* @access private
* @var float $startTime
*/
var $startTime;
/**
* @access private
* @var mixed $url
*/
var $url;
/**
* Constructor. Set optional URL property
*
* @access public
* @param mixed $url (optional)
*/
function Timer($url = '') { // CONSTRUCTOR
$this->url = $url;
}
//-------------------------------------------- --* GETTER/SETTER METHODS *-- ------------------------------------------
/**
* Retrieve $url property
*
* @access private
* @return mixed $url
*/
function &getURL() { // STRING METHOD
return $this->url;
}
/**
* Set the time
*
* @access private
* @param float $timeKeeper (reference)
* @return float $timeKeeper
*/
function &setTime(&$timeKeeper) { // STATIC FLOAT METHOD
$start = microtime();
$start = explode(' ', $start);
$start = (float)$start[1] + (float)$start[0];
$timeKeeper = $start;
return $timeKeeper;
}
/**
* Set $url property
*
* @access private
* @param mixed $url
*/
function &setURL($url) { // VOID METHOD
if (is_object($this) && !$this->url) $this->url = $url;
}
//--------------------------------------------- --* END OF GETTER/SETTER METHODS *-- -------------------------------
/**
* Display HTML based on given URL property value
*
* @access public
* @param mixed $url (optional)
* @return mixed HTML
*/
function &displayHTML($url = '') { // STATIC HTML STRING METHOD
if (is_object($this) && !$this->getURL() && $url) $this->setURL($url);
if (is_object($this)) $url = $this->getURL();
if ($url && ini_get('allow_url_fopen')) {
$this->setTime($this->startTime); // SET START TIME
$html = @file_get_contents($url);
$timer = $this->setTime($timer);
$html = preg_replace('/(.*<body[^>]*>)(.*)(<\/body>.*)/i', '$1<p><b>' . ($timer - $this->startTime) . ' seconds</b>' . '$2$3', $html);
}
return $html;
}
}
However, the problem lies in the displayHTML() method whereby the line:
$html = @file_get_contents($url);
will produce wrong results if $url happens to point to a URL that is either $COOKIE or $SESSION - dependent.
What on earth do I do in the method to ensure those are available prior to the "scrape" of the URL? I have no clue what to do at this point regarding that and this class produces false results.
Furthermore, the preg_replace() line also fails to display the time anywhere in the URL:
$html = preg_replace('/(.*<body[^>]*>)(.*)(<\/body>.*)/i', '$1<p><b>' . ($timer - $this->startTime) . ' seconds</b>' . '$2$3', $html);[/{PHP] - the pattern match fails every time.
Wow.
Phil