Header redirect problem

I am having a problem with header redirect on my login page. All was well until I upgraded from php 5.6 to 7.1 yesterday.

The user can enter credentials on a login form, and based on the creds, the user is validated against the database and the login also looks for a cookie on the user’s browser. All I said, all was well until yesterday, the upgrade, and my attempts to migrate to mysql that works (my bad for being years behind the curve).

Anyway, several files (config, variables, error codes, and a login cookie check) are called by include statements prior to any actual php being executed. As I said, i am years behind and am strictly procedural.

Through trial and error, I can get the page to redirect as long as I put a redirect before an included file called login.cookie.check.php. As soon as I put the redirect after that file, redirects fail. I have no clue what in that file is outputting html, but it is. I’ve also tried ob_start to no avail.

Here’s the code from that file:

<?
$remembered = "0";
if (isset($_COOKIE['userkey'])) { // check for a 'key'
    $userkey = $_COOKIE['userkey'];
    if ($get_username = mysqli_query($link, "SELECT username FROM userkeys WHERE userkey='$userkey' LIMIT 1")) { // there is a cookie, lookup username
        while ($row = mysqli_fetch_assoc($get_username)) {
            $username = $row["username"];
            $remembered = "1";
        }
        $get_userinfo = mysqli_query($link, "SELECT * FROM users WHERE u_employee_number='$username' LIMIT 1"); // check if the user info validates the db
        while ($row=mysqli_fetch_assoc($get_userinfo)) {
            $u_name = $row["u_name"];
            $u_email = $row["u_email"];
            $u_text = $row["u_text"];
            $u_employee_number = $row["u_employee_number"];
            $u_jpass = $row["u_jpass"];
        }
    } 
}
?>

Any ideas?

Thanks in advance.

Well, first, your code is oddly formed. If you are selecting one record from the database, you do not need to loop thru it, the one record, using a while loop. Waste of time. Just do the one fetch for each query. And, if you alter the second query to just get those five fields, you can use the extract function to place them into their variables of the same name. Again, faster…

The code looks straightforward and should not cause any trouble with a redirect right after it.
Did you turn on full error tracking and look at your sites error logs to see what is failing? Is the code you posted the entire login.cookie.check.php file?

The main reason redirects do not work in PHP code is if data is sent to the browser before the PHP code hits the header() function. Even one single space can cause it to fail. But, I do not see any place in this code where it displays or echo’s any data.
Normally, if a redirect fails it is due to another error. Or a badly spelled page name.

Thanks Ernie… yes I know I’m not a best practices kind of guy. That is the entire file. I’ll implement your suggestions over the next couple days. Alan

Uhhh… not sure what fixed it, but I took out the loops and removed the blank lines in the code and it seems to be working properly. Thanks much!

LOL ( what’s “best practices” ? ! ) Me neither… That just jumped out at me!

Normally, I leave the redirect where it belongs and debug the data just before it is encountered.
You code might not be getting there which would mean an error in the above code, but, I don’t see it.

Reading up on PHP.net about cookies, they must be text and so they use double quotes, not single ones.
isset($_COOKIE[“userkey”]) …
Also, they use them in their examples for when you save the cookie. Not sure if this means anything…
But, worth try…

OH, while writing this I saw you posted it is now working… Great! Glad it is working!

Sponsor our Newsletter | Privacy Policy | Terms of Service