tags:

views:

172

answers:

2

Which one is the better way to handle login in PHP?

#1 PHP.net

    $email = $_POST['email'];
    $password = $_POST['password'];
    if($user->connection($email,$password)){ // user logging validation
        session_start();     //start the session
        $_SESSION['user_logged'] = true;  // user logged in
        header('location : control_panel.php');  // go to control panel
    }
    else {  // go back to logging page
        header('location : logging.php?' . $user->error_string);
    }

#2 Me after Paul Dixon's improvements and Sebasgo's improvements

 if (isset($_REQUEST['email'])) {
     $result = pg_prepare($dbconn, "query22", "SELECT passhash_md5 FROM users
         WHERE email=$1;");                                             
     $passhash_md5 = pg_execute($dbconn, "query22", array($_REQUEST['email']));                 

     session_start(); 
     $_SESSION['logged_in'] = false;                                                                                                           
     if ($passhash_md5 == $_REQUEST['passhash_md5']) {                                            
         $_SESSION['logged_in'] = true;                                                                   

 }
 header('Location: index.php');

The code #2 has $_REQUEST commands because I am still trying to get it work.

+1  A: 

You force all new sessions to have the same ID, which means everyone will be sharing the same session! Probably not what you intended?

You can omit the call to session_id() and just let PHP take care of creating a unique id for the session.

Also, you should really call session_start before using the $_SESSION array to ensure it is populated with any current session variables.

Paul Dixon
Thank you for pointing the two problems out! - I updated the code #2 in the question.
Masi
OK, now what is the purpose of the SID session variable - it appears to be a count of how many times a user has successfully logged in with a single session.
Paul Dixon
+1  A: 

You shouldn't try to manage the session ids yourself. A simple scheme like the one you propose (incrementing the session id by one for every new session) contains a serious security issue: A user with freshly generated session id can trivially guess other valid session ids by trying ids slightly smaller than its own. Thus it is very easy two acquire someone else's session.

If you let PHP manage the generation of new session ids for you, PHP uses a pseudo random generator to create the new ids, which will be hard to guess for a potential attacker. This prevents the above outlined attack scenario effectively.

Additionally, you will virtually never want to access $_SESSION before calling session_start() because before the session array will be always empty. Therefore your test of empty($_SESSION['SID']) will always raise false.

Bottom line: I strongly recommend you to stick to the simple way of managing login like PHP.net does it.

sebasgo
Thank you for pointing out the problem with `random generation of login_id`s! Which code do you mean have the random-generation of ids in PHP.net? - I opened a new thread about the function in the code #1 which I do not understand completely http://stackoverflow.com/questions/1266847/to-understand-a-line-of-php-code-about-a-connection-and-arrays
Masi
PHP.net doesn't do anything fancy, it just calls session_start(). Then PHP checks if the user has already an session or creates a new one. If PHP decides to it needs an new one it creates an session id for it. This one is generated using a pseudo random generator, usually the one provided by the underlying operating system.
sebasgo
Another note: The start (pg_* bits) of you login code is okay and works probably on the same lines as the PHP.net way. All you have to do get a working solution is to remove the superfluous lines referring `$_SESSION['SID']`.
sebasgo