homepage Welcome to WebmasterWorld Guest from 54.197.215.146
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

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




msg:3626030
 10:51 pm on Apr 13, 2008 (gmt 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]

 

kwasher




msg:3626094
 1:13 am on Apr 14, 2008 (gmt 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');

cosmoyoda




msg:3626097
 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.

cosmoyoda




msg:3626129
 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?

cosmoyoda




msg:3635631
 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?

ashishp




msg:3635821
 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.

g1smd




msg:3635861
 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.

cosmoyoda




msg:3637168
 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?

bubbasheeko




msg:3640074
 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);

<?
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




msg:3640243
 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