Yesideez Posted April 15, 2009 Share Posted April 15, 2009 I've got a very basic site set up, here's the flow of the scripts as simple as I can make it: 1. index.php includes header.php 2. header.php includes the required classes 3. the page is output Simple. Although for some reason the classes being called in using require_once are sending something to the browser as header() and session_start() both complain that output has already been sent. I've checked the included files and there is nothing being sent not even a space at the start/end of the files. Anyone come across this before? I'm wanting to use autoload() with my classes to save access times and overheads and can't but either way I can't get rid of this empty space at the top of the browser because of the includes. I've even tried including empty classes and going as far as empty files and it doesn't make any difference. The files are encoded using UTF-8 (with proper character set headers being used) if that makes any difference. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/ Share on other sites More sharing options...
FaT3oYCG Posted April 15, 2009 Share Posted April 15, 2009 i thought you had to session_start() before the head inside your html, therefore remove the session_start() from the headder.php and simply place it at the top of the index.php file and see what that does p.s. if your includes contain any session_start() then you only need the main one in the index file. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-810590 Share on other sites More sharing options...
Yesideez Posted April 15, 2009 Author Share Posted April 15, 2009 I've only got the one session_start() within the script otherwise I'd get errors that I've already used it. It's actually at the very start of the main index file but nothing makes any difference. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-810613 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2009 Share Posted April 15, 2009 Save UTF-8 files without the BOM (Byte Order Mark) characters. Otherwise, post the error and the files mentioned in the error to get specific help with the problem. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-810623 Share on other sites More sharing options...
soak Posted April 15, 2009 Share Posted April 15, 2009 I suspect it may be content after the last ?> in your included header page or your classes. If so, either strip the excess or do what I do and remove the last ?> completely. PHP will work fine without it and that way you NEVER get any extraneous spaces appearing. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-810634 Share on other sites More sharing options...
Yesideez Posted April 15, 2009 Author Share Posted April 15, 2009 PFMaBiSmAd: The files are saved out like that in fact I'm thinking of switching them all over to some other coding. soak: As mentioned earlier, there are no extra characters at the start/end of the scripts. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-810669 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2009 Share Posted April 15, 2009 Otherwise, post the error and the files mentioned in the error to get specific help with the problem. Without specific information about what you see in front of you and what your code is, no one can provide a specific answer, just play a game of 20 guesses. You cannot identify what is causing the output that is preventing the headers from working, what makes you thank someone else can without seeing the error and the files responsible for the error. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-810685 Share on other sites More sharing options...
Yesideez Posted April 16, 2009 Author Share Posted April 16, 2009 Because I was wondering if anyone has experienced a similar thing. I have no output which is why I can't post any output. Well, something is output but it's empty space which is severe enough to disrupt the flow of the page. It's a waste of time posting code because it's standard PHP but here goes. <div style="background-color:#f00"> <?php include('myfile.php'); ?> </div> This is the contents of myclass.php: <?php echo 'test'; ?> In myclass.php nothing is after the ?> not even a space yet what I get in my browser is instead of the word "test" inside a thin band I get the word "test" inside a red band with about 15px top padding. I can't figure out where the hell it's coming from. I've even tried setting the padding and margin to 0 but they have no effect - the gap is still there. Encoding set to UTF-8 with no BOM. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-811455 Share on other sites More sharing options...
premiso Posted April 16, 2009 Share Posted April 16, 2009 Is error reporting on? Are you sure that myfile.php is being loaded and read and that it is in the same directory as the script you are running? It seems to me that myfile.php is not being ran for whatever reason and error reporting may be set to low or display errors is turned off. Paths do matter for including files. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-811498 Share on other sites More sharing options...
PFMaBiSmAd Posted April 16, 2009 Share Posted April 16, 2009 <div style="background-color:#f00"> You are outputting HTML tags before your class/session_start()/header() statements. That IS content being sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-811517 Share on other sites More sharing options...
Yesideez Posted April 16, 2009 Author Share Posted April 16, 2009 Erm...no I'm not sending data. The session_start() is inside at the very start of the HTML file - same place I always put them. That code is just an example as to what is getting messed up. All I want to remove is that blasted gap that gets added. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-811786 Share on other sites More sharing options...
PFMaBiSmAd Posted April 16, 2009 Share Posted April 16, 2009 Anyone come across this before? Because I was wondering if anyone has experienced a similar thing. Yes, everyone here has probably experienced and has come across this before. You seem to be expecting a "each symptom", "has one cause" relationship and that someone can just tell you to change some specific thing to fix the problem. Programming just does not work that way due to its' general purpose nature. You could put 1,013 monkeys in front of computers and each one of them could write code that has the same symptom that you are getting and there could be a different problem in each of their programs that is causing that same symptom. I'm going to guess this is actually a HTML/CSS issue, because you have not provided any evidence to indicate it is not. The session_start() is inside at the very start of the HTML file - same place I always put them.Then why did you state this mystery output was causing the session_start() to complain? When you are getting session_start()/header() errors, those errors state where the output is being started at. Knowing that information would help pinpoint what is causing that output. Post the session_start/header errors you are/were getting. You have described some output. Show us exactly where in the rendered HTML in the "view source" of the browser this output is at and post all the relevant code, starting with the main page through whatever code is responsible for at least a few lines past the point where the output is at. Attach your files to the post so that someone can attempt to duplicate the problem. For all we know you are using some word processor on a MAC that is producing files that cannot be used for coding. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-811880 Share on other sites More sharing options...
Yesideez Posted April 17, 2009 Author Share Posted April 17, 2009 You don't seem to understand - I'm NOT getting ANY error messages because there are NO errors! There NEVER have been. For some strange reason I'm getting an extra space at the exact point where my files are being included even if they're empty which is what I can't understand. The only way I can get rid of them is something like this: <?php session_start(); ob_start(); require_once('classes/some_class.php'); require_once('classes/another_class.php'); ob_end_clean(); //removes the weird gap at the top of the page ob_start(); //normal code resumes here If I include my classes where I want them included (which is in the middle of various places) I get the content of my DIVs positioned down about 10-20 pixels where they should be. Using the above method this doesn't happen. Only problem is all my classes have to be included every time the pages load which is a pain for obvious reasons. With my "fix" above everything works smoothly and isn't causing a problem but obviously I'd like to get to the bottom of this so I can stop it happening. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-812140 Share on other sites More sharing options...
Yesideez Posted April 17, 2009 Author Share Posted April 17, 2009 Just seen premiso's post - yes, full error reporting is on - strict mode and includes are all being called in fine as everything works as it should except for the extra "padding" Sure is confusing the hell out of me why this is happening! :/ When I get home tonight I'll make a short example and put it online and post the entire source so you can see exactly what I mean. Apologies for this confusion, I'm very competent at PHP but this I've never experienced before. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-812141 Share on other sites More sharing options...
i Posted April 17, 2009 Share Posted April 17, 2009 Hello Yesideez. I am new to this site, but I've been hanging around PHP for a dozen years of playing! I feel for you. I am teaching my wife PHP now and I keep telling her to look for the extra spaces... Wait till I start telling her to look for the invisible spaces! So I will share few of my favorite debug tips... Remember that all php starts in html mode. Even the end of file! That means that until you get the first <?php it is in html mode. A Comment A little trick I have done for years is to put the file name in a comment at the beginning... <?php // myclass.php ?> See if that solves some problems for you on the empty files you are including. exit; The next debug friend you will want to know is... <?php exit; ?> exit; stops processing. Try your myclass.php: <?php echo 'test'; ?> Like This... <?php echo 'test'; exit; ?> You can use exit; to debug a lot of stuff because you can see in your script where the error vanishes! echo ... it makes output! In your class look for any echo statement. any echo produces output. So in your class all echo statements must be in a condition statement (like if) to prevent it from running if you have it before your session_start();. As for me I would start the session first. In myclass.php nothing is after the ?> not even a space yet what I get in my browser is instead of the word "test" inside a thin band I get the word "test" inside a red band with about 15px top padding. This may not be php, this may be CSS. <div style="background-color:#f00"> <?php include('myfile.php'); ?> </div> in myfile.php <?php echo 'test'; ?> When you see it in your browser - view the html webpage source. It should say... <div style="background-color:#f00"> test </div> check your CSS for html, body, and div Might I suggest you modify your first code to be... <div style="background-color:#f00"> <p><?php include('myfile.php'); ?></p> </div> in myfile.php <?php echo 'test'; ?> That's html I know, but would use the CSS for p instead of for html, body or div... You may also want to do it this way... <div style="background-color:#f00"> <?php include('myfile.php'); ?> </div> in myfile.php <?php echo '<p>test</p>'; ?> Either way the html view source on your browser would give you... <div style="background-color:#f00"> <p>test</p> </div> One of the things I am teaching my wife to do is to try things without the php! If you change: <div style="background-color:#f00"> <?php include('myfile.php'); ?> </div> To: <div style="background-color:#f00"> test </div> Do you get the same wide test with 14px of red padding? If yes, then your php is correct and your html or css is in need... Your php is just inserting something into your html, but your html and css still have to be correct! I have my wife write pure html and then use php to replace what she needs to... <div style="background-color:#f00"> <p>My name is Percy Andes Tait</p> </div> With that working fine, looking just the way she wants it... then modify it... <?php $first_name = 'Percy'; $mid_name = 'Andes'; $last_name = 'Tait'; ?> <div style="background-color:#f00"> <p>My name is <?php echo $first_name;?> <?php echo $mid_name;?> <?php echo $last_name;?></p> </div> Now that looks good split it into two files... <div style="background-color:#f00"> <?php include('myfile.php'); ?> <p>My name is <?php echo $first_name;?> <?php echo $mid_name;?> <?php echo $last_name;?></p> </div> And myfile.php... <?php $first_name = 'Percy'; $mid_name = 'Andes'; $last_name = 'Tait'; ?> Take notice that the myfile.php has NO echo statements! return; Another debug friend of mine is: <?php return; ?> You have 15 functions and one of them is giving you an extra space... which one??? <?php function one () ... ... return; function two () ... ... ?> That return; is NOT part of the function, it is between the first and second,,, you just forget about the rest of the include page... On your myfile.php page try this... <?php return; echo '<p>test</p>'; ?> and this... <?php echo '<p>test</p>'; return; ?> You can start by putting return; at the top of your page and then working it's way down till you find where php is doing something... like out putting that extra space! I hope this helps you, Lan Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-812184 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2009 Share Posted April 17, 2009 as header() and session_start() both complain that output has already been sent.So why did you post that in the first post in the thread? You wanted it to take a really long time to solve this? Which it has, since the replies cannot be any better than the information provided as input. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-812265 Share on other sites More sharing options...
Yesideez Posted April 18, 2009 Author Share Posted April 18, 2009 Sometimes I get myself in a bit of a muddle when trying to explain some things and I did then - sincere apologies! I did try including the files before and after session_start() and no matter what I did I could never get rid of that padding. It felt as if my browser was hell-bent on knowing who I am and screwing with my head. But I can happily say all is now fixed! Software I'm using (for the record) WAMP (latest version) Notepad++ (latest version) Web space (PHP4, MySQL etc.) Photoshop CS3 for artwork I did have "UTF-8 without BOM" selected for the character encoding and just happened to take some of this to work and edit there - this lost the character encoding and reverted it back to plain ANSI. No more padding!!! I've since gone through this site and converted all files to ANSI and I don't have to buffer the output then clear it after including my files. I converted one file back to UTF-8 w/o BOM and the padding came back. Thanks "i" for the input and thanks for taking the time to type that lot - appreciated. btw, no, I didn't get the extra padding putting "test" on its own. Another bugging tip I'm sharing when it comes to conditional code... echo '1'; if (condition) { echo '2'; //something } else { echo '3'; //something else } I can get either 12 or 13 appear and that tells me what I need to know. Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-813022 Share on other sites More sharing options...
i Posted April 18, 2009 Share Posted April 18, 2009 Another bugging tip I'm sharing when it comes to conditional code... echo '1'; if (condition) { echo '2'; //something } else { echo '3'; //something else } I can get either 12 or 13 appear and that tells me what I need to know. Just remember that echo outputs - so if you get a header or session problem it may be your echos! - Lan Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-813043 Share on other sites More sharing options...
Yesideez Posted April 18, 2009 Author Share Posted April 18, 2009 Oh yes, that I'm fully aware of! Just a handy way of debugging conditionals. Just used that very method as it happens. Just written a secure user authentication system and logging in through the login page kept referring me back to the login page but once I'd done this I could navigate to an admin page via the URL directly. If I log out I can no longer navigate directly. Turned out I forgot to add one line of code to one of my classes: $this->session=$r['session']; So when I was logging in and the site settings being loaded in I wasn't loading the logged session ID in and it was checking empty. Not that it has much to do with this topic - went of on a tangent Quote Link to comment https://forums.phpfreaks.com/topic/154198-solved-including-files-disrupts-the-page/#findComment-813061 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.