Jump to content

[SOLVED] Tail -f Style window using php and probably javascript or Ajax


cr-ispinternet

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

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.