user1301404
user1301404

Reputation:

PHP header redirect - Not Working

This is the code:

if($rowCheck>0) {
  session_start();

  while($row = mysql_fetch_assoc($check)) {
    $_SESSION['developer'] = $row;          
  }

  header("Location: http://".$_SERVER['SERVER_NAME']."/index.php");
}
else
    echo 'Invalid Password or Email!';  

Everything is working fine except the header, and if i write an echo after the header it will work normally. Also if i printed the location, it will show me the exact and right location.

I'm running on mamp and macos. So it's on a localhost.

Any help ?

Upvotes: 1

Views: 927

Answers (4)

Steve Robbins
Steve Robbins

Reputation: 13822

Couple things.

First I would put ini_set("display_errors", 1); at the very top of your page within the PHP code. You may be getting a "headers already sent" error.

Add an exit after the header to keep the script from going any further:

header("Location: http://".$_SERVER['SERVER_NAME']."/index.php");
exit;

Lastly this bit doesn't make sense.

  while($row = mysql_fetch_assoc($check)) {
    $_SESSION['developer'] = $row;          
  }

You're setting that session variable once so you don't need the while. You're also setting it equal to a mysql resource (which you may intend to do), but maybe you just need a column

$row = mysql_fetch_assoc($check);
$_SESSION['developer'] = $row['username']; 

Upvotes: 0

Sebas
Sebas

Reputation: 21542

just comment everything and paste at the right beginning header("Location: http://".$_SERVER['SERVER_NAME']."/index.php"); and see if it is working. If not, check for a BOM encoding.

Upvotes: 0

Jesse Kernaghan
Jesse Kernaghan

Reputation: 4634

As scott said, you need the exit() or die() after the header.

Also, check that there are no empty spaces before or after your opening and closing php tags, like this:

   <?php

That will cause header functions to fail, as it is considered input. Any related include files should be checked as well.

Upvotes: 1

Scott Saunders
Scott Saunders

Reputation: 30414

Add exit() or die() after the header line. The header function does not stop execution of the rest of the script.

Upvotes: 0

Related Questions