PHP TAIL LOG: Send text field to js then to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gabrielpc1190
Forum Newbie
Posts: 5
Joined: Wed Oct 15, 2014 8:38 am

PHP TAIL LOG: Send text field to js then to PHP

Post by gabrielpc1190 »

Hi.
I am working on modifying this AJAX Logfile Tailer & Viewerhttp://commavee.com/2007/04/13/ajax-log ... er-viewer/.
Working Example: https://freepository.com/ajax-logtail-v ... viewer.php
I want to add an input tex field where I can write the log file name that I want it to read.

Code: Select all

<input type="text" id="filename"></input>
In the html file there is a button that starts the viewer, the button calls a javascript function:
This is the button code:

Code: Select all

<button onclick="getLog('start');">Start Viewer</button>
This is the javascript file that has the function the button calls:

Code: Select all

/* ajax utilit function from b. mclaughlin */
function createRequest() {
 var request = null;
  try {
   request = new XMLHttpRequest();
  } catch (trymicrosoft) {
   try {
     request = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (othermicrosoft) {
     try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (failed) {
       request = null;
     }
   }
 }

 if (request == null) {
   alert("Error creating request object!");
 } else {
   return request;
 }
}

var request = createRequest();

/* an ajax log file tailer / viewer
copyright 2007 john minnihan.
http://freepository.com
Released under these terms
1. This script, associated functions and HTML code ("the code") may be used by you ("the recipient") for any purpose.
2. This code may be modified in any way deemed useful by the recipient.
3. This code may be used in derivative works of any kind, anywhere, by the recipient.
4. Your use of the code indicates your acceptance of these terms.
5. This notice must be kept intact with any use of the code to provide attribution.
*/

function getLog(timer) {

var url = "ajax-logtail.php";
 request.open("GET", url, true);
 request.onreadystatechange = updatePage;
 request.send(null);
 startTail(timer);
}

function startTail(timer) {
 if (timer == "stop") {
 stopTail();
 } else {
  t= setTimeout("getLog(10)",1000);
 }
}

function stopTail() {
 clearTimeout(t);
 var pause = "The log viewer has been paused. To begin viewing again, click the Start Viewer button.\n";
 logDiv = document.getElementById("log");
 var newNode=document.createTextNode(pause);
 logDiv.replaceChild(newNode,logDiv.childNodes[0]);
}

function updatePage() {
 if (request.readyState == 4) {
  if (request.status == 200) {
   var currentLogValue = request.responseText.split("\n");
   eval(currentLogValue);
   logDiv = document.getElementById("log");
   var logLine = ' ';
   for (i=0; i < currentLogValue.length - 1; i++) {
    logLine += currentLogValue[i] + "<br/>\n";
   }
   logDiv.innerHTML=logLine;
  } else {
    alert("Error! Request status is " + request.status);
  }
 }
}
I modified the button that calls the function so it takes the text field value and passes it to the javascript getLog() function this way:

Code: Select all

<button onclick="getLog('start',document.getElementById("filename").value);">Start Viewer</button>
Then modified the function so it takes the second parameter:

Code: Select all

function getLog(timer,filename) {

var url = "ajax-logtail.php?"+"filename="+filename;
 request.open("GET", url, true);
 request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 request.onreadystatechange = updatePage;
 request.send(null);
 startTail(timer);
}
Now, that function calls the ajax-logtail.php file that has this content:

Code: Select all

<? // logtail.php
$cmd = "tail -10 some.log";
exec("$cmd 2>&1", $output);
foreach($output as $outputline) {
 echo ("$outputline\n");
}
?>
Because I wanted this php file to get the text field value in the html I modified it this way:

Code: Select all

<?
if(isset($_POST)){
	$cmd = "tail -10 " . $_POST['filename'];
}
elseif (isset($_GET)){
	$cmd = "tail -10 " . $_GET['filename'];
}
else {
	$cmd = "tail -10 logfile.log";
}
exec("$cmd 2>&1", $output);
foreach($output as $outputline) {
	echo ("$outputline\n");
}
?>
Then, after all that modified fields Im unable to pass the field text value to the ajax-logtail.php.
When I execute it chrome says:
[text]Uncaught SyntaxError: Unexpected token } , line 16 of file logtail.php[/text]
This is the contend of that line and some others before it:

Code: Select all

<div>
<button onclick="getLog('start',document.getElementById("filename").value);">Start Viewer</button>
<button onclick="stopTail();">Stop Viewer</button>
</div>
Can anyone help me identify the problem?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP TAIL LOG: Send text field to js then to PHP

Post by Christopher »

Not sure why you are getting that error message from looking at the code. Which code is giving that error message?

Also, these lines of code is incredibly hackable:[syntax] $cmd = "tail -10 " . $_POST['filename'];
$cmd = "tail -10 " . $_GET['filename'];
[/hackable]
Filter and validate all input.
(#10850)
gabrielpc1190
Forum Newbie
Posts: 5
Joined: Wed Oct 15, 2014 8:38 am

Re: PHP TAIL LOG: Send text field to js then to PHP

Post by gabrielpc1190 »

Christopher wrote:Not sure why you are getting that error message from looking at the code. Which code is giving that error message?

Also, these lines of code is incredibly hackable:[syntax] $cmd = "tail -10 " . $_POST['filename'];
$cmd = "tail -10 " . $_GET['filename'];
[/hackable]
Filter and validate all input.
In Chrome, at developer tools.
Can you suggest me a better way less hackable? I'll appreciate it.

Still no luck with this.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP TAIL LOG: Send text field to js then to PHP

Post by Christopher »

gabrielpc1190 wrote: In Chrome, at developer tools.
Can you suggest me a better way less hackable? I'll appreciate it.

Still no luck with this.
No, I mean which PHP code is giving you this problem. It says:[syntax]Uncaught SyntaxError: Unexpected token } , line 16 of file logtail.php[/syntax]Post the code to logtail.php. Have you called that PHP file directly to see what the problem is?
(#10850)
Post Reply