I'm building a users system plugin for a framework but the way I've currently set it up does not satisfy me. When the 'remember me' box is checked I create a cookie
setcookie('rmb', md5('salt'.$id), ...);
There are a few things I don't like about this. When I recreate a session from this cookie I do the following
$db->prepare('SELECT id FROM users WHERE md5(CONCAT("salt",id)) = ?')
->execute([$_COOKIE['rmb']])
->fetch();
Which seems alright but if I explain the query this is what I get
Highly inefficient, may run for hours, potentially. Apart from hashing with md5 being extremely insecure this system really doesn't seem reliable. Could you guys give me some pointers on how I can identify a user from a remember-me cookie hash efficiently and securely?