Hello again - I just spent the past hour and a half writing a little iFrame polling script because iFrames are REALLY ugly and they usually end up with scrollbars that you don't want.
I needed nicely formatted output with no scrollbars.
Change the iframe code to this:
<iframe id="iframe_source" src="commands.php" style="display: none;" />
// At an interval of 250 milliseconds, update the content of "visual_destination" with the data that is currently loaded in the "iframe_source" element.
new iFramePoller("iframe_source", "visual_destination", 250);
* Copyright 2009 Michael White
* You may contact me at: michael--at--getsprink--dot--com
* Website: http://getsprink.com
* LICENSE: This code is free for use and modification provided this copyright notice and license remains in tact.
* This software comes without ANY warranty, expressed or implied. (i.e. - this code may or may not work for you!)
* If you do use this code or a modified version of it on a project/website I would like to know! Contact me at the e-mail address listed above.
* If you would like help implementing or modifying this software for your solution, contact me at michael--at--getsprink--dot--com
// This script is used to poll an iframe on a set interval and update an HTML container element.
// Data is checked each time a content check is made to make sure that the data has actually changed prior to updating the destination element.
// Also, the onload event is listened for on the iFrame and that event triggers the stopPolling() function and no more updates are made after that point.
// It makes sense to set the iframe's display property to "display: none;" so that only the other HTML container element is visible.
will be treated just like this code written in only php:
as long as you are not using output buffering on your page (ob_start()) then the effect will be exactly what you are looking for since the buffers will be flushed to the browser after each line of command output.
But of course there is a caveat!
It doesn't work with executing custom shell scripts! (well - not without a small workaround)
the workaround is really simple though. Just use file_get_contents("myscript.sh") to turn the shell script contents into a command string. Insert that command string into your system() command and voila! The output buffers are flushed as expected.
Note: One thing i did *not* test was the effect of this process on the working directory for the execution of the shell script commands. However, if your shell script relies on a particular working directory then make sure that the working directory is either set inside the shell script itself or that you do a "cd /custom/dir/here" (change directory) command just prior to where you insert the shell script command string.
and for those of you (like me) who are using output buffering on the entire page for whatever reason (templates, caching, error handling) - there is still a little more hacking to do. As long as your output is (x)html then the following solution will work for fully output buffered pages (ob_start()).
Separate your system() and passthru() calls into a plain php file that just outputs plain text: (obviously make sure that you force some sort of user authentication prior to actually running the commands.php script!)
Usually a shell script on a web host will run in the area of the server that is not accessible to the public side of operations, this in many hosts is a security precaution.
The host I use has this kind of feature set up.
On the host I use I have a shell script that runs every hour to scrape a file in the webspace thats public, it is on the shell side a simple test to see if the hour is equal to 1/4 of a 12 hour period.
If not then operations cease.
If it is then the shell script calls a PHP script that then does the processing of the data, pushes information in to a datafile and database.
On the public side of things, the public page reads the file and database, no need to call a shell script web side as the process has been automated to write a data file and push data in to a database that is then made publicly available.
So you might want to set up a shell that is cronjobbed every few minutes that writes or pushes your data via a PHP script.
08-14-2013, 05:24 PM
Originally Posted by nipun1229
It worked like a dream !!! Thanks
Wow - I had completely forgotten that I wrote that up and posted those examples. Kind of cool to see someone is able to make good use of it four years after I wrote it. It certainly helps to vindicate the hours of research I spent figuring it all out. :D