-1

I have created 301 redirection from php. Following is my code to redirect url.

$current_url = substr($redirect_url, 11);
$sqlr = "SELECT old_url, new_url FROM cscart_301_redirection WHERE old_url  = '$current_url' ";
$resultr = mysql_query($sqlr);
$rowr = mysql_fetch_array($resultr);
$newurl = $rowr['new_url'];

if(mysql_num_rows($resultr)>0)
{
    header ('HTTP/1.1 301 Moved Permanently');
    header ('Location: '.$host.$newurl);
} 

but when code is run on live server I am getting

Warning: Cannot modify header information - headers already sent by this error. Please let me know where I am doing wrong?

Thanks

absolutek
  • 97
  • 2
  • 4
  • 11
  • Is there a newline just above the opening ` – mobius Aug 31 '12 at 07:31
  • there is no newlinw before – absolutek Aug 31 '12 at 07:34
  • 1
    The error/warning message should contain file names and line numbers like e.g. `Warning: Cannot modify header information - headers already sent by (output started at /home/volker/.htdocs/foo.php:2) in /home/volker/.htdocs/foo.php on line 4`. Please post the complete message you get. – VolkerK Aug 31 '12 at 07:38
  • Warning: Cannot modify header information - headers already sent by (output started at /home/indianet/public_html/var/compiled/customer/%%45^45E^45E480CD%%index.tpl.php:9) in /home/indianet/public_html/var/compiled/customer/%%FD^FD1^FD153A02%%top.tpl.php on line 32 this is the full error message – absolutek Aug 31 '12 at 07:43
  • use `ob_start() after session_start() (if it is)` before line number 9 in index.tpl.php. This will surely help you to achieve the same. – Arun Jain Aug 31 '12 at 08:04

2 Answers2

2

The "headers already sent" error is usually caused by having white space before or after the opening and closing PHP tags (<?php . . . ?>)

Try the following code by writing ob_start() in your if condition. ob_start() method will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

$current_url = substr($redirect_url, 11);
$sqlr = "SELECT old_url, new_url FROM cscart_301_redirection WHERE old_url  = '$current_url' ";
$resultr = mysql_query($sqlr);
$rowr = mysql_fetch_array($resultr);
$newurl = $rowr['new_url'];

if(mysql_num_rows($resultr)>0)
{
    ob_start();
    header ('HTTP/1.1 301 Moved Permanently');
    header ('Location: '.$host.$newurl);
    die;
} 
Arun Jain
  • 5,476
  • 2
  • 31
  • 52
-1
Warning: Cannot modify header information - headers already sent by (output started at /home/indianet/public_html/var/compiled/customer/%%45^45E^45E480CD%%index.tpl.ph‌​p:9) in /home/indianet/public_html/var/compiled/customer/%%FD^FD1^FD153A02%%top.tpl.php on line 32

On line 9 line in the script %%45^45E^45E480CD%%index.tpl.ph‌​p output is sent to the client before the header function on line 32 in top.tpl.php is executed.

A http reponse looks like

header1
header2
header3
<--empty line-->
body
body
body

i.e. as soon as content is sent to the client, php will sent all headers plus the "empty line" and there's no way to get back to the headers sections (you cannot "undo" the empty line that separates the response headers from the response body once it's sent to the client). You need either output buffering, i.e. php holds back the contents/body of the repsonse as long as possible. Or -more advisable- structure your scripts in a way, that the handling of headers happens before the output is generated. In case of a redirect it doesn't make too much sense anyway to say "You find the contents at url, but hey, here's the contents".

VolkerK
  • 95,432
  • 20
  • 163
  • 226