homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Redirecting "/myfile.php" to "/myfile"
Cannot send header information: "Headers have already been sent"

 10:51 pm on Apr 13, 2008 (gmt 0)

This is giving me a headache. I've searched this forum and I found out many people had the same problem.

I created a nice script that checks if the page has a .php extension to it and then redirects it to that same page, only WITHOUT the .php extension. So the problem is, the script doesn't work because it says "Headers have already been sent".

The only way to fix this is by placing the PHP code in the first line of the code. But I need to place it in EVERY SINGLE page of my site, so it would be best to place it in the sessions.php file, a file which is included in every single page of my site.

Even thought my script code is the first thing in the sessions.php, it doesn't work.

I've read somewhere that Output Bufferring functions could save me here, but I dunno how to use it, where to place the functions. Here's my sessions.php file:

// URL Redirect Function
$url_boolean = strpos($url, '.php');
$url_extensionless = str_replace('.php', '', $url);

if ($url_boolean == TRUE) {
header("Location: http://www.example.com{$url_extensionless}");
} else { }

// My Sessions Handling Functions Goes Here

Please help. Thank you very much!

[edited by: dreamcatcher at 7:03 am (utc) on April 14, 2008]
[edit reason] Use example.com, thanks. [/edit]



 1:13 am on Apr 14, 2008 (gmt 0)

Try this function (I think I got this from php.net a while ago):

function Redirect($url) {
if(headers_sent()) {
echo "<script type='text/javascript'>location.href='$url';</script>";
} else {
header("Location: $url");


 1:29 am on Apr 14, 2008 (gmt 0)

That's good, kwasher.
The only problem is that it's JavaScript dependent, but it's a good idea.


 2:45 am on Apr 14, 2008 (gmt 0)

Is there a way to do this with PHP only?

How does Output buffering work here?


 7:04 am on Apr 26, 2008 (gmt 0)

I think kwasher's works perfectly. The only problem is that hackers can just disable JavaScript and that would defeat the purpose of redirecting the users once necessary.

Is there a way to do this using Output Control functions in PHP? Could that work?


 6:05 pm on Apr 26, 2008 (gmt 0)

Does the error message tell you the output was started in which file/line?

That might help you debug it further.

I used to include the sessions file in my main file and sometimes had spaces or something output before the sessions.


 7:19 pm on Apr 26, 2008 (gmt 0)

The javascript redirect would not be followed by search engines, and in theory could easily be classed as a "sneaky redirect".

This is because users would be redirected and see different content to that seen by bots. I would not risk that.


 12:15 am on Apr 29, 2008 (gmt 0)

I would not risk that.

Totally agree.

I saw somewhere that this Header Already Sent error message can be avoided using output buffering. How so?


 4:03 am on May 2, 2008 (gmt 0)

Hi cosmoyoda,

By chance is your sessions.php file being included in another file? If that is the case your include for sessions.php should be the first include and nothing can be before that...

IE - (example - index.php);


This would generate a headers already sent error. Where:

<? include('options/session.php');

would not generate headers already sent...but...yes there is a but...if your settings.php has any redirect instructions then you will run into the same problem.

A suggestion as I don't know if you are even using includes.


 9:41 am on May 2, 2008 (gmt 0)

Why are you doing this in the script?

I would use Mod_Rewrite code in .htaccess to do this.

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