Jump to content

[SOLVED] including files disrupts the page


Yesideez

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.