0

for example, this is my PHP code:

<?php

if($country_code == 'US')
{
        header('Location: http://www.test.com');
}

else 
{
        header('Location: http://www.test.com/');
}

?>

I'm trying to use a Javascript code for tracking, it has to be above the </body> tag.

I have tried different ways of combining the PHP code with HTML, I have tried placing the HTML separately below the PHP also, one example:

<html>
<head></head>
<body>
    <?php

     ?>
<script></script>
</body>
</html>

The furthest I got was, it tracked the click but it didn't redirect giving me this error:

`Warning: Cannot modify header information - headers already sent by`

Will appreciate any suggestions and help, thank you!

5
  • 1
    You can't use php's header if you've already started to output the document, you'd have to use a different method for the redirect (i.e. JavaScript's window.location.href). Commented Dec 16, 2012 at 14:45
  • PHP works in order from top to bottom in a sense. Once the DOM starts to render content, PHP can't just stop, reverse, place your header, then rebuild, if that makes sense =] Put that PHP at the top of the page, then work from there. Commented Dec 16, 2012 at 14:48
  • Are you trying to redirect the page based on the variable $country_code? Or are you trying to do something else? Commented Dec 16, 2012 at 14:50
  • Yes, trying to redirect the page and track it using a Javascript code. Commented Dec 16, 2012 at 14:52
  • Where does $country_code come from? Commented Dec 16, 2012 at 15:27

5 Answers 5

1

Put the php redirect code at the begining of your document before anything is outputted. Check for spaces after the ?> tag and before the <?php tag because these will be printed out and the response header will be sent therefor you will not be able to modify the header to redirect.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to try something like the following to track:

<?php

if($country_code == 'US')
{
        header('Location: http://www.test.com/?us=yes');
}

else 
{
        header('Location: http://www.test.com/?us=no');
}

?>

And then in your index page check for the value of the us parameter. Also you should Notice that there is no any output should be printed before the header function to void the warning :

Warning: Cannot modify header information - headers already sent by

Comments

0

The trouble is that PHP run before JavaScript. So you need to geet the PHP variable inside a JavaScript.

<?php
// your normal code here, like connection to DB
?>

<script>var test = <?php> echo $thatVariable; <?> </script>

Comments

0

You may not send an output to the client before a header() tag of php. So you can generate a redirect page which gets the country information via js and send it to the php (using e.g form submit). after that you can redirect to the accoording page via php header()

Comments

0
<?php
if($country_code === 'US'){

   echo "<script> window.location.href='http://www.test.com1'</script>";

}
else{

   echo "<script>  window.location.href='http://www.test.com2/'</script>";

 }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.