Page 1 of 1

Headers already sent problem

Posted: Sat Mar 19, 2005 4:25 am
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

Posted: Sat Mar 19, 2005 6:01 am
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.

Posted: Sat Mar 19, 2005 7:29 am
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]

Posted: Sat Mar 19, 2005 7:35 am
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...

Posted: Sat Mar 19, 2005 9:24 am
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.

Posted: Sat Mar 19, 2005 10:14 am
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