Floeske
Floeske

Reputation: 110

PHP Header() function not redirecting

I've got a problem with the header function

if ( ! isset($_SESSION['user']) ) {

   header('Location: login.php');

}

There is no echo before the if, the page is empty!

it doesn't redirect me. I've var_dump();'ed something in the If statement and it worked so the if is correct.

The php version is 5.6

Upvotes: 0

Views: 2007

Answers (4)

n-dru
n-dru

Reputation: 9430

You might have outputted something to the browser before header call. First check:

  • is there any html before php opening tag

  • is there any whitespace before php opening tag

  • is another php script included with echo before that script

  • is the coding utf8 with BOM?

Those are possible reasons of your error.

Upvotes: 5

Basheer Kharoti
Basheer Kharoti

Reputation: 4302

white space may cause the redirect change it to something like

if ( ! isset($_SESSION['user']) ) {

   header('location:login.php');

 }

Upvotes: -1

Rahul Gavande
Rahul Gavande

Reputation: 324

You can also do following:

echo "<script>window.location = 'login.php';</script>";

Upvotes: 1

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

Try this code :-

<?php
if ( ! isset($_SESSION['user']) ) {
?>
<script>
    window.location = 'login.php';
</script>
<?php
}
?>

Upvotes: 2

Related Questions