0

Expample Link: http://mywebsite.com/?go=google

<?php if(isset($_GET['go'])) { ?>
<?php $link_id = $_GET['go']; ?> <!-- output = google -->

<!-- Short explanation: "google" is generating a link for google website -->
<?php $link_url = "https://www.google.com"; ?>

<?php header("Location: $link_url"); ?>
<?php exit; ?>
<?php } ?>

That link will redirect to https://www.google.com, but It is not redirecting when I access on mobile browser.

  • 2
    Why would you open and close php on every new line? –  Jan 14 '15 at 08:00
  • @Koen Hoeijmakers sometimes I need to insert html contents, and It will be more easy to do that for me.. – Eva Silviana Jan 14 '15 at 08:02
  • Follow this article it will help you. http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Debug Diva Jan 14 '15 at 08:07
  • @Eva Silviana but if you intend to redirect the HTML comments will never be seen on the browser, why not make them PHP comments? – komodosp Jan 14 '15 at 08:19

2 Answers2

0

You can't output even a single output before the header.Also you should have only one starting and ending for this header.You can use ob_start() to. use the code below

    <?php if(isset($_GET['go'])) { 

     $link_id = $_GET['go'];
    $link_url = "https://www.google.com";
    header("Location: $link_url");
     exit;
   } 
?>

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
  • That won't work either because even the returns between each `` line will cause the error. You need to just have a single `` at the end of the script. – komodosp Jan 14 '15 at 08:16
0

You do not have to break the PHP block.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

<?php if(isset($_GET['go'])) {
    $link_id = $_GET['go']; //output = google 

    //Short explanation: "google" is generating a link for google website 
    $link_url = "https://www.google.com"; 

    header("Location: $link_url");
    exit; 
 }?>
Dmitry
  • 2,057
  • 1
  • 18
  • 34