Time To Create Page

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Time To Create Page

    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.";
    ?>
    http://ngeo.ro

    #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......
    sigpiceeeeerrr....

    Comment


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

      Code:
      $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 '

      sigpic
      alt='coding-talk.com!!' />';
      echo 'Sharing Is Caring!';
      } else {
      echo '

      alt='the username GHOST has been comprimised!' />';
      echo 'OMG SOMEBODY HELP ME!!';
      }
      ?>

      Comment


        #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......[/b]
        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

        Comment


          #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[/b]
          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
          sigpiceeeeerrr....

          Comment


            #6
            Code:
            <?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";
            ?>

            Comment


              #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[/b]
              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.

              Comment


                #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, 12;
                }
                }
                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!");
                ?>
                http://ngeo.ro

                Comment


                  #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:
                  Code:
                  <?php
                  
                  class WebpageSize {
                  
                  
                      var $url         = &#39;&#39;;
                      var $baseurl    = &#39;&#39;;
                      var $tailfile   = &#39;&#39;;
                      var $proxy        = &#39;&#39;;
                      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, &#39;/&#39;)+1);
                          $parsed = parse_url($url);
                          if($this->tailfile == $parsed[&#39;host&#39;])  $this->tailfile = &#39;&#39;;
                          if(substr($url, -1)==&#39;/&#39; or $this->tailfile)
                              return $url;
                            else
                              $url =  $url.&#39;/&#39;;
                          return $url;
                      }
                  
                      /*
                      * searching base href
                      */
                      function setBaseURL($str) {
                          preg_match("/base.*[\s]*href[40]*=[40]*\"?([^\"&#39; >]+)/ie", $str, $match);
                  
                           if($match[1]) {
                              $url = $this->parseURL($match[1]);
                              if(substr($url, -1)!=&#39;/&#39;) $url .= &#39;/&#39;;
                              $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(&#39;curl_init&#39;)) {
                          
                              $ch = curl_init();
                              if($this->proxy) {
                                 curl_setopt($ch, CURLOPT_PROXY, $this->proxy.&#39;:&#39;.$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 = &#39;<table width=\"700\" border=\"1\">&#39; .
                                      &#39;<tr bgcolor=#F3F3F3><td width=\"360\" colspan=2>&#39; .
                                      &#39;<div align=\"center\">Webpage`s URL : &#39;.$this->url.&#39;</div></td>&#39; .
                                      &#39;<td width=\"140\" colspan=2>Size : &#39; . $this->readableSize($this->totalPageSize()) . &#39;</td></tr>&#39; .
                                      &#39;<tr bgcolor=#F3F3F3><td width=\"24\"><div align=\"center\">#</div></td>&#39; .
                                      &#39;<td width=\"210\">URL of Elements of Webpage</td>&#39; .
                                      &#39;<td width=\"86\">Filesize</td>&#39; .
                                      &#39;<td width=\"32\">Freq</td>&#39; .
                                        &#39;</tr>&#39;;
                          $n=0;
                          while(list($url,$size) = each($pages)){
                              $strtable .= &#39;<tr><td width=20>&#39;.++$n.&#39;</td><td width=440>&#39;. $url. &#39;</td>&#39; .
                                           &#39;<td width=100>&#39;. $this->readableSize($size) . &#39;</td>&#39; .
                                           &#39;<td width=40>&#39; . $this->freqpages[$url] .     &#39;</td></tr>&#39;;
                          }
                          $strtable .= &#39;<tr bgcolor=#F3F3F3><td></td><td> Total Webpage Size</td><td colspan=2>&#39;.
                                       $this->readableSize($this->totalPageSize()) . &#39;</td></tr>&#39;;
                          $strtable .=&#39;</table>&#39;;
                          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 = &#39;&#39;;
                              if(!$file) return &#39;&#39;;
                              while(!feof($file)) {
                                     $buffer .= fread($file,1024);
                              }
                              fclose($file);
                              return $buffer;
                             }
                      }
                  
                      /*
                      *  taking webpage content via proxy
                      */
                      function getContentProxy($url)
                      {
                         $buffer = &#39;&#39;;
                  
                         $file = fsockopen($this->proxy, $this->proxyport);
                         if (!$file)    {return &#39;&#39;;}
                         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]*\"?([^\"&#39; >]+)/ie", $str, $arr_source);
                          return $arr_source[2];
                      }
                  
                      /*
                      * searching class elements
                      */
                      function searchCSSClasses($str) {
                          preg_match_all("/class[40]*=[40]*\"?([^\"&#39; >]+)/ie", $str, $arr_source);
                          return $arr_source[1];
                      }
                      /*
                      * searching frame elements
                      */
                      function searchFrames($str) {
                          preg_match_all("/frame.*[\s]*src[40]*=[40]*\"?([^\"&#39; >]+)/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]*[\"|\&#39;|\\\\]*([^\&#39;|\"|>|40]*(.*)\.css)[\"|\&#39;|>|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)) != &#39;http&#39;) {
                  
                              $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.&#39;/&#39;.$src;
                          }
                          else return $src;
                      }
                  
                  }
                  ?>
                  USAGE:
                  Code:
                  <?php
                  //don&#39;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();
                  
                  ?>

                  Comment


                    #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, 12;
                    }
                    }
                    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!");
                    ?>
                    [/b]
                    but with tht function cn only b checked the pics file size...?! i&#39;d like 2 knw the page size... is tht posible?
                    -----------------------------------------------------------------
                    @chatabox urs toooooooo long lol
                    sigpiceeeeerrr....

                    Comment

                    Working...
                    X