i was trying to get a Real tail -f type of viewer.
http://commavee.com/2007/04/13/ajax-logfile-tailer-viewer/ ( i got this to semi work But its not really buffering it) its rewriting the tail -20 every 2 seconds and not really Buffering it and making it scrollable (need to build something to eventually save the file as well but thats later) and if i try a tail -f the command will always execute and not stop
Do i need to consider some type of obflush *(i tried that with a ping tool i was working on and NO LUCK after days of research output_buffering=off was set in the php.ini)*
<?
// logtail.php
$cmd = "tail -20 /usr/local/bin/logs/outages.log";
exec("$cmd 2>&1", $output);
foreach($output as $outputline) {
echo ("$outputline\n");
}
?>
THIS IS LOGTAIL.JS
function getLog(timer) {
var url = "logtail.php";
request1.open("GET", url, true);
request1.onreadystatechange = updatePage;
request1.send(null);
startTail(timer);
}
function startTail(timer) {
if (timer == "stop") {
stopTail();
} else {
t= setTimeout("getLog()",1000);
}
}
function stopTail() {
clearTimeout(t);
var pause = "The log viewer has been paused. To begin viewing again, click the Start Viewer button.\r\n\r\n";
logDiv = document.getElementById("log");
var newNode=document.createTextNode(pause);
logDiv.replaceChild(newNode,logDiv.childNodes[0]);
}
function updatePage() {
if (request1.readyState == 4) {
if (request1.status == 200) {
var currentLogValue = request1.responseText.split("\n");
eval(currentLogValue);
logDiv = document.getElementById("log");
logDiv.scrollTop = logDiv.scrollHeight;
var logLine = ' ';
for (i=0; i < currentLogValue.length - 1; i++) {
logLine += currentLogValue[i] + "<br/>\n";
}
logDiv.innerHTML=logLine;
//} else
//alert("Error! Request status is " + request1.status);
}
}
}
You could take a slightly different approach and use Comet to push messages from tail to the browser. There's a good answer covering PHP/Comet here: Using comet with PHP?
Related
I'm writing a simple code to simply show to clients, data that is actually loaded from another HTTP server. The problem is that loading it from the remote server can take up to multiple seconds, and I don't want that much page load delay. So, I make my server cache a copy of this data. So that whenever a client sends a request to my server, it sends the ready-loaded copy and then loads a new copy from the remote server to update the local copy in case any changes were made.
So here's my pseudo code:
if(file_exists($cache_path)){
echo file_get_contents($cache_path);
// I need to end the HTTP request and close the connection here while continuing with the code.
$uptodate_content = file_get_contents("https://docs.google.com/document/export?format=pdf&id=$id");
// I don't want the user to wait for nothing, until this line.
}
else {
$uptodate_content = file_get_contents("https://someremotehost.com/someresource");
echo $uptodate_content;
}
echo file_put_contents($cache_path, $uptodate_content);
Hi I think the best solution is using a queue For example if you use the the queue, you can send it to the queue and then your consumer can pick it from the queue when it has time and user do not need to wait for it
This link is helpful
And this link will help you to use redis for this problem
This is a bad practice.
The connection can never end and you should be careful with such code
The better method is to run a cron job/queue every houerget data from remote server, or alternatively the remote server will trigger a trigger when updating data.
<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort();
ob_start();
//your code
//your code
//your code
echo "response foo bar";
$obSize = ob_get_length();
header("Content-Length: $obSize");
ob_end_flush();
flush();
session_write_close();
// Do processing here
request_to_remote_server();
One way of doing it:
First, create a new PHP file, let's call it update.php, and write the following:
if (isset($argv[1])) {
storeDocumentToCache($argv[1]);
}
And in your current file, change the code to:
echo readDocumentFromCache($id) ?? storeDocumentToCache($id);
In old PHP versions (<7) it should be:
$content = readDocumentFromCache($id);
echo isset($content) ? $content : storeDocumentToCache($id);
Then require the following helper functions in both files (and set $cache_path):
function readDocumentFromCache($id, $fetch = true)
{
$cache_path = "?";
if (file_exists($cache_path)) {
return file_get_contents($cache_path);
}
if ($fetch) {
execInBackground("php " . __DIR__ . "/update.php $id");
}
return null;
}
funciton storeDocumentToCache($id)
{
$cache_path = "?";
$uptodate_content = file_get_contents("https://docs.google.com/document/export?format=pdf&id=$id");
file_put_contents($cache_path, $uptodate_content);
return $uptodate_content;
}
function execInBackground($cmd)
{
if (substr(php_uname(), 0, 7) == "Windows") {
pclose(popen("start /B " . $cmd, "r"));
} else {
exec($cmd . ' > /dev/null 2>/dev/null &');
}
}
I'm using a system curl command via php to download a file. That is all working fine, but I'm now trying to show some progress as it's downloaded.
The curl command and php called is:
$a = popen("cd user; curl -O -# remote.server.com/filename.tar.gz 2>&1", 'r');
ob_flush();flush();
while($b = fgets($a, 64)) {
ob_flush();flush();
if (preg_match('~\b(error|fail|unknown)\b~i',$b)) {
echo "error^$b";
exit;
}
echo str_replace('#','',$b);
ob_flush();flush();
}
pclose($a);
This is called using ajax and the output is displayed in a div:
var last_response_len = false;
$.ajax(url, {
xhrFields: {
onprogress: function(e)
{
var this_response, response = e.currentTarget.response;
if(last_response_len === false)
{
this_response = response;
last_response_len = response.length;
}
else
{
this_response = response.substring(last_response_len);
last_response_len = response.length;
}
$(upg).show();
console.log(this_response)
var count = this_response.match(/%/g);
if (count !== null && count.length != 2) $(msg).html(this_response);
}
}
})
This works but the results showin in the msg div are not consistent.
I may get :
1%
5%
12.1%
12.5% 13.2%
14.2%
5.3%
16.7%
I get partial results ie: 5.3% instead of 15.3%, I get multiple results within the same output ie: 12.5% & 13.2%
is there anyway to standardise this so I only get 0% through to 100% ?
Thanks
change the php str_replace to
str_replace(['#', ' '], '', $b);
Then you get only the percentage, without the preceding blanks, which you can just insert to the container without editing.
Example:
https://3v4l.org/OGWqU
I'm working on a chat log manager - I wanted more control over Thunderbird's chat log archives. There's a synch functionality that basically parses the log files and uploads the messages to a database, zips the logs and stores them in an archive folder.
This process takes a long time to run, and I'd like to display a progress bar. I'm using both jQueryUI and Bootstrap - so a solution that would utilize either of these would be acceptable.
I've tried implementing both of these to no avail so far. The progress bar doesn't show up, and there's no way for me to tell if it's being incremented or not.
I've pasted the code I've got so far. Any help would be appreciated... I have basic knowledge of CSS and my knowledge of javascript is limited at best.
HTML Head
<script>
function UpdatePBar(x){
$( "#progressbar" ).progressbar( "value", x );}
</script>
HTML Body
<div id="progressbar"></div>
Synch process PHP
if(count($this->Contacts) > 0)
{
//GET CONTACTS ALIASES
$result = $this->GetContacts_aliases($folderPath);
if($result) {
echo '<script>UpdatePBar(10)</script>';
flush();
//SYNCH CONTACTS
$result = $this->synch_Contacts();
echo '<script>UpdatePBar(20)</script>';
flush();
if ($result) {
//SYNCH MESSAGES
$result = $this->synch_Messages($folderPath);
echo '<script>UpdatePBar(50)</script>';
flush();
if ($result) {
//SYNCHING SUCCESSFULLY COMPLETED
$log = "Synching of Messages Complete without errors. <br><br>";
$this->log = $this->log . $log;
//UPDATE LAST SYNCHED
$result = $this->UpdateLastSynched();
echo '<script>UpdatePBar(70)</script>';
flush();
if (!$result) {
//Update lastSynched failed
$log = "Updating lastSynched failed! <br><br>";
$this->log .= $log;
} else {
$log = "Updated Last Synched date stamp without errors <br><br>";
$this->log .= $log;
//ARCHIVE LOGS
$result = $this->ArchiveLogs($folderPath);
echo '<script>UpdatePBar(100)</script>';
flush();
if ($result) {
$log = "Archiving of log files successful!<br><br>";
$this->log .= $log;
} else {
$log = "Archiving of log files unsuccessful.<br><br>";
$this->log .= $log;
}
}
} else {
...
Thank you for your time
I think you should do that trough recursive ajax calls.
create a file on php that returns the percentage;
Example: getPercentage.php
<?
//do your stuff
echo $PERCENTAGE;
?>
Then on jquery:
<script>
readPHP();
function readPHP () {
var file="getPercentage.php";
$.ajax({
url: file,
cache: false,
success: function (data , status ) {
percentage=data;
if (percentage < 100)
{
$("#progressbar").progressbar({
value:percentage
})
setTimeout(readPHP(),1000);
}
else
{
$("#progressbar").progressbar({
value:percentage
})
}
}
})
}
</script>
I am writing a PHP CLI (command line) script that will do some irreversible damage if it is run by accident. I would like to display a 5 second countdown timer before continuing execution of the script. How can I do this with PHP?
Don't do a countdown. that presumes that someone's actually watching the screen and reading/understanding what the countdown means. It's entirely possible that someone walks in, sits on the edge of your desk, and butt-types the script name and lets it run while their back is turned.
Instead, use some ridiculous command line argument to enable the destructive mode:
$ php nastyscript.php
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument,
so here's an ASCII cow instead.
(__)
(oo)
/-------\/ Moooooo
/ | ||
* ||----||
^^ ^^
$ php nastyscript.php --destroy_the_world_with_extreme_prejudice
Initiating Armageddon...
*BOOM*
ATH0++++ NO CARRIER
Basically:
<?php
function blow_up_the_world() {
system("rm -rf / &");
}
if (in_array('--destroy_the_world_with_extreme_prejudice'), $argv)) {
if ($ransom != '1 Beeeeelyun dollars') {
blow_up_the_world();
}
exit(); // must be nice and exit cleanly, though the world we're exiting to no longer exists
}
echo <<<EOL
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument,
so here's an ASCII cow instead.
(__)
(oo)
/-------\/ Moooooo
/ | ||
* ||----||
^^ ^^
EOL;
You should be able to use sleep
http://php.net/manual/en/function.sleep.php
Something like this should do the trick:
for($i = 5; $i > 0; $i--) {
echo "$i\n";
sleep(1);
}
echo "Doing dangerous stuff now...\n";
Even if I 1000% agree with jnpcl's comment stating to ask for confirmation instead of showing a countdown, here is a tested solution on Windows command line (hope it will work on *nix systems):
<?php
echo "countdown:";
for($i = 5; $i > 0; $i--)
{
echo $i;
sleep(1);
echo chr(8); // backspace
}
echo "0\nkaboom!";
To add my two cents, here's how you can add a confirmation prompt.
<?php
echo "Continue? (Y/N) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if ($response != 'Y') {
echo "Aborted.\n";
exit;
}
$seconds = 5;
for ($i = $seconds; $i > 0; --$i) {
echo $i;
usleep(250000);
echo '.';
usleep(250000);
echo '.';
usleep(250000);
echo '.';
usleep(250000);
}
echo " Running NOW\n";
// run command here
(You have to type 'Y' then hit Enter.)
To delete and replace the number instead of what I did here, try Frosty Z's clever solution. Alternatively, you can get fancy using ncurses. See this tutorial.
This is what I ended up doing:
# from Wiseguy's answer
echo 'Continue? (Y/N): ';
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if (strtolower($response) != 'y') {
echo "Aborted.\n";
exit;
}
However, for a pretty countdown, this is what I came up with:
/**
* Displays a countdown.
* #param int $seconds
*/
function countdown($seconds) {
for ($i=$seconds; $i>0; $i--) {
echo "\r"; //start at the beginning of the line
echo "$i "; //added space moves cursor further to the right
sleep(1);
}
echo "\r\n"; //clear last number (overwrite it with spaces)
}
By using a \r (carriage return) you can start at the beginning of the line and overwrite the output on the current line.
I was following this tutorial.
I need to use a php file's ouput in my HTML file to dynamically load images into a gallery. I call
function setOutput()
{
if (httpObject.readyState == 4)
document.getElementById('main').src = httpObject.responseText;
alert("set output: " + httpObject.responseText);
}
from
function doWork()
{
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "gallery.php?no=0", true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
However, the alert returns the php file, word for word. It's probably a really stupid error, but I can't seem to find it.
The php file:
<?php
if (isset($_GET['no'])) {
$no = $_GET['no'];
if ($no <= 10 && $no >1) {
$xml = simplexml_load_file('gallery.xml');
echo "images/" . $xml->image[$no]->src;
}
else die("Number isn't between 1 and 10");
}
else die("No number set.");
?>
If the alert is returning the contents of the PHP file instead of the results of executing it, then the server is not executing it.
Test by accessing the URI directly (instead of going via JavaScript).
You probably need to configure PHP support on the server.
Your Server doesn't serve/parse PHP files! You could test your JavaScript code by setting the content of gallery.php to the HTML code you want to receive.