I need to set a cookie to keep user login state. I’m going to hash username, password and IP. My code:
login process:
$hashed = md5($username.$pwd.IP);
setcookie('userstate', $username.':'.$hashed);
restore user state:
$vars = split(':', $_COOKIE['userstate']);
pseudo: get user with username in $vars[0]
$hashed = md5($username.$pwd.IP);
if($hashed == $vars[1]) return true;
else return false;
Is this way safe with XSS attack?
A XSS attack is only possible when you are outputting content to the client. Because you aren’t, it’s not possible.
Another attack vector is SQL injection. You cannot trust the input of the $_COOKIE values. So you would have to escape it when you are trying to get the information from the database.