Aegidius Posted April 28, 2011 Share Posted April 28, 2011 Hi all. I'm new to PHP language and I'd like to know which is the best way to handle the user login. With cookies or with sessions? Do you know some example on the web that I could inspire on? I just know how to do it in both way, I just wanna know which is the best way and like to read some code written by a pro. Quote Link to comment https://forums.phpfreaks.com/topic/235013-best-way-to-handle-user-login/ Share on other sites More sharing options...
ManiacDan Posted April 28, 2011 Share Posted April 28, 2011 Sessions are tracked using a cookie. A session is what's used to maintain a user's state through a single "visit" to your website, which can span multiple page loads. Without a session, you have no way of knowing that the user is returning to the site over and over again. Once the user closes his browser window or turns off his computer, the session is destroyed (there are other ways for this to happen as well). You can set an "auto-login" cookie to log the user back into the site the next time he visits automatically. This is a security risk though, so make sure to read up on how to properly do it so you don't end up passing dangerous data in the cookies. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/235013-best-way-to-handle-user-login/#findComment-1207786 Share on other sites More sharing options...
Aegidius Posted April 28, 2011 Author Share Posted April 28, 2011 Thanks for the answer. I have another 2 questions. If the user want to be remembered, what should I save in the cookie? Is it correct to save the user ip address in the db and the username in the cookie, so when i read the cookie with the username i can check if the ip address corresponds? If I use the session_set_save_handler, i must define where the session variables are saved? Quote Link to comment https://forums.phpfreaks.com/topic/235013-best-way-to-handle-user-login/#findComment-1207793 Share on other sites More sharing options...
ManiacDan Posted April 29, 2011 Share Posted April 29, 2011 1) Yeah, sure. Generally I save a hash of IP, user-agent, username, and unique ID in the cookie. 2) You don't need to, generally, but yes. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/235013-best-way-to-handle-user-login/#findComment-1208012 Share on other sites More sharing options...
Aegidius Posted April 29, 2011 Author Share Posted April 29, 2011 Thank you very muck for the answer. You are really kindly. Quote Link to comment https://forums.phpfreaks.com/topic/235013-best-way-to-handle-user-login/#findComment-1208055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.