-3

I am trying to make a redirect to page based on its referrer. the layout is like

<?php
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
    header("Location: url of another site"); 
    exit();
}
else    {
    header("Location: my home page"); 
    exit();
}

?>

I am getting the error : Cannot modify header information - headers already sent by.

I have to modify my php.ini to output_buffering = On but I can't do this as I have a shared hosting account in hostgator. can anyone suggest me what options now I have? can I do it by changing my code? If yes, than what changes?

narayanpatra
  • 5,627
  • 13
  • 51
  • 60
  • You'll need to attach this code to one of Wordpress' early running [actions](http://codex.wordpress.org/Plugin_API/Action_Reference) to send the headers before output begins. I'd go with [init](http://codex.wordpress.org/Plugin_API/Action_Reference/init) – Phil Nov 07 '13 at 04:08
  • @mario That's the symptom but the problem is Wordpress' crappy architecture. Probably better over at http://wordpress.stackexchange.com/ – Phil Nov 07 '13 at 04:09
  • This question belongs over at http://wordpress.stackexchange.com/ – Phil Nov 07 '13 at 04:10
  • Show us the next part of the error. Cannot modify header information - headers already sent by... there it will tell you where exactly you are outputting the headers! – fos.alex Nov 07 '13 at 04:12
  • also under stanf $_SERVER['HTTP_REFERER'] is browser set, often blank and and can never be relied on. –  Nov 07 '13 at 04:14

4 Answers4

1
<?php
function custom_redirect() {
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
    header("Location: url of another site"); 
    exit();
}
else    {
    header("Location: my home page"); 
    exit();
}
}
add_action('init','custom_redirect');
?>

Try hooking your function to INIT HOOK

Prince Singh
  • 5,023
  • 5
  • 28
  • 32
1

Hi actually header("Location: url of another site"); will not support in wordpress. Instead of this you can use

<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

and for home page just use <?php wp_redirect( home_url() ); exit; ?> this will solve your problem of redirection.

0

try using javascript instead

<?php
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
   ?>
   <script type="text/javascript">window.location = "http://www.yoururl.com";</script>
   <?php
}
else    {
    ?>
     <script type="text/javascript">window.location = "http://www.anotherlocation.com";</script>
    <?php
}

?>

check the reference here

Community
  • 1
  • 1
loQ
  • 2,147
  • 18
  • 21
0

Header Information already sent is a wordpress's common error, like as you mentioned you want to redirect the web site then you have to options to follow.

  1. Create a wordpress Template file do not call header in that file

    <?php /* Template Name:Redirect*/ ?> <?php //get_header();?> <!-- Do not un comment this function -->Write your code here Write your code here Write your code here Write your code here <?php get_footer();?>

  2. Or you can redirect with java script's window.location function

    <script>document.write(location.http://google.com); </script>

Shobhit
  • 182
  • 9