More secure? Sessions or Cookies?

Discussion in 'PHP' started by OnlinePerson, Nov 5, 2007.

  1. #1
    Hi,

    I want to keep a users e-mail address and password in a cookie, but heard a "session variable" is the preferred way to do that. Supposedly it's more secure.

    Does anyone have some expert advice for me? I want the visitor's information to be safe.

    Thank you.
     
    OnlinePerson, Nov 5, 2007 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    I use session handling, for the reason, that if the
    session variable is != "" it will work globally until the script destroys the session.

    example:

    page1.php
    session_start();
    .....
    $_SESSION['varName'] = "name";
    ......
    redirect to page2.php

    page2.php
    session_start();
    echo $_SESSION['varName'];

    that will hold the session until you came to session_destroy();
     
    bartolay13, Nov 5, 2007 IP
  3. iteamweb

    iteamweb Active Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    using sessions is a best way to accomplish this .
    u can start a session ans destroy it if u need .
    its a good way to make use of .

    session_start();

    session_destroy();

    $_session["variable"]="exapmle";

    echo $_session["variable"];


    $_session["password"]=md5($_post[password])
     
    iteamweb, Nov 5, 2007 IP
  4. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Its not really a good idea to keep the user name and password anywhere unless its encrypted (say md5(password)).

    Just store a unique identifier as a session variable and keep delicate information in the database.
     
    tonybogs, Nov 6, 2007 IP
  5. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sessions definitely! To be secure you should not store the password in the session (or even in the database). In sessions just keep track of the state of this session: 'anonymous' or 'authenticated'. If the state is 'authenticated' the userId should also be stored in the session.

    When storing passwords in a database I don't like the plain md5($password) hash because it can be attacked quite easily by brute force and/or dictionary attacks. I prefer using a salted hash, in which the password is concatenated with a secret string before being hashed: md5($password.$secret). Now a brute force attacker can find strings for which the md5($string) matches the stored hash but can not use them to log in.
     
    mvl, Nov 6, 2007 IP
    iteamweb likes this.