Welcome to WebmasterWorld Guest from 54.167.86.211

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Redirecting "/myfile.php" to "/myfile"

Cannot send header information: "Headers have already been sent"

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

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 0


Hi,
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:

<?php
// URL Redirect Function
$url = $_SERVER['REQUEST_URI'];
$url_boolean = strpos($url, '.php');
$url_extensionless = str_replace('.php', '', $url);

if ($url_boolean == TRUE) {
header("Location: http://www.example.com{$url_extensionless}");
exit;
} 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)

Full Member

10+ Year Member

joined:Sept 24, 2003
posts:318
votes: 0


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


###BEGIN REDIRECT IF HEADER ALREADY SENT
function Redirect($url) {
if(headers_sent()) {
echo "<script type='text/javascript'>location.href='$url';</script>";
} else {
header("Location: $url");
}
}
//usage
//Redirect('somefile.html');
1:29 am on Apr 14, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 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)

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 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)

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 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)

Junior Member

10+ Year Member

joined:May 27, 2004
posts:82
votes: 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)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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)

Junior Member

5+ Year Member

joined:Nov 3, 2007
posts:131
votes: 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)

New User

5+ Year Member

joined:May 2, 2008
posts:24
votes: 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);

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

This would generate a headers already sent error. Where:

<? include('options/session.php');
include('options/settings.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)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Why are you doing this in the script?

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