cr-ispinternet Posted January 2, 2008 Share Posted January 2, 2008 Hi all, ive got several logs which are generated when certain services run in php triggered by cron, im currently out putting the results to the logs what i need or want to do is 2 things... 1) Be able to read the logs via a button in a web browser 2) be able to watch the text file in real time in a tail -f style but in a window embedde dina web page does nay one know how i can acheive this, any ones input would be really appreciated Kind regards Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/ Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 1/ <?php echo shell_exec('tail -n 50 /var/log/logfile.log');?> 2/ We have an Ajax board. Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428191 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Thorpe, This is why i love this place so much, thanks so much for your insight much apreciated it really is... Checking out the ajax board now Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428194 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi, Have run in to a problem though with number 1, im getting a shell_exec error saying its disabled for security reasons, i just cant figure out how to enable it via my plesk platform, php safe mode is already off Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428215 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 With that disabled, you may actually have to read the entire file into any array and then just display the last 50 (or however many) lines you want. <?php $lines = file('/var/log/logfile.log'); for ($i = count($lines)-50; $i < count($lines); $i++) { echo $lines[$i]."\n"; } ?> Given the fact that log files have a tendency to get rather large though, this probably isn't a real good solution. You may need to speak to your host provider to find out why you cannot get shell access to tail. Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428233 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi There, yeah seems to work ojk, i am the provider i have a server running plesk with full root access ive been thorugh everything and cant seem to find anything which relates to a problem with it reading and exec the shell command... Still trying desperately to find this real time ajax script though nothing seeems to fit the bill :-( alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428308 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 If you have shell access, log in via shell and change the php.ini. Ive never used plesk. Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428310 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi Again, ha aha ha found it i think, just testing now #disable_functions = symlink,shell_exec,exec,proc_close,proc_open,popen,system,dl,passthru,escapeshellarg,escapeshellcmd it seems to work now i found that but doesnt seem to parse it right http://database.j2barnightclub.co.uk/getlog.php the log file looks like this when created.... <-------------- New Campaign Check Start [2008-01-02 00:56] -------------> [2008-01-02 00:56] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:56] -------------> <-------------- New Campaign Check Start [2008-01-02 00:57] -------------> [2008-01-02 00:57] QUERY: SELECT * from sms_delayed_campaigns where approved='Y' and processed='N' and processing='0' and sms_type='delayed' LIMIT 1 [2008-01-02 00:57] DATE AND TIME: Sending Date and Time is 2008-01-02 & 00:45 [2008-01-02 00:57] CURRENT TIME AND DATE: Current time is 00:57 and current date is 2008-01-02 [2008-01-02 00:57] QUERY: SELECT * from sms_delayed_campaigns where '2008-01-02' <= '2008-01-02' and '00:45' <= '00:57' and approved= 'Y' and processed= 'N' [2008-01-02 00:57] SUCCESS: txt2799S database created --> Sending to 07725307138 CHARGE: SESSION ID: 9057780a67dd955d725d5dfe92df890a [2008-01-02 00:57] SUCCESS ID: 85870a239407b8067809e3e6f146b539 [2008-01-02 00:57] PROCESSING SMS: Updated Processing field in sms_delayed_campaigns [2008-01-02 00:57] FROM: Club [2008-01-02 00:57] TITLE: Error Log testing [2008-01-02 00:57] MESSAGE: Error Log testing [2008-01-02 00:57] QUERY: SELECT * FROM members WHERE mobile_number = '07725307138' AND member_status= 'Full' and mobile_number = '' <-------------- New Campaign Check Start [2008-01-02 00:58] -------------> [2008-01-02 00:58] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:58] -------------> <-------------- New Campaign Check Start [2008-01-02 00:59] -------------> [2008-01-02 00:59] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:59] -------------> is there anything else i can do using shell_exec to parse it so it looks like it does when its imputted in to the log, also now ive enabled shell_exec am i under any security vulnerabilities because of that?? Alan thanks again for your replies Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428320 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 You'll want to use the html pre tag. eg; <?php echo '<pre>'; echo shell_exec('tail -n 50 /var/log/logfile.log'); echo '</pre>'; ?> Not if you make sure no outside (user) data can be directly fed into it. Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428322 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 ps: You may also not even need any ajax. If you simply refresh the page every 5 seconds or so using a meta tag. eg; <html> <head> <meta http-equiv="refresh" content="5"> </head> <body> <pre> <?php echo shell_exec('tail -n 50 /var/log/logfile.log'); ?> </pre> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428327 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi Thorpe, Thank you so much for your answers and code examples, those work perfecting ill persue the meta refresh for the time been but id also like to try and find a truely real time part which reads the text file in real time as i think it would be great that the client can watch a campaign being sent Again your answers are much apreciated, thank you for your time Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428519 Share on other sites More sharing options...
GingerRobot Posted January 2, 2008 Share Posted January 2, 2008 For the real-time update, how about this: <html> <head> <script type="text/javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('There was a problem creating the XMLHttpRequest object'); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest() { // Open PHP script for requests http.open('get', 'ajax.php'); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("log").innerHTML = response; setTimeout(update,1000); } } } function update() { sendRequest(); } </script> </head> <body onLoad="sendRequest()" /> <pre> <span id="log" name="log"></span> </pre> </body> </html> Then use this as ajax.php: <?php echo shell_exec('tail -n 50 /var/log/logfile.log'); ?> This will update the page every second without a full page refresh. Alter the time, which is given in milliseconds, in the setTimeout function if you need to. Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428533 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi There, looks cool and certainly looks like it whats i need but maybe im mising something here how do i call it, i cant see a link between ajax.php etc and the code u posted at the bottom sorry for being dumb here very new to ajax etc... Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428562 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 how silly of me ive seen it now, jesus lol Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428565 Share on other sites More sharing options...
GingerRobot Posted January 2, 2008 Share Posted January 2, 2008 Haha, yeah - the initial call is made by the javascript function called in the body onLoad attribute. Have a read up over on www.ajaxfreaks.com/tutorials if you're interested. It's where i took the javscript from. Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428570 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 HI there, wow its actually doing it but i have one last error, the <pre> tags dont seem to be parsing it properly, its all just on one line?? any ideas Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428579 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 hmmmm, it was working but its just stopped, not sure if i have made a boo boo here some where used ure code as u said etc but its outputting all on one line and i could of swore it updated but now its stuck on 21:11 http://database.j2barnightclub.co.uk/logs/getlogs.php the pre tags dont seeem to be doign there job there is something that confuses me where u have put log in this exmaple, does that stay as log? or does that chnage to my logname or the full path??? // UPDATE ajaxTest content document.getElementById("log").innerHTML = response; setTimeout(update,1000); } } } function update() { sendRequest(); } </script> </head> <body onLoad="sendRequest()" /> <pre> <span id="log" name="email-delayed.txt"></span> Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428604 Share on other sites More sharing options...
GingerRobot Posted January 2, 2008 Share Posted January 2, 2008 No, it should all be left as "log" - its just the id of the span tag, which is needed to allow its inner HTML to be updated. Not entirely sure why its stopped working. When i visit the link you gave, no content appears to be being retrieved. I notice you've changed the page we make a request to, to email-delayed.php. I assume this still contains the same thing as ajax.php? And im unsure as to why the newlines are not being preserved. The pre tags should keep them. p.s. When you post code, can you put it inside of tags (without the spaces) Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428625 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Ahhh yes sorry about that, ill do that next time, basically i have the following directory which is logs.. in here is email-delayed.txt ( the log file ) also email-delayed.php ( the tail line php code ) getlogs.php ( the Ajax code ) This is the getlogs.php <html> <head> <script type="text/javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('There was a problem creating the XMLHttpRequest object'); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest() { // Open PHP script for requests http.open('get', 'email-delayed.php'); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("log").innerHTML = response; setTimeout(update,1000); } } } function update() { sendRequest(); } </script> </head> <body onLoad="sendRequest()" /> <pre> <span id="log" name="log"></span> </pre> </body> </html> here is the email-delayed.php <?php echo shell_exec('tail -n /home/httpd/vhosts/domain/subdomains/database/httpdocs/logs/email-delayed.txt'); ?> and here is the log file <-------------- New Campaign Check Start [2008-01-02 00:02] -------------> [2008-01-02 00:02] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:02] -------------> <-------------- New Campaign Check Start [2008-01-02 00:03] -------------> [2008-01-02 00:03] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:03] -------------> <-------------- New Campaign Check Start [2008-01-02 00:04] -------------> [2008-01-02 00:04] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:04] -------------> <-------------- New Campaign Check Start [2008-01-02 00:05] -------------> [2008-01-02 00:05] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:05] -------------> <-------------- New Campaign Check Start [2008-01-02 00:06] -------------> [2008-01-02 00:06] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:06] -------------> <-------------- New Campaign Check Start [2008-01-02 00:07] -------------> [2008-01-02 00:07] ERROR: There are no campaigns awaiting sending! <-------------- Campaign Ended In Nothing To Send Error [2008-01-02 00:07] -------------> cant really see anything a miss just checking over everything now Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428657 Share on other sites More sharing options...
GingerRobot Posted January 2, 2008 Share Posted January 2, 2008 Well it looks like there's an issue with the call to the shell_exec() function, since email-delayed.php doesn't display anything. Im afraid im not too hot on that sort of thing, so perhaps someone else could suggest why it's not working. I assume it's not something stupid like a mistyped path? Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428664 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hmmmm, there soething not right i agree, but email-delayed.php is working and outputting now as is getlogs.php its not updating even when i control and F5 refresh it weird Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428678 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi Again, wel its parsed now and working right in terms of lay out but its not updating im watching the tail -f in shell and even a ctrl and F5 aint shifting it. wonder if ive missed something in the ajax code?? Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428690 Share on other sites More sharing options...
GingerRobot Posted January 2, 2008 Share Posted January 2, 2008 It appears to be working perfectly for me, updating too. The 22:08 entry just came on screen. Edit: In Firefox and IE Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428694 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 are you having to refresh or anything like that? ill send an eflyer now and see if you get the updates through tail -f on the shell is on 22.12 though :-) Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428697 Share on other sites More sharing options...
cr-ispinternet Posted January 2, 2008 Author Share Posted January 2, 2008 Hi there, works brill in firefox, just doesnt in IE7 maybe thats just the problem ehh must be a modification i can do to make it work with IE7 Alan Quote Link to comment https://forums.phpfreaks.com/topic/84121-solved-tail-f-style-window-using-php-and-probably-javascript-or-ajax/#findComment-428708 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.