Headers already sent problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Headers already sent problem

Post by mjseaden »

Hi,

I know this sort of error comes up all the time, but each situation from browsing this forum seems to have unique circumstances and solutions.

My 'property' page is a single script that generates an output page dependent upon any information after the script name, e.g.

property/region

or

property/region/location

or

property/region/location/type35

I detect the contents after the property/.... by using $_SERVER['REQUEST_URI'] and then explode('/', ...) etc.

I do this because, as I am using shared hosting, mod_rewrite is disabled and therefore I have to use this method to create keyword meaningful URLs.

I want to change this URL form however to

property/region

or

property/region.location

or

property/region.location.type35

I know how to do this. However, I don't want Google to come across my site and spider broken URLs - I want the original form of the URLs to be redirected with the permenantly moved header to the new type. So, for example

property/region/location

would be permenantly redirected to

property/region.location

Where property/region.location would then contain the code originally contained in property/region/location.

The problem I'm having is that I'm trying to use

Code: Select all

header(&quote;HTTP/1.1 301 Moved Permanently&quote;);
    header(&quote;Location: http://www.mysite.biz/property/region.location&quote;);
    header(&quote;Connection: close&quote;);
to redirect where the script detects the property/region/location URL form, but i'm getting the age-old error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/xxxx/public_html/property2:1309) in /home/xxxx/public_html/property2 on line 1321 Warning: Cannot modify header information - headers already sent by (output started at /home/xxxx/public_html/property2:1309) in /home/xxxx/public_html/property2 on line 1322 Warning: Cannot modify header information - headers already sent by (output started at /home/xxxx/public_html/property2:1309) in /home/xxxx/public_html/property2 on line 1323
I've had a search of the forum for this particular scenario, and I can't see it specifically referred to. Can anyone give any suggestions as to what to do?

I hope that sufficiently explains the problem

Many thanks

Mark
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

what code do you have around it? It would seem from what you have shared so far that the code is being executed either way, look at the placement of the code to make sure that it follows what you are trying to do.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

The code is of the form of the following psuedocode

Code: Select all

<?php
session_start();
include &quote;includes/globals.php&quote;;
do_dbconnect($dbcnx);

// Take a look at the $REQUEST_URI for indicators
$myarray = explode( '/', $_SERVERї'REQUEST_URI'] );

// We use count() to count number of elements in array
$count = count( $myarray );

$url = $_SERVERї'REQUEST_URI'];

$pagecall = false; 
$propcall = false; 

// Check if a pagecall is made using /pX at the end of the URL
if (preg_match('/\/(p??)(\d+)$/', $url, $match)) 
{ 
  if (!empty($matchї1])) 
  { 
    $pagecall = true; 
  } 
}

// If a pagecall hasn't been made, check if a property call has
preg_match('#ї0-9]*$#', basename($url), $match);
if(!empty($matchї0])){
  $propcall = true;
  $propnumber = $matchї0];
} else {
  $propcall = false;
}

function function1( $blah )
{
}

function function2( $blah )
{
}

if( $count == 2 )
{
    // /property/region
    header(&quote;HTTP/1.1 301 Moved Permanently&quote;);
    header(&quote;Location: http://www.mysite.biz/property/region&quote;);
    header(&quote;Connection: close&quote;);
}

if( $count == 3 )
{
    // /property/region/location
    header(&quote;HTTP/1.1 301 Moved Permanently&quote;);
    header(&quote;Location: http://www.mysite.biz/property/region.location&quote;);
    header(&quote;Connection: close&quote;);
}

...
Hope that makes things a little clearer.

Cheers

Mark


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Check there's no output prior to sending the headers - spaces before or after the php opening and closing tags are prime culprits...or any other output in between.

Remember - headers before output...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hey look at that, we have a tutorial on this:
viewtopic.php?t=1157

mjseaden, start using

Code: Select all

or I start locking your threads.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Okay, that's sorted out. I had some 'echo' statements before the headers - now they've been removed, and there is no output to the browser prior to using header(), they all seem to work.

Cheers for clearing this up

Mark
Post Reply