Hey, well I don't really know how to describe this but here's my current situation.
On my old host, I could have a loop going and echo things in the loop. As the code was running that loop, the text i was echoing would appear right away and I could see things happening.
Now though, on my current host if I have a loop like that I only see the text that was being echoed once the loop is done and the script is fully done executing.
I was just wondering if anyone knows why?
Thanks!
edit: output buffering and output compression is off.
As others have said, it sounds like output buffering is on by default on your host.
You can turn it off programatically, using ob_end_flush() (which sends any buffered output), or ob_end_clean() (which will discard any output). Calling one of those functions early in your code (at the top of your script, or in some config file, for example) will get you at least part of the way there.
You may also be able to turn off output buffering in php.ini, or by setting php_value output_buffering Off in an .htaccess file, if you wanted to turn it off globally.
As Tobias P. points out, you can then use flush() to (try to) force whatever PHP is hooked up to (apache, CGI etc) to flush the output as well.
Do you use flush() ? If you do, maybe the server caches the response and sends it all at once, ask your webhoster
Output buffering my be on by default in the server config. As Tobias says, consult you webhost.
As has been stated, output buffering is likely enabled by the webhost in php.ini.
I believe you can inspect this yourself (though, it's possible for the webhost to disable access to the function):
echo ini_get('output_buffering');
Edit: some useful links
http://php.net/manual/en/outcontrol.configuration.php
http://php.net/manual/en/book.outcontrol.php
Related
I run a php script from xampp portable on windows. The script takes over a minute. Recently, the script is having flush() problem, as echo statements are not immediately displayed. The script used to work fine earlier with no buffering problem.
Interestingly, I ran the same script copying the xampp portable to another system and the flush statements were working without any problem. Same code, Same xampp portable.
What could be the reason?
Make the first line of your script ob_implicit_flush();.
or
Change the php.ini file setting implicit_flush = On.
From the documentation:
implicit_flush boolean, FALSE by default. Changing this to TRUE tells
PHP to tell the output layer to flush itself automatically after every
output block. This is equivalent to calling the PHP function flush()
after each and every call to print or echo and each and every HTML
block.
When using PHP within an web environment, turning this option on has
serious performance implications and is generally recommended for
debugging purposes only.
I read other responses and you keep insisting on the fact that your home environment and your work environment are the same. However, you can see that there is a difference. This point of view really help (at least to me) to investigate problems.
Since you did not provide many details about the problem, I would try the following checklist:
Settings
Is your PHP settings really identical? Try to compare results of phpinfo() on both environments.
Data
Do you really test your script on identical data? There are many subtle problems that are described in PHP manual:
Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the tag of the outermost table is seen.
Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.
http://php.net/manual/en/function.flush.php
or
http://www.php.net/manual/en/function.ob-flush.php#90529 (commenters point out many problems that you may experience)
Try dummy plaintext data instead of HTML. You may try to output simple lines like current time and check behaviour of your script.
Browsers
Try a few browsers (with cleared cache) to see if the issue is browser-specific or not.
I think it may be your browser. Have you cleaned the temporary settings of Iron Portable browser?
this occurs when you use gzip and there is some output have been send so the browser get confused to solve this , i uses this code always
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)
{ob_start('ob_gzhandler'); ob_start();}
else
ob_start();
You have not mentioned about the versions of windows both the systems running. Are the systems completely identical?
You also said that you are running a portable version of XAMPP , if you are using a pen drive/thump drive there is a chance that data transfer speeds can vary due to USB speeds.
I had a similar issue where the First system had USB legacy ports and the second one I tested has usb 2+ ports or higher.
The speed and processing time changed based on the systems, while one system took 20 seconds , other took nearly 60 seconds to process.
The slow system produced undesired results [I was working on an image processor].
I guess your case is similar and had to do a lot with the system vitals.
Cheers
Clain
I figured out that the problem is with antivirus. I recently switched to Bitdefender from Avast. When I switched back to Avast, the problem disappeared miraculously. So, i guess antivirus is also a factor here.
Does anybody know how can I debug to check where my session variable is setting in the code. There are lots on included files too. I tried site wide search for that session variable but unable to find any clue where it is being set.
May be anybody know any chrome or firefox extension that tell which variable setting where in the code. Just like firebug for javascript, where we can use debugger to check all the above possibilities.
The correct answer to your question is "Use a proper PHP debugger, like xDebug. This will allow you to do lots of stuff, including examining variables at any given point in the program, stepping through line by line, etc. Combined with a good quality IDE like Netbeans or Eclipse, it's an amazingly powerful tool.
However, if you really want a browser plugin, you could try either FirePHP or Chrome Logger.
Both of these will require you to put debugging code into your program that sends debug data to the browser, but the debug info appears in the dev tools, rather than messing up your rendered page output, so it's a lot cleaner to use than just using echo or print_r, etc to display the info.
Hope that helps.
$_SESSION PHP variables are set server-side; you won't be able to access them in the browser unless you output them there.
I would suggest placing several var_dump($_SESSION) statements in your PHP code to debug what key/value pairs are living in the session as your script executes.
At the point in your php script you wish to know what is stored do this:
echo "<pre>";
var_dump($_SESSION);
echo "</pre>";
exit;
You can use php debugger Like (XDebug or Zend Debuger) by adding watch command which will track all changes of that variable, you will need capable IDE for that though.
I want to share a problem I had, the fix I found and then ask a question about the reason behind the fix.
The problem
After upgrading to wampserver 2.2, one of my webpages consistently didn't work the first time it was loaded in the browser. This happened with internet explorer, chrome, firefox and safari. When reloaded the page worked in all browsers.
the fix
I decided to implement a better debugging solution and while doing so inadvertently fixed my problem. When I set output_buffering =On in php.ini the page worked correctly.
my code
I'm not going to go into detail here. I'm more interested in theory for how output_buffering could be causing problems. Also I think my code will be more of an eyesore than a help.
I used ajax and joomla sessions (external script) to retrieve
information for the page.
I believe that when output_buffering was off, the joomla session
was not able to retrieve values. I'm not able to confirm this yet
though.
My question
In what ways can output_buffering= Off adversely affect code? Why?
Output buffering simply allows you to hold off on displaying data that would otherwise be immediately printed to the browser. These are used largely with templating engines in order to store unrendered templates so that they can be filled in with values. I'm guessing Joomla depends on output buffering to fill in the correct values for its templates, which would explain why you were seeing invalid output.
"I used ajax and joomla sessions (external script) to retrieve information for the page."
That is your problem. You're retrieving a content that varies within a certain time delay.
Refer to this, it may help you understand how it works: https://stackoverflow.com/a/2832179/817419
As it turned out one of the files being called by the webpage was encoded incorrectly. Once I encoded it as UTF8 without BOM my problem was largely fixed. My script would work without output_buffering turned on.
The other part of the problem was that some of the scripts that used Firebug complained of headers already being sent. This stopped the code in its tracks.
i use file_get_contents function to grab data from sites and store the data in database. it will be very inconvenient for me, if one day the script will start not working.
I know, that it can start not working, if they change the structure of site, but now i'm afraid, that maybe there are mechanisms to disable the working of this function, maybe from server?
i tried to find documentation about it, but can't get, so maybe you will help me?
Thanks
I know, that it can start not working,
if they change the structure of site,
but now i'm afraid, that maybe there
are mechanisms to disable the working
of this function, maybe from server?
Yes, it can be disabled from php.ini with allow_url_fopen option. You have other options such as CURL extension too.
Note also that you will need to have openssl extension turned on from php.ini if you are going to use the file_get_contents function to read from a secure protocol.
So in case file_get_contents is/gets disabled, you can go for CURL extension.
It is possible to disable certain functions using disable_function. Furthermore the support of URLs with filesystem functions like file_get_contents can be disabled with allow_url_fopen. So chances are that file_get_contents might not work as expected one day.
There are at least two PHP configuration directives that can break your script :
If allow_url_fopen is disabled, then, file_get_contents() will not be able to fetch files that are not on the local disk
i.e. it will not be able to load remote pages via HTTP.
Note : I've seen that option disabled quite a few times
And, of course, with disable_functions, any PHP function can be disabled.
Chances are pretty low that file_get_contents() itself will ever get disabled...
But remote-file loading... Well, it might be wise to add an alternative loading mecanism to your script, that would use curl in case allow_url_fopen is disabled.
I noticed the other day that a new script I wrote for php 5 began outputting html that was viewable before the php script had actually finished. Did this happen with 4?
For instance, I have a long loop that echos something out with each iteration. The output was small in terms of kb, so I dont think it was lag due to the download speed. Can someone explain the difference in output?
Maybe there a difference in the configuration of the output_buffering directive, in php.ini ?
If output_buffering is enabled, PHP will "keep" the generated output in memory (at least, if it doesn't become bigger than the size of the memory buffer), and only send it to the browser when the page's generation is finished.
If output_buffering is disabled, the ouput is sent immediatly when generated, even if the script's execution is not finished yet.
I doubt there is a difference to this regard between PHP 4 and 5, but you can get this behaviour on both versions, namely by enabling/disabling the output_buffer. Maybe the default value for PHP 5 is different than it was for PHP 4? (Haven't checked)
When the data is sent, is dependant on PHP configuration, it's an output buffer, and behaves like a buffer.
Having said that, you can use the function ob_start() and ob_end_flush() to take control of the buffer. The Zend Framework does some clever stuff with output buffering for instance...
The usual suspects are:
Browser and HTML structure
Output buffering or output handlers
HTTP compression handled by PHP or by the web server
A close look at phpinfo() at a tool to see HTTP headers can help you.