-1

My script:

<?php //header("refresh: 7;"); include 'theme.php'; ceklogin(); css(); echo '<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script> $(document).ready( function() { setInterval(function() { var randomnumber = Math.floor(Math.random() * 100); $("#show").text( "I am getting refreshed every 3 seconds..! Random Number ==> " + randomnumber); }, 3000); }); </script>'; exec('/www/openvpn_wget_status.sh'); echo '<br>'; echo file_get_contents( "/www/openvpn_status.txt" ); echo '<div id="show">'; //include 'wget.log'; //echo "<br><textarea name=\"text-info\" id=\"scores\" rows=\"30\" cols=\"90\" readonly style=\"font-family: Arial;font-size: 7pt;\" >"; //$datalines = file ("wget.log"); //foreach ($datalines as $zz) { //echo $zz; } echo '</div>'; //echo "</textarea><br></div>"; echo '<script type="text/javascript"> var textarea = document.getElementById("scores"); textarea.scrollTop = textarea.scrollHeight; </script>'; foot(); echo ' </div> </body> </div> </html>'; ?> 

it works perfectly but what if I want the variable from a log file in my webserver, let's say the name of the log file is wget.log and I want to refresh it every 3 seconds since wget.log keeps changing as I run wget to download files?

8
  • Don't you need ajax for that? Commented Jan 9, 2015 at 11:37
  • I don't know but it already works fine Commented Jan 9, 2015 at 11:38
  • You need to call ajax and read file from server, echo it using php and print on browser using javascript. Make ajax every 3 seconds. Commented Jan 9, 2015 at 11:39
  • maybe this thread can help you topic tail of logfiles with php stackoverflow.com/questions/1102229/… Commented Jan 9, 2015 at 11:40
  • @RaviDhoriya can you please write the script for me ? I know next to nothing about ajax. Sorry. Commented Jan 9, 2015 at 11:41

1 Answer 1

0

Script to read file (readFile.php)

<?php //in this script, read any file(or do something else) //whatever you output, it will be processed in ajax request from below script echo "<pre>".file_get_contents( "/www/openvpn_status.txt" )."</pre>"; //used <pre> to keep linebreaks in file. ?> 

HTML (another file)

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script> $(document).ready( function() { setInterval(function() { $("#show").load("readFile.php"); //it will make ajax call to readFile.php and get output from that script and load to #show div }, 3000); }); </script> <div id="show"> This will be refreshed every 3 seconds. </div> 
Sign up to request clarification or add additional context in comments.

5 Comments

Wow. Never seen or used that type of ajax call ever. What request type does it use? POST or GET?
@Ravi Dhoriya my log file consists of line breaks and the line breaks are ignored once it's loaded on my webpage, how do I keep the line breaks?
@KrisztiánDudás lol on you. You should have look at this. It uses GET btw
@hillz then print it inside <pre></pre>.
Use the <pre> tag if you want to keep the formatting (linebreaks)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.