PHP - Purpose Of Ob_start ?
Hey guys, what's the purpose of using ob_start() in the begining of the script? I even saw alot of people turning output buffering by default in their php.ini file ! What's the benefit of this action ?
Thanks in advance Similar TutorialsWhat is the purpose of using an Interface in OOP? I understand the textbook definition, but am not getting the extra value that they provide... TomTees In C, when you dynamically allocate memory, you have to free it, otherwise you face memory leaks:
char *str; str = (char *) malloc(10); free(str);But in most other languages you do not need to worry about this. PHP has a function called unset, which unsets the variable's value. But why would you want to do this, if PHP has a garbage collector that will free variables once they leave scope anyway? I see no point of unset in a scripting language with garbage collection. Can someone tell me what some of the purposes of this would be? global $PHP_SELF; its defined right after a function like this function myFunction(module =' '){ global $PHP_SELF; .... } Thanks This will be somewhat of a brain dump with somewhat vague questions. To summarize it right now, something about "refactoring software" has been bugging me. The length of the process, the work it takes, and the time, and the "no immediately visible results" that it produces.
There have been numerous questions on "do I refactor or rewrite". Depending on circumstances, one choice may work better than another, although historically for working projects (projects people are using continuously), refactoring seemed to do better, since it does not break things as much as rewriting does. And when things break all at once, such as when introducing a rewrite (which most likely has different behavior, UI, feel, workflow, etc. etc.), people tend to get upset. Well, refactoring seems to smooth out the 'upsetness' over a much longer period of time. People change as software changes, and each change is not as abrupt as with a rewrite. There are books written about refactoring software. That process takes time and work, `while not changing the functionality of the software`. Little improvements can be done here and there, but the main point becomes improving the internals of the software without affecting the outside behavior, the user experience. From the business end, *there are no changes!*. We have developers *doing busy work* without showing anything to the business, until perhaps much later. Is that bad? Where I work, I have business people scoff at "what have we been doing" quite regularly, and saying things like "add convertion from one unit system to another? Just put it in!" Which ends up me staring into vastness of legacy code and taking 2-3 weeks to "put it in", what in someone's mind takes a day at the most. But that's cuz maybe I'm slow, but I chose to refactor relevant code first, at least moving View items into respective view containers as a first step. What is the purpose of refactoring software? What, per se does it make better? What expectations can be set? What do I do next time someone says "it is taking too long", other than saying ... cuz I am refactoring! Which of these is the "best coding practice" for the proper order of "ob_start," "sessions," and "require?" (my "require" files are usually just mySQL login stuff):
a.)
b.) And also, instead of just plain "session_start()" isn't it more proper to ALWAYS use if(!isset($_SESSION)) { session_start(); } Thank you in advance.
I have just uploaded my 1st website project in my online server and the 1st error that I got is something like "Header ouput buffer" error. I have research on how to solve it and and I read that ob_start() will solve it. I have use it on the file that I'm having problem with and it instantly solve the problem. My question is... Is it always always ok to use it? I mean will it cause a slow page load or any conflicting errors that I might encounter in the future? another thing is... do I always need to end it? like put this end code "ob_end_flush(); " at the end of the line or it isn't necessary anymore? i enabled my output_buffering i used ob_start() and ob_end_flush functions i did'nt put any html tag before my php i used rapid php 2010 so dont know it making utf8 or not and i dont know how to check it i read many forums threads but cant find answer please answer but still i'm not redirecting to index.php from login.php here is my login.php <?php require_once("../../includes/database.php"); require_once("../../includes/functions.php"); require_once("../../includes/user.php"); require_once("../../includes/session.php"); if($session->is_logged_in()) { redirect_to(index.php); } //redirect_to("index.php"); if(isset($_POST['submit'])) //... if submit button is clicked { $username = trim($_POST['username']); $password = trim($_POST['password']); // check in database to see if username/password exist $found_user = User::authenticate($username,$password); // if we get some record in var $found_user if($found_user) { $session->login($found_user); // echo $found_user->username; // here is problem not redirecting redirect_to("index.php"); // this method is user define in function.php } else { //where i put header() $message = "Password/Username combination is not correct."; } } else { $username =""; $password =""; } ?> <html> <head>....... here is my function.php <?php function redirect_to( $location = NULL ) { if ($location != NULL) { header("Location: {$location}"); exit; } } function output_message($message="") { if (!empty($message)) { return "<p class=\"message\">{$message}</p>"; } else { return ""; } } ?> i'm attaching files too Hello, I am trying to change the string 'IRM Reporting Portal' to 'BIS Reporting Portal' as illustrated in the attachment 'Requirements.jpg'. On inspecting 'IRM Reporting Portal', I noticed the function DoResizeMS (). I found this function call in only one file across the entire code. That file is 'header.dsp' (attached) This leads me to believe that the string 'IRM Reporting Portal' is being dynamically generated in 'header.dsp' (Between ob_get_contents() and ob_start()) How do I change that string to 'BIS Reporting Portal'? Please help. I am a PHP newbie. Thanks, Trinanjan. Hi, this, i think, is a 'php in firefox' issue despite me mentioning css in the title so please read on!.. please have a look, using firefox, at http://www.laureleroch.com/gallery.php... As you can see, when choosing different images via clicking on the thumbnails the page reloads and there's an amount of time where we see the page loaded without css applied..., however doesn't seem to be happening in IE9, opera, chrome, or safari. It IS happening in firefox on W7 and OSX. the file gallery.php has... <?php session_start(); ob_start(); include("scripts/php/gallery_display.php"); include("scripts/php/file_reader.php"); ...as its start...and... </body> </html> <?php ob_flush() ?> ..at its end. I can post all the code from the includes if required. I'd say there's a fair bit of processing done by these includes but really, not THAT much, plus other browsers seem to be doing ok with it. Also, the background images are big. That's a fact. The site will be in this way and I plan to use a preloader once I get the main build sorted...nearly there. One other thing...I really am a coding newbie. I've been toying with it for years, first with actionscript in flash, then html/css and now I'm getting really into php, however I'm sure my coding is pretty inefficient here and there thru nothing other than pure, undiluted ignorance...good, eh?! so, if anyone got some pointers that would be really brilliant... any other info required - please shout. cheers, Scott Hello, I am an absolute PHP newbie, and I have something which is probably a very stupid question. Nevertheless, I am stuck, despite Google and reading the docs. Here a very cut down example code: Code: [Select] <?php ob_start(); echo "123\n"; $body = ob_get_flush(); echo "body = ".$body; ?> I would have expected the output to be body = 123 It actually is 123 body=123 ie ob_start does indeed buffer the output, but it does not prevent if from getting through. This happens both on my Mac with 5.3.10, and on my server where I dont know the PHP version (...newbie...) Any help would be appreciated! This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=355260.0 Hi there, I am trying to create a file using fwrite function, but for something reason all I get is a blank file with no content inside. Here is the code, go to the bottom where the fwrite function is called: <?php //Get Nohay Albums function function GetAlbums($cattype){ if ((isset($_GET['year']))||(isset($_GET['reciter']))){ //nothing displayed } else{ $query = "SELECT COUNT(Topic), SUM(Hits) AS totalhits, FileFormat, Speaker, Language, Year, FileType, Cat FROM nauhey WHERE Cat='".addslashes($cattype)."' AND Year='".date('Y')."' GROUP By Speaker, Year"; $result = mysql_query($query) or die (mysql_error()); //<!------------ Create Cache -------------!> //folder where cache is saved $path = "cache/"; //Page name format: FunctionName_Category_Year.html $pagename = $path."getalbums_".$cattype.".html"; $cacheFile = fopen($pagename, "w"); if ($cacheFile == FALSE){ die ("Unable to create cache file"); } //Get HTML in a variable $obstart = ob_start(); if ($obstart == FALSE){ die ("Unable to buffer cache content!"); } //<!------------ Cache closed -------------!> if ($cattype = strtolower('nauhey')){ while ($row = mysql_fetch_array($result)){ echo ("<div class='container'>"); echo ("<img src=\"http://localhost/HAQMOLAAALI/backed up/haqmolaali/images/".$row['FileFormat'].".gif\">"); echo ("<b style=\"font-size:10px; color:#000066; position:absolute; margin-left: 500px;\">DOWNLOADS: ".$row['totalhits']."</b>"); echo (" <a href=\"nohay.php?reciter=".urlencode($row['Speaker'])."&year=".urlencode($row['Year'])."\">".$row['Speaker']." Vol ".$row['Year']."</a> <b style=\"font-size:10px; color:#000000;\">(".strtoupper($row['Language']).")</b>"."\n"); echo ("<div class='small'><b>Year: ".$row['Year']." | Language: ".ucfirst($row['Language'])." | Tracks: ".$row['COUNT(Topic)']."</b></div>"); echo ("</div>"); }//while loop closed } else { while ($row = mysql_fetch_array($result)){ echo ("<div class='container'>"); echo ("<img src=\"http://localhost/HAQMOLAAALI/backed up/haqmolaali/images/".$row['FileFormat'].".gif\">"); echo ("<b style=\"font-size:10px; color:#000066; position:absolute; margin-left: 500px;\">DOWNLOADS: ".$row['totalhits']."</b>"); echo (" <a href=\"majlis.php?reciter=".urlencode($row['Speaker'])."&year=".urlencode($row['Year'])."\">".$row['Speaker']." Vol ".$row['Year']."</a> <b style=\"font-size:10px; color:#000000;\">(".strtoupper($row['Language']).")</b>"); echo ("<div class='small'><b>Year: ".$row['Year']." | Language: ".ucfirst($row['Language'])." | Lectures in series: ".$row['COUNT(Topic)']."</b></div>"); echo ("</div>"); } } //<!------------ Cache Code -------------!> $cacheContent = ob_get_contents(); //Write to cache file fwrite ($cacheFile, $cacheContent) or die ("Cannot write cache content to file!"); fclose ($cacheFile); ob_flush(); ob_end_clean(); //<!------------ Cache Closed-------------!> } } GetAlbums("nauhey"); ?> When I execute the function, I get my self-made error "Cannot write cache content to file!". Help would be much appreciated. Thank you. I am trying to use output buffering in the following manner: Code: [Select] ob_start("OB_BUFFER"); echo 'Start'; do_stuff_function(); ob_end_clean(); echo 'End'; The problem is that when the output of "do_stuff_function" exceeds 4k the output is automatically flushed and I don't know why (exceeding some limit I suppose). So when the output of "do_stuff_function" is less than 4k, I get this: Code: [Select] Start My <4k output here... End When "do_stuff_function" exceeds 4k, I end up with this: Code: [Select] My > 4k output here... Start End I don't understand why this is. The docs seem to indicate that the way I am starting the ob_start() function there should be no buffer limit. Is that not true? |