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
  • 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,296
  • 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
  • 92,047
  • 28
  • 163
  • 215
1

You shouldn't need the additional '

$redirect = "Location: http://".$_GET['redirect'];
BenOfTheNorth
  • 2,884
  • 1
  • 19
  • 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,045
  • 6
  • 18
  • 27
0

You can only use redirect without echo:

header("Location: http://".$_GET['redirect']);
Smamatti
  • 3,891
  • 3
  • 30
  • 42
Dawid Sajdak
  • 3,074
  • 2
  • 21
  • 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,136
  • 18
  • 69
  • 98