I've discovered my problem.
It seems that setcookie(user, $userid, $exp) doesn't work for me. I don't know why exactly.
I had to use the setcookie command in the header for it to work.
It looks something like this:
$exp = time()+3600*12;
$date = gmstrftime("%A, %d-%b-%Y %H:%M:%S", time()+$exp);
header("Set-Cookie: user=$userid; expires=$date GMT;\n\n");