homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Cannot modify headers warning
Extremely strange issue
LinusIT




msg:4564748
 7:34 pm on Apr 14, 2013 (gmt 0)

I've got a strange issue with some code giving me Warning: Cannot modify header information - headers already sent by

I have this code at the top of every page but it is only giving me this error on one particular page. If I copy the code into a new file (test.php) it works perfectly, just like the other pages.

Here is the code thats driving me mad:

<?php
session_start();
error_reporting(E_ALL); ini_set('display_errors', '1');
require_once('includes/config.php');
if (!isset($_SESSION['user_id'])) {
header('Location: error-402.php');
} else if (!$scrap_cars) {
header('Location: error-401.php');
}
include('includes/functions.php');
?>


When a user logs in, it sets a session called "user_id". In config.php there is $scrap_cars variable, this is set to either true or false.

As you'll see the basic idea of the code is, if you're not logged in then redirect to "Not logged in page" but if you are logged in and $scrap_cars variable is set to false then redirect to "You don't have access page".

I have checked for whitespace everywhere, removed all the content so the erroneous page mimics (test.php) but all to no avail.

Please help.......

 

omoutop




msg:4564935
 2:10 pm on Apr 15, 2013 (gmt 0)

either something is causing output in this page before the start of headers(), or this page has probably a BOM element on top of it to mark it as utf-8 encoded page.
Check tat your php code does not echo/print anything before the headers()
Check with your ftp client that your file begins with the php tags and not with some strange characters.

lucy24




msg:4565013
 7:19 pm on Apr 15, 2013 (gmt 0)

If I copy the code into a new file (test.php) it works perfectly, just like the other pages.

The entire code? Not just the if/else bit?

or this page has probably a BOM element on top of it

I'll be darned. I would not have thought of the BOM-- except that another thread linked to w3c's discussion of it.* If your code works fine in a new document, an invisible character may well be the problem. Does it work if you paste the entire page text into a new page and give it the name of the old one? If so, the problem has fixed itself :)

:: memo to self: remember BOM the next time a page displays inexplicably ::

If you open the page locally in a browser and manually set the encoding to Latin-1-- or anything other than UTF-8 or -16 --any nasty invisible characters should jump out loud and clear.


* At long last I understand why the same character is both the BOM and the Zero-Width Nonbreaking Space. It's because it is the same character; it's just been repurposed.

LinusIT




msg:4565024
 8:17 pm on Apr 15, 2013 (gmt 0)

I've just copied the entire code into a new page called (test2.php) and accessed it. How strange that it works just as it should. I have cleared temporary internet files etc etc and tried again, the original page still plays up, the test2.php works perfect.

What on earth is going on?

lucy24




msg:4565029
 8:48 pm on Apr 15, 2013 (gmt 0)

I think omoutop may well have nailed it with the BOM. Do you have a text editor that allows you to reinterpret (not convert) text encoding? Or, as I suggested above, try opening the original page in a browser. If the problem is indeed a BOM, then it won't matter that the php doesn't execute; you should still see the character. You may even see it if you open Page Source from within the browser.

LinusIT




msg:4565985
 11:19 am on Apr 18, 2013 (gmt 0)

Could you explain what BOM is please?

I have got no further with solving the issue. Copying the entire code into a new file works just as it should do. I think there is a demon in the filename :-)

Leosghost




msg:4565987
 11:30 am on Apr 18, 2013 (gmt 0)

Byte Order Mark

[en.wikipedia.org...]

swa66




msg:4565998
 1:12 pm on Apr 18, 2013 (gmt 0)

A BOM can be inserted and made invisible by your (text) editor. Make sure to chose the one you use so that it doesn't do that, or at least know how to configure it not to do that.

As far as PHP code is concerned: you're better off without a BOM in the source code when dealing with UTF-8.
I've far too little experience myself with UTF-16, so I'll refrain from generalizing there, but even if you want/need it in the output, you can always output it yourself as opposed to it being forced upon you by your editor.

LinusIT




msg:4566034
 3:13 pm on Apr 18, 2013 (gmt 0)

Many thanks for the explanation, I've looked into it and found out how to turn these feature off. How frustrating..

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved