-4

File redirect.php contain this code:

   <?php

$url  = $_SERVER["PHP_SELF"];
$path = explode("?q=", $url); 
$last = end($path);
header('Location: http://mywebsite2.com/something&q='.$last);
?>

However, when I go http://mywebsite.com/redirect.php?q=anything, it shows an error:

Cannot modify header information - headers already sent by".

How to fix it?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
user3576468
  • 23
  • 1
  • 3

3 Answers3

1
header('Location: '.$yourURL);

or you can use

header("Location: yourPage.php");
die();

Why you should use die() go to this link

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
0

Looks like you have some whitespace before the opening <?php tag. This will cause the error.

   <?php
^^^ remove
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Header Must be FIrst HTML ELEMENT in the document Remember spaces do count as html elements

remove them

header('Location: http://mywebsite2.com/something&q=anything');
die():

for more

PHP variable in header function

EDITED

ADD ANYTHING IN YOUR URL :) SImple

Community
  • 1
  • 1
M.chaudhry
  • 651
  • 1
  • 6
  • 13