1

I don't know what's going on with my cookies, I basically have a system that generates a random code, stores it in a cookie and also in a database. When the user logs back in, along with username/password authentication, this cookie has to match the database too in order to pass through the login phase (it's basically a system to authorise certain machines to access).

The problem is: Basically, I have set up a method to check the cookie data before I log in, it's just a page that prints the cookie data that I can access without logging in. This is located in '/includes/check_cookies.php'. I also have the main login script, that is meant to read the cookie data before anything and see if the 'auth_code' matches the database entry, if it does, allow access providing the username and password is correct etc etc, if it doesn't, it will generate a new 'auth_code' and store it in a table and the cookie. This is based on whether the user is an admin or a standard user, admin's can auth themselves there and then, standard users can't. Say i log in for the first time right now, I'm an admin, so can authorise myself, and it works a treat, i can logout and back in without a hitch. BUT, if i then went home tonight and came back tomorrow morning, attempted to log in, I would be told that my computer isn't authorised again, even though, when checked, the cookie entry matches the database within my check_cookies file, but a new one is generated in the login script? I'm seriously baffled.

But anyway, here's the relevant code (I will use the admin code):

$auth_code = $_COOKIE['auth_code'];
echo $auth_code . "<br>";

// Check Cookie Auth Code Vs Database Auth Code
$auth_query = "SELECT * FROM authorised_computers WHERE auth_code = '".$auth_code."';";
$auth_result = mysqli_query($link,$auth_query);
if($auth_result->num_rows == 0) {
    $auth = 0;
} else {
    $auth = 1;
}

if($auth == 1) {
    setcookie('admin',$admin,$cookie_expire,'/');
    setcookie('login_time',$date,$cookie_expire,'/');
    $_SESSION['logged_in'] = "true";
    header('Location: ../logs.php');
} elseif($auth == 0) {
    setcookie('auth_code',$no_generator,$cookie_expire,'/');
    setcookie('admin',$admin,$cookie_expire,'/');
    setcookie('login_time',$date,$cookie_expire,'/');
    $_SESSION['logged_in'] = "false";
    header('Location: admin_auth.php'); 
}

Basically the code is setting $auth to '0' and following that route.

Below is the cookie data from the login script:

    Array
(
    [auth_code] => 13367320 // Does not match database
    [admin] => 1
    [acc_id] => 1
    [user_id] => 10001
    [login_time] => 2015/07/29 10:25:20
)

And cookie data from before the login attempt:

Array
(
    [auth_code] => 342221-32 // Matches the Database
    [admin] => 1
    [acc_id] => 1
    [user_id] => 10001
    [login_time] => 2015/07/29 10:25:35
)

Basically, the new auth code shouldn't have been generated as the cookie data already matched the Database, so should have just gone straight through.

I have a feeling that it may be an issue with the cookies expiring, but i have set the cookies to: $cookie_expire = time() + (21*365*24*60*60); so they shouldn't be. I might be missing something blatant, but then again, my understanding of Cookies isn't the greatest. Any help would be much appreciated!

Scott Thornton
  • 331
  • 1
  • 3
  • 17
  • `echo $auth_code . "
    ";` <- that's gonna break it
    – CD001 Jul 29 '15 at 10:09
  • Can you explain why? It works at the moment and outputs: 13367320.. Which is correct. – Scott Thornton Jul 29 '15 at 10:12
  • You can't send anything to the output buffer before `setcookie()` it *should* result in a *Headers already sent* error : http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – CD001 Jul 29 '15 at 10:18
  • Fair enough, makes sense, i'll give it a go, still a little puzzled to how it will work throughout the day when i'm logging in and out constantly, but as soon as a new day starts, I then have to re authorise... Also, i have error's turned on, and nothing is returned error wise. I'll remove the outputs to after the setcookie() functions and try again tomorrow. Thanks :) – Scott Thornton Jul 29 '15 at 10:23
  • Also - interestingly, since it looks like you're setting the cookie to expire 21 years into the future you could be hitting the Y2038 problem on 32-bit software : https://en.wikipedia.org/wiki/Year_2038_problem (if your server is configured a with the wrong date) ... try just setting it to 1 year. – CD001 Jul 29 '15 at 10:28
  • That's correct, I was going to put it as far as possible toward Jan 2038, but didn't want any issues with going a little too far, so just knocked a few years off. I've checked our servers dates and they all match our PC's times etc, so that's not a problem, good spot though! – Scott Thornton Jul 29 '15 at 10:36

1 Answers1

0

Upon checking my code, I found that I was re-setting the cookie further up the code when logging in, thus changing the "cookie code" so it did not match. I have since removed/re-wrote the login script and all appears to be working as it should. Just need to retest it tomorrow morning to see if the issue occurs again.

Thanks for everyones input.

Scott Thornton
  • 331
  • 1
  • 3
  • 17