1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how can i get this done

Discussion in 'PHP' started by mumfry, Aug 11, 2010.

  1. #1
    hey guys

    am trying to implement this php script which displays how long my web page takes to load in seconds

    the code is two parts.
    this first part goes at the beginning of the web page
    <?
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $start = $time;
    ?>

    then the final part, that echos the number of seconds it took to load the page goes at the end of the web page

    <?
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 4);
    echo '<p>Page generated in '.$total_time.' seconds.</p>'."\n";
    ?>

    the entire script works fine, but the problem i am having is that the time gets displayed only at the end of the web page where the echo code was placed

    but instead i would like to have it echo the results at the top of the page instead of the end

    i tried to put it in a function but it didn't work out

    any ideas would be greatly appreciated
     
    mumfry, Aug 11, 2010 IP
  2. GetGamesHere

    GetGamesHere Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use an iframe to display it at the top, as part of its source url, pass in the start time.
     
    GetGamesHere, Aug 11, 2010 IP
  3. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use the output buffering functions, which let you modify the final page before rendering. The first code comment contains an explanation of what's going on.

    
    <?php
    /*
    	this function is called when
    	the output is ready to be flushed
    	to the browser.  We calculate our
    	page creation time and substitute it 
    	into a string on our web page.  For
    	this example, we look for the string
    	### and substitute that.
    */
    function ob_buffer_callback($buffer)
    {
    	global $start;		// hacky, but necessary
    	
    	$time = microtime();
    	$time = explode(' ', $time);
    	$time = $time[1] + $time[0];
    	$finish = $time;
    	$total_time = round(($finish - $start), 4);
    
      	// replace our time placeholder with the actual value
      	return (str_replace("###", $total_time, $buffer));
    }
    
    // calculate start time
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $start = $time;
    
    // start buffering output
    ob_start(ob_buffer_callback);
    ?>
    <html>
    	<head>
    	</head>
    	<body>
    		<p>Page loaded in ### seconds</p>
    		<p>More content</p>
    		<div>Maybe a div too</div>
    	</body>
    </html>
    <?php
    ob_end_flush();
    ?> 
    
    PHP:
     
    sea otter, Aug 11, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    ^^ you could also do it with javascript or css
     
    gapz101, Aug 12, 2010 IP
  5. Aeroforce

    Aeroforce Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If the user does not have Javascript enabled (a small percentage do, but it is still a possibility) then the page load time will not be displayed at all. This is not good.
    CSS is a styling language, you can't calculate stuff like loading times with it.
     
    Aeroforce, Aug 12, 2010 IP
  6. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #6
    dude it can, actually, with positioning, just tricking
     
    gapz101, Aug 12, 2010 IP
  7. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    For this problem the only thing CSS can provide is the ability to potentially set the display type to none until the page is finished loading, then you can use javascript to unhide it when the page is finished. That would be a viable javascript approach.

    Your page might have a paragraph with an id of "pageRenderTime" and the css for that is display: none and the tag is empty.
    After your entire page loads have javascript set the display back to inline once you've set the render time information.
     
    AntelopeSalad, Aug 12, 2010 IP