943,547 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6461
  • PHP RSS
Sep 26th, 2008
0

Inserting delay in PHP scripts

Expand Post »
Hi all,

I am writing a PHP script that executes shell commands on a linux machine. When I run this script on Linux system, it waits while it executes the command and store the result in a PHP variable. But when I run it on a web browser, the browser don't wait to get result of the command execution and moves on. I want that browser to wait 2 seconds while it executes that command. How can I do this. Please help me.

The PHP Code is as under:-
PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <?php
  4.  
  5. $cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
  6. echo "The Current CPU Utilization is ".$cpu;
  7. <?php
  8. $mem=exec('top -n 1|grep Mem');
  9. echo $mem;
  10. ?>
  11. </body>
  12. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Sep 26th, 2008
0

Re: Inserting delay in PHP scripts

Below is a script I made which will delay the page by 2 seconds however, although the page will be processed with the delay, you may find that the whole page may display at the same time (after 2 seconds).
php Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <?php
  4. $oldseconds=date(s);
  5. if ($oldseconds==58)
  6. {
  7. $newseconds=='00';
  8. } else
  9. if ($oldseconds==59)
  10. {
  11. $newseconds=='01';
  12. } else
  13. {
  14. $newseconds=$oldseconds+2;
  15. }
  16. while (date(s)!=$newseconds)
  17. {
  18. //anything before this loop may not be delayed
  19. //anything after this loop will be delayed.
  20. // But with some browsers or servers this
  21. // loop will delay the entire page
  22. }
  23. //place below here what you want after 2 seconds
  24. $cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
  25. echo "The Current CPU Utilization is ".$cpu;
  26. <?php
  27. $mem=exec('top -n 1|grep Mem');
  28. echo $mem;
  29. ?>
  30. </body>
  31. </html>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 26th, 2008
0

Re: Inserting delay in PHP scripts

you can also use sleep
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Sep 27th, 2008
0

Re: Inserting delay in PHP scripts

Click to Expand / Collapse  Quote originally posted by cwarn23 ...
Below is a script I made which will delay the page by 2 seconds however, although the page will be processed with the delay, you may find that the whole page may display at the same time (after 2 seconds).
php Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <?php
  4. $oldseconds=date(s);
  5. if ($oldseconds==58)
  6. {
  7. $newseconds=='00';
  8. } else
  9. if ($oldseconds==59)
  10. {
  11. $newseconds=='01';
  12. } else
  13. {
  14. $newseconds=$oldseconds+2;
  15. }
  16. while (date(s)!=$newseconds)
  17. {
  18. //anything before this loop may not be delayed
  19. //anything after this loop will be delayed.
  20. // But with some browsers or servers this
  21. // loop will delay the entire page
  22. }
  23. //place below here what you want after 2 seconds
  24. $cpu=exec('top -d 1 -n 1|grep Cpu | tail -n 1 | cut -c 25-33');
  25. echo "The Current CPU Utilization is ".$cpu;
  26. <?php
  27. $mem=exec('top -n 1|grep Mem');
  28. echo $mem;
  29. ?>
  30. </body>
  31. </html>
It looks like you have a syntax error in your PHP. That is probably why it is returning immediately.

PHP should wait for the exec() command to complete, unless you specified a background process with & in the command.

Remove the <?php in:

PHP Syntax (Toggle Plain Text)
  1. echo "The Current CPU Utilization is ".$cpu;
  2. <?php
  3. $mem=exec('top -n 1|grep Mem');

ps:

Output can also be buffered by PHP and/or the sever. Use ob_flush() and flush() to flush the buffers.
IE6 also doesn't render any output until it receives 256 bytes.

You should make sure you have 256 bytes if you want to display in IE6.

PHP Syntax (Toggle Plain Text)
  1. if (strlen($content) < 256) {
  2. $content = str_pad($content, 256); // IE hack
  3. }
  4. echo $content;
  5. @ob_flush();
  6. flush();
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Web Shell
Next Thread in PHP Forum Timeline: I have problem with two value search





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC