I have a password protected area on my web site and want to set a cookie once users login into the area. However, I'm wondering what to include in the name and value fields of the cookie. I thought that I could set the name of the cookie to the username and then the value would be "1" if the user had previously logged in. To authenticate the user, I would just check to see if the value were 1. However, it occured to me that other users could gain access to the website by just spoofing a cookie with a particular username and setting the cookie value to 1.
My next thoughts were to assign each user a random number and track this number in a database. I would then set the cookie name to the username and the value to the random number. Each time I would read the cookie in, I would check it in the database if the username and "number" were correctly associated with each other. However, this is going to increase the load of my db because every pageview is now going to require a database check.