Yesideez Posted March 20, 2009 Share Posted March 20, 2009 Just playing with this. I thought when ob_start() was called nothing is sent to the browser? Eg. function doHomePage() { ob_start(); include('homepage.php'); $page=ob_get_contents(); echo $page; } The contents of homepage.php is just HTML. What I'm getting is the contents of homepage.php twice! Am I doing something wrong or is this a configuration problem? Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/ Share on other sites More sharing options...
premiso Posted March 20, 2009 Share Posted March 20, 2009 You are echoing $page, and when the browser closes it flushes (displays to the screen) the output buffer. To stop/prevent this you need to use ob_end_clean to clear the output buffer that is being held. Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789784 Share on other sites More sharing options...
Yesideez Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks! Works a treat! Just now have to figure out why the contents of homepage.php is appearing a couple lines down from where it should :/ Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789805 Share on other sites More sharing options...
Yesideez Posted March 20, 2009 Author Share Posted March 20, 2009 When capturing output to the buffer does anything get "added" at the start? When I use this I get an empty space at the top right before where homepage.php is being shown and I can't get rid of it even though my CSS is setting margin and padding all to 0. In index.php I'm checking "page" which is being sent in the URL (eg. index.php?page=home) and this is the function that gets called: function doHomePage() { echo '<h1>Hello</h1>'; include('homepage.php'); $page=new homepage(); echo $page->contents(); unset($page); } This is the contents of homepage.php: <?php class homepage { function contents() { ob_start(); ?> <h1>Welcome...</h1> <p>Test line 1</p> <p>Test line 2</p> <?php $tmp=ob_get_contents(); ob_end_clean(); return $tmp; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789825 Share on other sites More sharing options...
premiso Posted March 20, 2009 Share Posted March 20, 2009 Show the full code of the index.php page. Chances are there is something in that code causing the problem. Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789828 Share on other sites More sharing options...
Yesideez Posted March 20, 2009 Author Share Posted March 20, 2009 This is index.php in full... <?php /* File: index.php ** Started: 08-Feb-2009 (Ivan Rood) ** Edited : 08-Feb-2009 (Ivan Rood) *********************************************************/ require_once("includes/dbconfig.php"); require('includes/site.php'); ?> <html> <head> <title>Amiga Software Downloads :: Home</title> <link rel="stylesheet" type="text/css" href="css/main.css" /> <link rel="shortcut icon" href="favicon.ico" /> <?php include("includes/metatags.php"); ?> </head> <body> <div id="wrapper"> <div id="title"> </div> <?php require("includes/menu.php"); ?> <div id="wrap_left"> <?php switch (strtolower($_REQUEST['page'])) { case 'demos':doDemos();break; case 'games':doGames();break; case 'apps':doApplications();break; case 'lsd':doLSDDocs();break; case 'winuae':doWinUAE();break; case 'links':doLinks();break; case 'tome':doLinkToMe();break; case 'stats':doSiteStats();break; case 'contact':doContactMe();break; default:doHomePage(); } ?> </div> <?php include("includes/panel_right.php"); include("includes/footer.php"); ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789831 Share on other sites More sharing options...
Yesideez Posted March 20, 2009 Author Share Posted March 20, 2009 This is what I've now changed in site.php: function doHomePage() { echo '<h1>Hello</h1><h1>test</h1>'; include('homepage.php'); $page=new homepage(); echo $page->contents(); unset($page); } This is now the contents of homepage.php: <?php class homepage { function contents() { ob_start(); echo '<h1>Welcome...</h1> <p>After a major re-vamp (and some much appreciated persuasion) I\'m back with a new-look site.</p> <p>Before I go on I must say that there will be no Kickstart ROM images, Kickstart boot disks or Workbench disks available for download as these are all still held under strict copyright by <a href="http://www.amigaforever.com/" class="url">Amiga Forever</a>. If you see any software here that shouldn\'t, please <a href="contactme.php" class="url">contact me</a> letting me know who you are and what the file(s) are. With so many games and applications available for the Amiga computer over the years it\'s very difficult to keep track of what can and cannot be freely distributed.</p>'; $tmp=ob_get_contents(); ob_end_clean(); return $tmp; } } ?> The output can be seen here: http://www2.pictureinthesky.net/ Note the big gap above "Welcome..." which I can't find out why is there. Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789845 Share on other sites More sharing options...
Yesideez Posted March 20, 2009 Author Share Posted March 20, 2009 Seems I can't include homepage.php incide the function itself. I moved that one line outside of the function and it works. Quote Link to comment https://forums.phpfreaks.com/topic/150387-solved-question-re-ob_start/#findComment-789867 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.