Notice that I’ve highlighted the functions that are used for output buffering. To begin output buffering, we use the ob_start() function, which is located at the very top of the page: <? ob_start(); session_start(); //someone registered? if(isset($_GET['reg'])){ $reg="Your details have been added, please login"; } $error=false; $errmsg=""; //has form been submitted if(isset($_POST['key'])){ //check that the username and password is not empty if( empty($_POST['uname']) && (empty($_POST['upass']))){ print "Please enter your username and password."; $errmsg="Please enter your username and password."; Once you’ve called the ob_start() function, every single output for this page will be sent to a memory buffer rather than to the web browser. HTTP calls, like the header() and setcookie(), won't be buffered and will operate as usual. Towards the end of the script you will notice the ob_end_flush() function: <tr> <td> </td> <td><input type="submit" name="submit" value="Login"></td> </tr> </table> </form> <!-- InstanceEndEditable --></td> </tr> <tr> <td class="copy">©2008</td> </tr> </table> </body> <!-- InstanceEnd --></html> <? ob_end_flush(); ?> This function will take the accumulated buffer and send it to the web browser. You can also use the ob_end_clean() function; it does the same thing. Both of the functions turn off output buffering. From a developer’s point of view, there is not so much of a benefit except that once you start using these functions, you really don’t have to spend any time worrying about HTTP headers. When you use templates in your applications, it would be beneficial to place these functions at the top (ob_start() and ob_end_flush() at the bottom). That way, these functions will be available on every page of your site.
blog comments powered by Disqus |
|
|
|
|
|
|
|