2

Annoyingly my log in check script is not redirecting where I tell it to. I know it hits the if statements correctly because I put in echos to check it did. It is blatantly skipping to header as during the first circumstance it echos "- blanks" as it should then also "Did it even get this far?"

So on that basis I know it hits the header - its just plain ignoring it and I can not for the life of me fathom why.

Im sure its simple and I'm missing something ridiculously obvious but I just cant see it. Any suggestions?

// makes sure they filled it in 
if($_POST['frmLogin-username'] == "" || $_POST['frmLogin-password'] == ""){
    echo " - blanks";

    header('Location: ?page=landing&action=login&message=1');
    echo "Did it even get this far?";
    die;
}
else{
    // checks it against the database
    $query = mysql_query("SELECT * FROM shops WHERE shopUsername = '".$_POST['frmLogin-username']."'");

    //Gives error if user dosen't exist
    $count = mysql_num_rows($query);

    if ($count == "0"){
        echo " - no user";
        header('Location: ?page=landing&action=login&message=2');
        die;
    }
    else{

        while($row = mysql_fetch_array( $query )){

            //gives error if the password is wrong

            if ($_POST['frmLogin-password'] != $row['shopPassword']){
                echo " - wrong pass";
                header('Location: ?page=landing&action=login&message=3');
                die;
            }

            else{

                // if login is ok then we add a cookie

                $hour = time() + 3600;

                setcookie(shopusername, $_POST['frmLogin-username'], $hour);
                setcookie(shopid, $row['shopId'], $hour);

                //then redirect them to the shop panel
                header("Location: ?page=shop");
                die;
            }
        }
    }
}

EDIT: The issue was to do with the way I load all my pages within index.php by calling includes which I am now investigating I have moves this process page to its own php file and it now works fine

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
TomKDev
  • 61
  • 10
  • 1
    You can't send headers once you've sent text content to the browser - any echos or whitespace will prevent the page from redirecting. – Sam Dufel Mar 29 '12 at 20:08
  • 1
    Also, according to the HTTP specifications, you must provide a full URI to Location, not a relative path. (Though that's likely not the problem as any modern browser will tolerate that.) – Corbin Mar 29 '12 at 20:10
  • The echos are not normally there - they were added for testing – TomKDev Mar 29 '12 at 20:13
  • So, uhm, where are you redirected instead? And what is the exact `wget -S http://example.org/yourscript.php` output? Prove us that the headers are there, and which ones. Otherwise: – mario Mar 29 '12 at 20:27
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – mario Mar 29 '12 at 20:28

3 Answers3

2

The issue was to do with the way I load all my pages within index.php by calling includes which I am now investigating I have moved this process page to its own php file and it now works fine

TomKDev
  • 61
  • 10
2

First of all: you can not send headers after having output anything using echo like Sam said in his comment.

Secondly, to send a redirect, the URL after the Location: must be absolute, like http://localhost/page/to/redirect/to.php.

EDIT
Corbin actually beat me to my answer for about 10 seconds ;-)

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I have used an absolute URL and dont normally have the echos there (they were added to check my code was hitting the IF statements correctly and just were not removed pre posting here) Even with absolutes it still was not working – TomKDev Mar 29 '12 at 20:19
1

You can use window.location, just echo it within PHP.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Adeel Mughal
  • 776
  • 3
  • 12