-2

I've got a huge problem with my php-file. I want to redirect to another site through header(). But the if-clause doesn't get accepted and I can't think of any problem with it. I tried everything, but it won't work. Could you help me?

<?php

session_start();
include("php/connect.php"); //connection to database
$test = 0;

if($test == 0){
    header("Location: /nextsite.php");
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    Dear George, without actual code pasted in right here it's just Rumble in the Jungle... – Jan Dec 20 '15 at 14:21
  • 1
    It might be that it enters, but if you have any kind of output prior to using `header()`, it won't work. See [Headers already sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php). If you add `echo "Test";` inside the if-statement, you will be able to tell if it enters or not. – Qirel Dec 20 '15 at 14:22

1 Answers1

0

This is because the header is already sent to the client before your if clause is executed. I normally use a buffer before loading the page in php.

<?php
ob_start();
session_start();
include("php/connect.php"); //connection to database
$test = 0;

if ($test == 0) {
    header("Location: /nextsite.php");
}

ob_end_flush();
?>

Note that, the client can only view the page after the entire html has been downloaded. So it might be better to use ob_end_flush() after your if clause.

Pavlin
  • 5,390
  • 6
  • 38
  • 51
Woody
  • 612
  • 9
  • 21