Welcome to WebmasterWorld Guest from 54.163.52.98

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

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

     

cosmoyoda

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

5+ Year Member



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]

kwasher

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

10+ Year Member



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');

cosmoyoda

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

5+ Year Member



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

cosmoyoda

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

5+ Year Member



Is there a way to do this with PHP only?

How does Output buffering work here?

cosmoyoda

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

5+ Year Member



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?

ashishp

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

10+ Year Member



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.

g1smd

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

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



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.

cosmoyoda

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

5+ Year Member



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?

bubbasheeko

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

5+ Year Member



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.

g1smd

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

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



Why are you doing this in the script?

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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month