0

I am following along with a Lynda.com tutorial on php and am learning about php redirects. Using header("Location: basic.html"), the tutor says for a redirect to work it must be the very first thing on a page. However, I tested that by putting the php code within the tags and the redirect still worked. I have looked at other online resources and they too say that there should be nothing above the redirect. Are the resources I have looked at simply out of date or am I missing a point here?

Thanks

I am running XAMPP on my local machine. My code is: Redirect

<?php
// This is how you redirect to a new page
    function redirect_to($new_location) {
        header("Location: " . $new_location);
            exit;       
    }

    $logged_in = $_GET['logged_in'];

    if($logged_in == 1) {
    redirect_to("basic.html");
    } else {
        redirect_to("http://www.google.co.uk");
    }
    ?>

</body>

Chris
  • 37
  • 4

7 Answers7

4

There should be no output before a php redirect.

Otherwise you may have the error : "Headers already sent".

But you can do whatever you want with PHP which wouldn't be echo or things like that.

Loïc
  • 11,804
  • 1
  • 31
  • 49
2

This depends on the server for the output method. The server could either start delivering the PHP output as it is computed or only after the whole output is ready. If there was any output sent to the client you can't send an HTTP redirect header anymore.

However you can suppress the output in PHP itself using the output buffering functions. See: http://php.net/manual/en/ref.outcontrol.php

Community
  • 1
  • 1
feeela
  • 29,399
  • 7
  • 59
  • 71
0

Object buffering can help.Try using ob_start() at the top of the file

ob_start();
echo("Hello there!");
Aritra
  • 23
  • 10
0

In your case it is OK. because you are not using any output statement before header() function. Chance of Error: Header will be sent will appear when you use echo() or print() is called before header()

Zeeshan
  • 48
  • 1
  • 8
0

according to the author, redirecting should be first before you output something on the page, because if you try to print something on the page before this redirection, you will get error saying Headers already sent.

So in this context header need to be used first to redirect to other page without any errors.

Developer
  • 3,857
  • 4
  • 37
  • 47
0

Your code works correctly (i.e) when you execute this code it will redirect to www.lynda.com because $logged_in has 0 value because no value in $_GET['logged_in'] variable . As you read tutorial on php in Lynda.com, any output statement before header() function will cause error. In your code there is no output statement mentioned before header() statement hence it wont show any error

Rajesh
  • 786
  • 6
  • 12
0

Thank you all for your replies. In this case it was because I had output_buffering on in my PHP ini. As soon as I turned that off, I got the expected response from the server.

Thanks again for your support.

Chris
  • 37
  • 4