-3

Possible Duplicate:
Headers already sent by PHP

I have a class file that handles registrations and after a successful registration I want to redirect them to another page, but if I use something like:

    if(!isset($_SESSION['referer'])) {
        $_SESSION['referer'] = "home.php";

        header("Location: " . $_SESSION['referer']);
        exit();
    }

I get the error:

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

Is there an alternative (non java script) way of redirecting the user to another page?

Here is my current code flow:

// index.php file
<?php 
include('header.php');
include_once('classes/class.signup.php');

$signUp = new SignUp();

// redirect in signup.php class
if(!isset($_SESSION['referer'])) {
    $_SESSION['referer'] = "home.php";      
    header("Location: " . $_SESSION['referer']);
    exit();
}
Community
  • 1
  • 1
Paul
  • 11,671
  • 32
  • 91
  • 143
  • 3
    Alternative is to prevent any output before `header()`. It is pointless to output anything in case that user will **never** see that – zerkms Jan 13 '12 at 02:20
  • to make sure i don't accidentally send header data i consequently never close my ` – davogotland Jan 13 '12 at 02:24

3 Answers3

0

You most likely get this error because somewhere before you call the header() function you output something to the browser, by an echo for example. You can use the a combo of ob_start() and ob_end_flush() as a quick fix, but perhaps you should reconsider the flow of your application. For the ob (output buffer) functions look here: http://www.php.net/manual/en/function.ob-start.php.

Edit: even a space or other whitespace before php opening or closing can cause the same effect.

Edit2: Can't really see if anything's wrong with your code in your example. Try the output buffer functions and see if helps. Don't want to use them? Than you'll have to track down the output you already sent.

Ruben
  • 89
  • 7
0

You can use your web-browser's 'view source' function to investigate whether there are stray characters written to the output of the page before the exception. Perhaps one of your included files is the culprit.

cars
  • 421
  • 7
  • 18
-2

You can try with html meta tag. First value is delay in seconds, second the url you want to redirct to.

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/index.html">
Corubba
  • 2,229
  • 24
  • 30
  • No, if he uses PHP he should rather solve the problem with content output before the code part that should redirect. Besides that, a meta-refreh is not as reliable as a server-side redirect. – Constantin Groß Jan 13 '12 at 09:15