Time To Create Page

Started on the old forum in March 2008 · view the original in the archive · old links redirect here

9 replies · 695 views · started 18 years ago · 1 min read

5 participants
blackhowk Senior · 411 posts #1

shows the loading time of a page.

<? function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } $time_start = getmicrotime();

// start page content // this is dummy - content for ($i=0; $i <1000; $i++){ } // end page content

$time_end = getmicrotime(); $time = $time_end - $time_start; $time = round($time,6); echo "This page loaded in $time seconds."; ?>

alesh Lead · 604 posts #2

i wonder is there any way 2 make a code which will show how many KB transfered during the opening tht perticular page?

i mean if theres already a code 2 show the time of loading pages......

eeeeerrr....

Ghost Senior · 366 posts #3

i have an easier and shorter code that will do the exact same thing...

$time=number_format(microtime(),2);
echo "Script loaded in: $time Sec(s)";

but thanks for this other code that was posted also..

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
chatabox Senior · 361 posts #4

i wonder is there any way 2 make a code which will show how many KB transfered during the opening tht perticular page?

i mean if theres already a code 2 show the time of loading pages......

Good question.. I never thought of that but it would be a nice feature.. ir would be easy to get the size of the page but im not sure about ading the size of all images.. maybe there is a complete function for thisa on php.net.. mmm

alesh Lead · 604 posts #5

Good question.. I never thought of that but it would be a nice feature.. ir would be easy to get the size of the page but im not sure about ading the size of all images.. maybe there is a complete function for thisa on php.net.. mmm

i always wanted tht feature cuz gprs is expencive at my country... i wanted 2 include tht so i cn minimize page 2 specific size....but i never thought about pics on page.....ughhh

eeeeerrr....

hypetype Developer · 128 posts #6
<?php
function getmicrotime() 
{ 
    list($usec, $sec) = explode(" ", microtime()); 
    return ((float)$usec + (float)$sec); 
} 

$time_start = microtime(1);

for ($i=0; $i < 100000; $i++) {
}

$time_end = microtime(1);
$time = $time_end - $time_start;
echo "Script Timer: "; echo "[b]$time [/b]"; echo " seconds\n";
?>
chatabox Senior · 361 posts #7

i always wanted tht feature cuz gprs is expencive at my country... i wanted 2 include tht so i cn minimize page 2 specific size....but i never thought about pics on page.....ughhh

To get the size of each page is very simple one line of php but im really not sure how to total the images.. i will look into it later.

blackhowk Senior · 411 posts #8

a php function to get the size of a remote file without the need of download it.

<?php function remote_file_size ($url) { $head = ""; $url_p = parse_url($url); $host = $url_p["host"]; $path = $url_p["path"];

$fp = fsockopen($host, 80, $errno, $errstr, 20); if(!$fp) { return false; } else { fputs($fp, "HEAD ".$url." HTTP/1.1\r\n"); fputs($fp, "HOST: dummy\r\n"); fputs($fp, "Connection: close\r\n\r\n"); $headers = ""; while (!feof($fp)) { $headers .= fgets ($fp, 128); } } fclose ($fp); $return = false; $arr_headers = explode("\n", $headers); foreach($arr_headers as $header) { $s = "Content-Length: "; if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) { $return = substr($header, strlen($s)); break; } } return $return; }

// sample code print ("Google logo is " . remote_file_size ("http://www.google.it/intl/it_it/images/logo.gif") . " bytes!"); ?>

chatabox Senior · 361 posts #9

yes but that gets ONLY the file sixe, you can do it in a much smaller line than that with the file size php function.. it does not include images or css in the total eh... here is a class that will determine the true web page size but its a big clas and a waste of resources in my opinion...

Originally posted by http://codemafia.org/index.php?showtopic=5030 CLASS:

**```

<?php class WebpageSize {     var $url         = '';     var $baseurl    = '';     var $tailfile   = '';     var $proxy        = '';     var $proxyport    = 3128;     var $pages      = array();     var $freqpages  = array(); //frequency of page element to be loaded     function setURL($url) {          $this->url = $this->parseURL($url);     }     function parseURL($url) {         $this->tailfile = substr($url, strrpos($url, '/')+1);         $parsed = parse_url($url);         if($this->tailfile == $parsed['host'])  $this->tailfile = '';         if(substr($url, -1)=='/' or $this->tailfile)             return $url;           else             $url =  $url.'/';         return $url;     }     /*     * searching base href     */     function setBaseURL($str) {         preg_match("/base.*[\s]*href[40]*=[40]*\"?([^\"' >]+)/ie", $str, $match);          if($match[1]) {             $url = $this->parseURL($match[1]);             if(substr($url, -1)!='/') $url .= '/';             $this->baseurl = $url;         } else {             $this->baseurl = $this->url;         }     }     /*     *   if you access via proxy, use this     */     function setURLviaProxy($url,$proxy,$port) {         $this->setURL($url);         $this->proxy     = $proxy;         $this->proxyport = $port;                  }     /*     *  core function!     *  page elements and the size     */     function getResult() {         $paths      = $this->grabPageSources();         array_unshift ($paths, $this->url);         $pages    = array();         //cURL                  if(function_exists('curl_init')) {                      $ch = curl_init();             if($this->proxy) {                curl_setopt($ch, CURLOPT_PROXY, $this->proxy.':'.$this->proxyport);             }             curl_setopt($ch, CURLOPT_HEADER, 1);             curl_setopt($ch, CURLOPT_NOBODY, 1);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                  for($i=0; $i<count($paths); $i++){                      if(!array_key_exists($paths[$i],$pages) ){                     curl_setopt($ch, CURLOPT_URL, $paths[$i]);                     $headers = curl_exec ($ch);                     $filesize = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD);                     if(!$filesize) //try using fopen                         $filesize = strlen($this->getContent($paths[$i]));                     if(!$filesize)                         continue;                     $this->freqpages[$paths[$i]] = 1;                     $pages[$paths[$i]] = $filesize;                     $this->totalsize  += $filesize;                 } else                     $this->freqpages[$paths[$i]] += 1;             }             curl_close ($ch);         } else {                      for($i=0; $i<count($paths); $i++){                 if(!array_key_exists($paths[$i],$pages) ){                     $filesize = strlen($this->getContent($paths[$i]));                     $this->freqpages[$paths[$i]] = 1;                     $pages[$paths[$i]] = $filesize;                     $this->totalsize  += $filesize;                 } else                     $this->freqpages[$paths[$i]] += 1;             }                  }                  natsort($pages);         return $pages;     }     function totalPageSize() {         return $this->totalsize;     }     /*     * this one is usefull     */     function readableSize($size) {         return number_format($size/1024,2)." KB";     }     /*     *  pre-formated output     */     function printResult() {         $pages = $this->getResult();         $strtable = '<table width=\"700\" border=\"1\">' .                     '<tr bgcolor=#F3F3F3><td width=\"360\" colspan=2>' .                     '<div align=\"center\">Webpage`s URL : '.$this->url.'</div></td>' .                     '<td width=\"140\" colspan=2>Size : ' . $this->readableSize($this->totalPageSize()) . '</td></tr>' .                     '<tr bgcolor=#F3F3F3><td width=\"24\"><div align=\"center\">#</div></td>' .                     '<td width=\"210\">URL of Elements of Webpage</td>' .                     '<td width=\"86\">Filesize</td>' .                     '<td width=\"32\">Freq</td>' .                       '</tr>';         $n=0;         while(list($url,$size) = each($pages)){             $strtable .= '<tr><td width=20>'.++$n.'</td><td width=440>'. $url. '</td>' .                          '<td width=100>'. $this->readableSize($size) . '</td>' .                          '<td width=40>' . $this->freqpages[$url] .     '</td></tr>';         }         $strtable .= '<tr bgcolor=#F3F3F3><td></td><td> Total Webpage Size</td><td colspan=2>'.                      $this->readableSize($this->totalPageSize()) . '</td></tr>';         $strtable .='</table>';         echo $strtable;     }     /*     *  from "../../images/some.jpg" for example to "http://www.domain.com/images/some.jpg"     */     function resolvePathSources($paths) {         while(list(,$src) = each($paths))             $arr_path[] = $this->makeAbsolutePath($src,$this->baseurl);        return $arr_path;     }     /*     *  taking webpage content     *  fopen is lighter than cURL     */     function getContent($url){        if($this->proxy) {             return $this->getContentProxy($url);        } else {             $file =];             $buffer = '';             if(!$file) return '';             while(!feof($file)) {                    $buffer .= fread($file,1024);             }             fclose($file);             return $buffer;            }     }     /*     *  taking webpage content via proxy     */     function getContentProxy($url)     {        $buffer = '';        $file = fsockopen($this->proxy, $this->proxyport);        if (!$file)    {return '';}        fputs($file, "GET $url HTTP/1.0\r\nHost: $proxy_name\r\n\r\n");        while(!feof($file)) {$buffer .= fread($file,4096);}        fclose($file);        $buffer = substr($buffer, strpos($buffer,"\r\n\r\n")+4);        return $buffer;     }     /*     * searching webpage elements     */     function grabPageSources() {         $content  = $this->getContent($this->url);         $this->setBaseURL($content);         $arr_src1 = array();         $arr_src2 = array();         $arr_src3 = array();         $arr_src4 = array();         $arr_src5 = array();         $arr_src6 = array();                  $arr_src1 = $this->searchSources($content);         //search CSS classes that applied on page         $this->CSSclasses = $this->searchCSSClasses($content);        // print_r( $arr_clss );         $arr_src2 = $this->searchSourcesOnCSS($content);         $arr_src3 = $this->searchCSSLinks($content);         if(!empty($arr_src3))         $arr_src4 = $this->searchSourcesOnCSSFiles($arr_src3);         //search on frames if exists         $arr_src5 = $this->searchFrames($content);         if(!empty($arr_src5))         $arr_src6 = $this->searchSourcesOnFrames($arr_src5);         $arr_sources  = array_merge ($arr_src1, $arr_src2, $arr_src3,                                  $arr_src4, $arr_src5, $arr_src6);         return $this->resolvePathSources($arr_sources);     }     /*     * searching image/js elements     */     function searchSources($str) {         preg_match_all("/[img|input|embed|script]+.*[\s]*(src|background)[40]*=[40]*\"?([^\"' >]+)/ie", $str, $arr_source);         return $arr_source[2];     }     /*     * searching class elements     */     function searchCSSClasses($str) {         preg_match_all("/class[40]*=[40]*\"?([^\"' >]+)/ie", $str, $arr_source);         return $arr_source[1];     }     /*     * searching frame elements     */     function searchFrames($str) {         preg_match_all("/frame.*[\s]*src[40]*=[40]*\"?([^\"' >]+)/ie", $str, $arr_source);         return $arr_source[1];     }     /*     * searching css elements     */     function xsearchSourcesOnCSS($str) {         preg_match_all("/(url\(\"?([^\")]+))/ie", $str, $arr_source);         return $arr_source[2];     }      /*     * searching css elements     */     function searchSourcesOnCSS($str) {         preg_match_all("/(\.(.*)\s+\{[\s]+)*.*url\(\"?([^\")]+)/ie", $str, $arr_source);         for($i=0; $i<count($arr_source);$i++) {             if( in_array( $arr_source[2][$i], $this->CSSclasses )) {                 $arr_sources[] = $arr_source[3][$i];             }         }         return $arr_sources;     }     /*     * searching webpage elements on frames     */     function searchSourcesOnFrames($framefiles) {         $arr_source  = array();         $arr_sources = array();         while(list(,$src)   = each($framefiles)) {             $framepage        = $this->makeAbsolutePath($src,$this->baseurl);             $page = new WebpageSize;              if($this->proxy)                 $page->setURLviaProxy($framepage, $this->proxy, $this->proxyport);             else                 $page->setURL($framepage);             $arr_source  = $page->grabPageSources();             $arr_sources = array_merge($arr_sources, $arr_source);         }         return $arr_sources;     }     /*     * searching webpage elements on CSS files     */     function searchSourcesOnCSSFiles($cssfiles) {         //search sources on CSS file         $arr_CSSlinks = array();         while(list(,$src)   = each($cssfiles)) {             $numstepback    = substr_count($src, "../");             $CSSpage        = $this->makeAbsolutePath($src,$this->baseurl);             $CSScontent     = $this->getContent($CSSpage);             $arr_sourcelink    = $this->searchSourcesOnCSS($CSScontent);             if(empty( $arr_sourcelink )) continue;             while(list(,$srclink)   = each($arr_sourcelink)) {                 $arr_CSSlink[]  =   str_repeat("../",$numstepback) . $srclink;             }             $arr_CSSlinks   = array_merge($arr_CSSlinks, $arr_CSSlink);         }         return $arr_CSSlinks;     }     /*     * searching webpage elements on CSS     */     function searchCSSLinks($str) {          preg_match_all("/<link[^>]+href[40]*=[40]*[\"|\'|\\\\]*([^\'|\"|>|40]*(.*)\.css)[\"|\'|>|40|\\\\]*/ie",$str, $arr_CSSlink);         return $arr_CSSlink[1];     }     function makeAbsolutePath ($src,$url) {         $addone = 1;              if ($this->tailfile) {             $url = substr($url, 0, -(strlen($this->tailfile)+1));             $addone = 0;         }                  if (strtolower(substr($src,0,4)) != 'http') {             $numrel  = substr_count($src, "../");             $src     = str_replace("../","",$src);             for($i=0; $i < $numrel+$addone; $i++) {                 $lastslash  = strrpos($url,"/");                 $url       = substr($url, 0, $lastslash);             }           return $url.'/'.$src;         }         else return $src;     } } ?>

**USAGE:**

**```
<?php
//don't forget to set time limit, because this class eats a lot of time :)
set_time_limit(900);
include("class.webpagesize.php");

$size = new WebpageSize;
//$size->setURLviaProxy("http://www.netscape.com/","proxy.mycompany.com","3128");
$size->setURL("http://www.php.net/");
$size->printResult();

?>
```**
alesh Lead · 604 posts #10

a php function to get the size of a remote file without the need of download it.

<?php function remote_file_size ($url) { $head = ""; $url_p = parse_url($url); $host = $url_p["host"]; $path = $url_p["path"];

$fp = fsockopen($host, 80, $errno, $errstr, 20); if(!$fp) { return false; } else { fputs($fp, "HEAD ".$url." HTTP/1.1\r\n"); fputs($fp, "HOST: dummy\r\n"); fputs($fp, "Connection: close\r\n\r\n"); $headers = ""; while (!feof($fp)) { $headers .= fgets ($fp, 128); } } fclose ($fp); $return = false; $arr_headers = explode("\n", $headers); foreach($arr_headers as $header) { $s = "Content-Length: "; if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) { $return = substr($header, strlen($s)); break; } } return $return; }

// sample code print ("Google logo is " . remote_file_size ("http://www.google.it/intl/it_it/images/logo.gif") . " bytes!"); ?>

but with tht function cn only b checked the pics file size...?! i'd like 2 knw the page size... is tht posible?

chatabox urs toooooooo long lol

eeeeerrr....

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close