Jump to content

How to check php page loading takes time ?


skwap

Recommended Posts

Hello Coders,

 

Iam in a confused situation. I made a php script & in that script i want to check how much time (in seconds) the page is taking to fetch the content from the server.

If the time is greater than to i defined time then i want to show a error message to the users.

 

 

Anybody can give me ideas ..................... ??

Link to comment
Share on other sites

Or use the optional parameter for microtime.

 

<?PHP

  // put at the top of the script
  $start = microtime(true);

  // put at the bottom of the script
  $end = microtime(true);

  $exec_time = $end_time - $start_time;

?>

 

Regards, PaulRyan.

Link to comment
Share on other sites

Maybe JS? In the beginning, when loading is started, set first JS variable, make it equal the current client's time. And also when page is loaded read the current time. Calculate the difference between that times and you will know the loading time.

 

BTW, do you expect a long period of time? Why do you like to control it?

 

PS. scootstah, PaulRyan - PHP solution is not correct :) It will show page preparation time. TC would like to know how long this page is transfered from server to client.

Link to comment
Share on other sites

Some outside of the box thinking from me, making use of set_time_limit and register_shutdown_function.

 

Try this on for size:

<?PHP

  //### The time in seconds the script can run for before error is shown.
  $max_time = 1;

  set_time_limit($max_time);
  register_shutdown_function('taking_too_long');
  
  //### Execute your PHP code below this
  
  for($i=0; $i<10000000; $i++) {
    echo '<!--'.$i.'-->';
  }
  
  //### Execute your PHP code above this
  
  function taking_too_long() {
    $error = error_get_last();
    
    if(strstr($error['message'],'Maximum execution time')) {
      echo '<script type="text/javascript">';
      echo '  document.body.innerHTML = "This is an error message, if the page takes to long to execute.";';
      echo ' </script>';
    }
  }
  
?>

 

Regards, PaulRyan.

Link to comment
Share on other sites

Some outside of the box thinking from me, making use of set_time_limit and register_shutdown_function.

 

Try this on for size:

<?PHP

  //### The time in seconds the script can run for before error is shown.
  $max_time = 1;

  set_time_limit($max_time);
  register_shutdown_function('taking_too_long');
  
  //### Execute your PHP code below this
  
  for($i=0; $i<10000000; $i++) {
    echo '<!--'.$i.'-->';
  }
  
  //### Execute your PHP code above this
  
  function taking_too_long() {
    $error = error_get_last();
    
    if(strstr($error['message'],'Maximum execution time')) {
      echo '<script type="text/javascript">';
      echo '  document.body.innerHTML = "This is an error message, if the page takes to long to execute.";';
      echo ' </script>';
    }
  }
  
?>

 

Regards, PaulRyan.

i think this method is not beneficial for me because my script was for a mobile site & most of mobile devices are not support java script.

Also it will not work in user's browser who disabled java script.

Link to comment
Share on other sites

...because my script was for a mobile site & most of mobile devices are not support java script.

Also it will not work in user's browser who disabled java script.

The only way to measure load time is to do it on client side. If it's not possible because of any reason - it's impossible at all. As I told already it this topic "PHP solution is not correct  It will show page preparation time."

Link to comment
Share on other sites

There's no reliable way to do what you want with PHP. The best you can do is measure the time it takes for the script to execute.

Means ?

 

PaulRyan post is beneficial for me ?

What do you think ?

 

Means that, what you want to do is not possible.

 

If all you want is the time it takes to load the DOM, most browsers do that already. Although I'm really not sure why you even care about such a thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.