This question already has an answer here:
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");
}
?>
</div>
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.