ob_get_contents NO contents on server - php

I got a very strange problem.
Today I tried to implement caching into my PHP application.
It works like a charm on my localhost WAMP server (Windows 8).
But it does not work online.
Hence I do not have a clue what I am doing wrong.
The code is somewhat like:
<?php
function write_cache(){
$contents = ob_get_contents();
/// do something with contenst (like writing it..
}
$tpl_content = 'loooooong string'; // gets filled throughout the application
echo $tpl_content;
/// should be filling the cache
write_cache();
?>
This should work. I echo it hence it is in the buffer.
And I am somewhere doing it correctly because locally it is working.
But online it remains empty..
Does anyone know what I am doing wrong??
Thanks in advance!!

#faintsignal
You where right!! ob_start(); was missing.
It is probably not needed on a WAMP server.
But on a LAMP server it is needed.
The code now looks like:
<?php
ob_start();
function write_cache(){
$contents = ob_get_contents();
/// do something with contenst (like writing it..
}
$tpl_content = 'loooooong string'; // gets filled throughout the application
echo $tpl_content;
/// should be filling the cache
write_cache();
?>
And it works!!
Thanks!!!!

Related

PHP - Alternative to shell_exec

I am using a PHP script on a local machine connected to an offline server. That server has a webpage on it that processes some homebrewed scripts. This all works fine. However, my setup currently looks something like this
<?php
echo shell_exec(./script1)
echo('Script 1 Done!' .PHP_EOL);
echo shell_exec(./script2)
echo('Script 2 Done!' .PHP_EOL);
echo shell_exec(./script3)
echo('Script 3 Done!' .PHP_EOL);
echo('All Done! .PHP_EOL);
?>
This is fine and works. However, each of these scripts have a ton of output. At seemingly random, arbitrary points in my code, the webpage refreshes and shows that output on a white background. I'm fine with this, except for the random, arbitrary points.
Is it possible to get this to do that in real time? I'm not even sure what to Google for this issue as nothing I've tried has seemed related.
Just capture the output in an array and then you can control when you want to output if you decide to at all.
<?php
$shellOutput = [];
$shellOutput[] = shell_exec('./script1');
$shellOutput[] = shell_exec('./script2');
$shellOutput[] = shell_exec('./script3');
//then when you want to output a simple foreach will do the trick.
foreach($shellOutput as $output){
echo $output;
}
?>

Progress bar not working flush

I got big problem, becouse i tested everything to make it works, but`s not - yet :)
i got simple for loop and there is a star, end, flush inside, but still my browser load all output at the and of loop, and i took for this question simple example:
<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++){
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
ob_flush();
flush();
sleep(2);
}
echo "Done.";
ob_end_flush();
?>
a i`vd setup all about outpuuting_bufforing, zlib, gzib, and other alls. Exacly in htacces, script, file, even in php.ini, apache. I got dedicaded server so can configure what i need. Can some1 tell me what more can i try?
Ofc there is no error in any log file.
Thanks for advice !
The comments in the official PHP documentation for ob_flush() mention, that most browsers have an all-or-nothing approach to loading content. Therefore the browser will not show anything until the whole page is loaded.
See http://php.net/manual/de/function.ob-flush.php#109699
This means that flushing output to the browser will not work for you.
The alternative would be to start the initial request via AJAX and then use a second request to provide information about the current progess.

PHP - Saving array to file - Not stacking up

Really stumped on this one and feel like an idiot! I have a small PHP cron job that does it's thing every few minutes. The client has requested that the app emails them with a daily overview of issues raised....
To do this, I decided to dump an array to a file for storage purposes. I decided against a SQL DB to keep this standalone and lightweight.
What I want to do is open said file, add to a set of numbers and save again.
I have tried this with SimpleXML and serialize/file_put_contents.
The issue I have is what is written to file does not correspond with the array being echo'd the line before. Say I'm adding 2 to the total, the physical file has added 4.
The following is ugly and just a snippet:
echo "count = ".count($result);"<br/>";
$arr = loadLog();
dumpArray($arr, "Pre Load");
$arr0['count'] = $arr['count']+(count($result));
echo "test ".$arr0['count'];
dumpArray($arr0, "Pre Save");
saveLog($arr0);
sleep(3);
$arr1 = loadLog();
dumpArray($arr1, "Post Save");
function saveLog($arr){
$content = serialize($arr);
var_dump($content);
file_put_contents(STATUS_SOURCE, $content);
}
function loadLog(){
$content = unserialize(file_get_contents(STATUS_SOURCE));
return $content;
}
function dumpArray($array, $title = false){
echo "<p><h1>".$title."</h1><pre>";
var_dump($array);
echo "</pre></p>";
}
Output View here
Output File: a:1:{s:5:"count";i:96;}
I really appreciate any heads up - Have had someone else look who also scratched his head.
Check .htaccess isn't sending 404 errors to the same script. Chrome was looking for favicon.ico which did not exist. This caused the script to execute a second time.

Display script result first on browser then run include file in php

I have a script that shows a result done then i call an include file to run at intervals. I would like the script result to display on browser then include file will run in the back. Right now my browser is connecting but showing nothing.I would like the browser echo Done and Logging.
<?php
ob_implicit_flush(true);
require_once('syslog.php');
$syslog = new Syslog();
$line="My msg";
$hostname = gethostname();
$ip= #$REMOTE_ADDR;
$hostnameip = GetHostByName($ip);
$syslog->Send('127.0.9.1', $hostname." ".$hostnameip." ".$line);
echo"Done";
echo "Logging......";
ob_end_flush();
include('execute.php');
?>
I believe you'll be able to get what you want by calling the flush() method right after your echo commands.
The flush() function is described as -
Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.
There are some special considerations when using this function. Certain apache modules and client side buffering will still be enforced, but for now however, I do believe this will help.
Try flush() before include after last echo.

How to echo in PHP while the script is still executing?

I developed a facebook application in PHP. The problem is that it takes 2 minutes to display the result. This might confuse the user, who sees a blank canvas and leaves.
I just want to echo a statement that it is still processing.
I tried flush(); and ob_flush(); and ob_start(); but it is of no use.
Is there any other simpler alternative to address my specific problem?
I tried this, but it did not work as well.
ob_implicit_flush(true);
ob_end_flush();
for ($i=0; $i<5; $i++) {
echo $i.'<br>';
sleep(1);
}
EDIT:
The above code works perfectly fine with IE and other Browsers.
Only Chrome has this issue.
Convert it to an AJAX request, where you load a quick page which can have anything you want, and then loads in data from the slower page in the background.
flush() won't do what you want because it will return only part of the output and the client will tend to wait for the complete page.
Call flush(); as often as required.
Unfortunately, this might or might not make the browser feel happy to display your stuff. Even on IE, the result isn't predictable.

Categories