TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-04-2008, 12:43 PM   #1 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: Dominican Republic
Posts: 5
Thanks: 4
Reiji is on a distinguished road
Default Help with user login

Hello,

I am creating a user system and have no problems with the validation/registration...where I really have some problems is with the login part...the site i'm planing will make users stay a looooong time connected to the site and I don't know if it's better to use sessions or cookies (and I don't know how to use them either)...

Can someone please guide me in how can I make the user login part?

Thanks.
Reiji is offline  
Reply With Quote
Old 04-04-2008, 12:50 PM   #2 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 2
johnN is on a distinguished road
Default

Basically php will use cookies whenever possible for sessions. Without cookies sessions can only be parsed via url which means you loose the session when you navigate away from the site.

When you check the "remember me" button on a site, it normally places a cookie which can then be retrieved. You can set the length a cookie lasts which is handy, but the user may have them turned off, or may clear their cookies prematurely.
johnN is offline  
Reply With Quote
The Following User Says Thank You to johnN For This Useful Post:
Reiji (04-04-2008)
Old 04-04-2008, 01:02 PM   #3 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: Dominican Republic
Posts: 5
Thanks: 4
Reiji is on a distinguished road
Default

So I may basically make it with sessions unless the user choose the remember me option?
Reiji is offline  
Reply With Quote
Old 04-04-2008, 03:48 PM   #4 (permalink)
The Contributor
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
TerrorRonin is on a distinguished road
Default

The cookie would basically just be a method of storing their username and password for future use. The login should just operate on sessions.

example:

<input username>
<input password>
<checkbox remember me>

Those would just be blank, unless they previously checked remember me. Then the cookie would auto-fill those forms.

The login wouldn't be changed though, if you have a session based login. A lot of people have cookie based logins, but I prefer session strict myself.

PHP: session_start - Manual
TerrorRonin is offline  
Reply With Quote
The Following User Says Thank You to TerrorRonin For This Useful Post:
Reiji (04-04-2008)
Old 04-04-2008, 04:01 PM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Cookies are a fine way of storing login data. They are just as secure if the proper precautions are taken (as must be done with sessions). The general way I do it is this.

Cookie id
[The users ID]

Cookie pass
[The users pass]

At every page where it displays data you should be logged in to see, I have a function verify that ID and password in the database. If they match, the user is logged in. If they do not match, the user is given an error message.

Also, NEVER have the members username as the validating data, it could cause problems if there are more then one users by that name. Find the user by their unique ID.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Reiji (04-04-2008)
Old 04-04-2008, 09:01 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I wouldn't recommend storing the username and password because of cookie sniffing. However, if you're wanting to go down that route then store the session ID. Although session IDs are still susceptible to cookie sniffing, you can try your best to make it much more difficult to takeover a session that isn't yours. This is done by creating a fingerprint based on data you KNOW exists, such as the IP address. You can also add in optional parameters such as the user agent.

I have wrote us a basic class for the very purpose of resuming sessions. The cookie remains active for only an hour in this example, and so if a user doesn't login for an hour then the cookie will expire. It can be used like so:

php Code:
$pCookie = TalkPHP_Cookie::restore();

if(TalkPHP_Cookie::restore())
{
    session_id($pCookie->session_id);
    printf('Welcome back, %s', $_SESSION['username']);
}
else
{
    $_SESSION['username'] = 'Wildhoney';
    TalkPHP_Cookie::save();
    printf('Welcome, %s', $_SESSION['username']);
}

You should be able to close your browser and then return to the page. The class should pick up on the attempt to restore the session, and as long as your IP address matches the saved IP address, it will do just that.

I've not put the class through any rigorous tests. That's entirely up to you if you decide to use it ! It should say "Welcome back, Wildhoney" once the session has been restored. You won't want to restore the session on every page click and so that's where your own function would come in, to see if the user is already logged in or not.

php Code:
class TalkPHP_Cookie
{
    const COOKIE_NAME = 'talkphp_cookie';
   
    public static function save(array $aData = array())
    {
        @session_start();
       
        $aData['session_id'] = session_id();
        $aData['fingerprint'] = md5($_SERVER['REMOTE_ADDR']);
       
        setcookie(self::COOKIE_NAME, serialize($aData), time() + 3600, '/');
    }
   
    public static function restore()
    {
        @session_start();
       
        if(isset($_COOKIE[self::COOKIE_NAME]))
        {
            $szData = $_COOKIE[self::COOKIE_NAME];
            $pCookie = (object) @unserialize($szData);
           
            if(!$pCookie)
            {
                return false;
            }
           
            if(!isset($pCookie->fingerprint) || !isset($pCookie->session_id))
            {
                return false;
            }
           
            if(md5($_SERVER['REMOTE_ADDR']) != $pCookie->fingerprint)
            {
                return false;
            }
           
            return $pCookie;
        }
       
        return false;
    }
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Reiji (04-04-2008)
Old 04-04-2008, 09:11 PM   #7 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: Dominican Republic
Posts: 5
Thanks: 4
Reiji is on a distinguished road
Default

Thank you everybody...I will try and process all that information :D

And Wildhoney, as I'm just starting with classes and OOP, I'll have to read that class of yours very carefully so I can understand what does every part of it means.

I'll let you all know how it went and copy some of the code in case you guys want to make me some corrections.

Thanks again and talk to you soon.
Reiji is offline  
Reply With Quote
Old 04-04-2008, 09:25 PM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I should add that while vB also uses sessions, if you look in your cookies, your user ID and encrypted pass are in there. With those two you can effectively change your account (I've done it, see Programming Tips Blog Archive The Danger of JavaScript Links in vBulletin and other forums). That is the one thing I dont like about cookies.
__________________

Village Idiot is offline  
Reply With Quote
Old 04-04-2008, 11:24 PM   #9 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Well then. I have two edumacational questions regarding your coding style there Wildhoney, but in attempting to edumacate myself prior to asking them I seem to have broken my test server.

That's not good.

All I was trying to do was turn error reporting on in PHP so that I could tell if it was generating warnings or other notices when I accessed a class method without the class being instantiated. From the PHP manual,

Declaring class members or methods as static makes them accessible without needing an instantiation of the class.

Now forgive me if I've misunderstood this, but it would seem to call a method (someClass::method()) according to this you would need it declared as static. However, in writing some quick test.php code (my favorite file, I think there's one in almost every directory of my document tree) to see how a child class interacts with the parent, and visa versa, I had written some code in there which accessed methods and constants from my test classes previous to the class being called into existence.

I'm assuming however that since none of them were declared static, this is probably producing warnings, as it does not halt execution. So I went to turn my error reporting on, and apache won't restart when I have it set to on, but it will if I set it to off. Wtf. *kicks it*

Anywho, while I try to figure that out, I was also curious why you suppressed errors from calling session_start(). Does this sometimes have unexpected behavior?
-m
delayedinsanity is offline  
Reply With Quote
Old 04-04-2008, 11:49 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

They should be declared static if you're accessing them like so, yes. I will wait until you've further tested that, however, before we go any further so we remain on the same page.

As for suppressing warnings of session_start, I do this because of the following addition:

Quote:
As of now, calling session_start() while the session has already been started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.
Thus suppressing it will ensure no notice is shown, but will nevertheless guarantee that a session is started. However, with that said, programmers should really be ensuring it is started themselves, and so those 2 error suppressing lines -- 1 in each function, should be removed when in the capable hands of a competent programmer.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-05-2008, 12:45 AM   #11 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

There we go, I got my error reporting on. I just have to restart apache twice when I switch php.ini settings for some reason.

Strict Standards: Non-static method testClass::testing() should not be called statically

So it allows it, but it doesn't like it. Coming from a non-object orientated background, I don't fully understand everything I'm doing just yet, primarily I just really like the flow and organization of using PHP's oop so I'm learning as I go. The best (short) explanation of static I found in the past hour was from Developer.com: An EarthWeb site:

Quote:
Most applications make use of various functions of use throughout the entire application. Because such functions aren't necessarily related to any particular object, they're often placed in a general utility class. However, such a strategy is followed because it's good OO programming practice, and not because we want to invoke a "utility" object (although you could). Rather, we just want to call the method as necessary, while still managing to encapsulate it in some sort of class. Class methods that can be called without first instantiating an object are known as static.
This and a few pages back and forward from it have helped clarify a few matters.
-m
delayedinsanity is offline  
Reply With Quote
Old 04-05-2008, 01:02 AM   #12 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

So it would seem you can call a method statically, if it's not declared static, and it produces a warning, as posted above. However, if you declared the method static, and then call it regularily (with ->) it doesn't?
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


All times are GMT. The time now is 02:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design