I think you're misunderstanding the concept of logging in and what happens after that.
In your code, how do they get the cookie in the first place so you can log them in? It's the other way around.
You first register someone by taking their chosen username and password and saving those to the database. Later, to log them in, they use login form and you take their inputted username and password and verify it matches those stored in the database. No use of sessions or cookies so far.
So if the username and password matches together in the db then they have confirmed credentials (note in this scenario you will need unique usernames, otherwise, however unlikely, if two people had the same username/password they would both access the first account in the db)
Now, if you want to check throughout your site they are logged in or not (of course) you need to set a session when you verify their credentials are good. This will store some data about them, which on each page you can check and verify they are logged in. This can be their username, whatever.
You can go to any length here, and check their IP still matches on each page etc. Ssessions are hard to hack, and likely if someone has hacked a session on a server they have access to all manner of other things anyway (it's safe to use sessions).
By default on most servers, a session will actually use a cookie as well, it's the session ID for the session stored on the server. So when you get session data for a user it gets the ID from that users sores cookie and access the relevant info stored on the server.
Don't store sensitive info in the session/cookie.
For storing their password, you want to use crypt (not md5 or sha) and use a salting method with it, such as blowfish.