Current location: Hot Scripts Forums » Programming Languages » PHP » best way to log in users, and keep them logged in


best way to log in users, and keep them logged in

Reply
  #1 (permalink)  
Old 01-11-06, 09:35 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
best way to log in users, and keep them logged in

my tools are php and mysql.
i want users to be able to log in to a preregistered account. they whould be able to autologin after the first time.

how do i make an optimal login function, how do i keep it secure and prevent users from logging into others accounts?

also, how do i keep the users logged in between reloads?

how is it usually done? do websites save username and password in a $_COOKIE locally on users machine? is this cookie matched between each reload or is it better to use $_SESSION? and if sessions should be used, is it better to use "cookie" sessions or "url" sessions?

all tips on security and ease of use is welcome. my users will have an ID, username and password. IDs and usernames are unique (no two users can have the same one).


thank you
Reply With Quote
  #2 (permalink)  
Old 01-11-06, 09:36 PM
Vineman Vineman is offline
Wannabe Coder
 
Join Date: Dec 2005
Location: Texas
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Cookies are the way to go.
Reply With Quote
  #3 (permalink)  
Old 01-11-06, 09:54 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, but would you like to elaborate? use straight cookies and no sessions? write to cookie for each reload? is that what forums etc are doing?

any words of wisdom on security?
Reply With Quote
  #4 (permalink)  
Old 01-12-06, 09:50 AM
Critical's Avatar
Critical Critical is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
I am using cookies and session.

Cookies for "Log in me next time"
Session for "Don't log me next time"

__________________
Help me with PHP
Reply With Quote
  #5 (permalink)  
Old 01-13-06, 09:34 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello

So, first of all, cookies are the most widely used means of conweying inter-request data so that most sites use them to keep logged user info. Secondly, they just store the user login or user ID in the session vars so it is sufficient to do a lookup on the user ID. Third, the session IDs are strong enough that will make brute force efforts effectless compared to the short session lifetime.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #6 (permalink)  
Old 01-15-06, 04:08 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
does anyone have any examples of code? most people say "go with cookies", but how?

should i store a value in the cookie saying "logged_in", along with username and encrypted password? is it that simple?
Reply With Quote
  #7 (permalink)  
Old 01-15-06, 05:53 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
In my opinion Sessions (server side) are the best way to go. Since Cookies are placed on the client side (visitor's computer), it can be manipulated a lot easier (so not very safe).
__________________
Reply With Quote
  #8 (permalink)  
Old 01-15-06, 06:12 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
but if one is to offer a permanent login (such as on this site), you'd be forced to use cookies. and if cookies are safe for that purpose, shouldn't they be safe for most other purposes?

i'm not saying you're wrong, i just want to discuss it.
Reply With Quote
  #9 (permalink)  
Old 01-15-06, 06:29 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
What I am basically saying is to not allow permanent / cookie based login, since doing so is risky. However, if you were to do permanent login, then cookies are the only way to do it. Just be aware that cookie based login system are extremely easy to bypass.
__________________
Reply With Quote
  #10 (permalink)  
Old 01-15-06, 06:35 PM
Christian's Avatar
Christian Christian is offline
Community VIP
 
Join Date: Mar 2005
Location: ProgrammingTalk
Posts: 2,449
Thanks: 0
Thanked 6 Times in 5 Posts
I found this code at http://us3.php.net/manual/en/function.session-start.php
PHP Code:

session_start();

// this session will expire in 7 days
setcookie(session_name(),session_id(),time()+3600*24*7);
//                                                ^Remove The Double Space It Wont Work Right :( 
__________________
:: ImperialBB :: New version in the works! :: http://www.imperialbb.com ::

:: Have a question about the board? The Rules? An Infraction/Warning? :: Contact Form ::

Last edited by Christian; 01-15-06 at 06:37 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 06:28 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.