1

Possible Duplicate:
Headers already sent by PHP

i want to redirect to some page so i tried like this.

www.example.com/logout.php?redirect=www.index-page.com/index.php
         echo $redirect = "'Location: http://".$_GET['redirect']."'";
       //redirects to index
       header($redirect);

But it is not working for me. is ther any suggestions.

Community
  • 1
  • 1
Ragavendran Ramesh
  • 376
  • 1
  • 7
  • 23
  • This needs basic debugging first, please log (and/or display) errors and view the PHP error log. Additionally you will learn more in the duplicate question. – hakre Sep 21 '12 at 11:23

6 Answers6

4

Something like this:

In logout link:

<a href="http://www.example.com/logout.php?redirect=<?=urlencode('http://www.index-page.com/index.php')?>"> Logout </a>

On logout.php page:

<?
    // destroy session
    header('location:'.urldecode($_GET['redirect']));
    die();
?>
VibhaJ
  • 2,256
  • 19
  • 31
2

you can not use echo before headers if not it would generate Warning: Cannot modify header information - headers already sent by

    $redirect = "Location: http://". $_GET['redirect'];
    header($redirect);
Baba
  • 94,024
  • 28
  • 166
  • 217
1

You shouldn't need the additional '

$redirect = "Location: http://".$_GET['redirect'];
BenOfTheNorth
  • 2,904
  • 1
  • 20
  • 46
1

Here is an example to redirect user to Google

$link='http://Google.com';
header("location: $link");

You can make it a function

function redirectUser($link){
    header("location: $link");
}

And then you can call it like

redirectUser('http://google.com');

Or

echo redirectUser('http://google.com');

No errors will happens!

I Advise you to use die(); After the redirection code, to abort the comming codes

Alaa Gamal
  • 1,135
  • 7
  • 18
  • 28
0

You can only use redirect without echo:

header("Location: http://".$_GET['redirect']);
Smamatti
  • 3,901
  • 3
  • 32
  • 43
Dawid Sajdak
  • 3,064
  • 2
  • 23
  • 37
0

the problem is here

echo $redirect = "'Location: http://".$_GET['redirect']."'";
//redirects to index
header($redirect);

You should not have to use echo before header, this will cause warning header already sent.

Use it like this

$redirect = "Location: http://".$_GET['redirect'];
//redirects to index
header($redirect);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100