skwap Posted February 8, 2012 Share Posted February 8, 2012 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 ..................... ?? Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/ Share on other sites More sharing options...
scootstah Posted February 8, 2012 Share Posted February 8, 2012 // put at the top of the script $start_time = array_sum(explode(' ',microtime())); // put at the bottom of the script $end_time = array_sum(explode(' ',microtime())); $exec_time = $end_time - $start_time; Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1315915 Share on other sites More sharing options...
PaulRyan Posted February 8, 2012 Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1315917 Share on other sites More sharing options...
SergeiSS Posted February 8, 2012 Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1315918 Share on other sites More sharing options...
PaulRyan Posted February 8, 2012 Share Posted February 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1315927 Share on other sites More sharing options...
skwap Posted February 9, 2012 Author Share Posted February 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1316013 Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1316024 Share on other sites More sharing options...
skwap Posted February 9, 2012 Author Share Posted February 9, 2012 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1316037 Share on other sites More sharing options...
SergeiSS Posted February 9, 2012 Share Posted February 9, 2012 ...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." Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1316058 Share on other sites More sharing options...
scootstah Posted February 11, 2012 Share Posted February 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256689-how-to-check-php-page-loading-takes-time/#findComment-1316995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.