Jump to content

Best way to handle user login


Aegidius

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.