Creating a UserID Variable in the Session

Discussion in 'PHP' started by devo10, Jun 30, 2010.

  1. #1
    OK guys

    Maybe my first question was not very clear, sorry for that. So here’s my problem.

    Situation:

    MySql Database contains: Username, Password & UserID

    When the user logs in, their username is checked by the session and registered in the session variable. But I think this is captured from the text field when they login.

    When the user makes a comment or other interaction with the site, their user name is logged from the session with the comment.

    I would also like to have their UserID logged from the MySql Database.

    How can I do this?

    Thanks in advance
     
    devo10, Jun 30, 2010 IP
  2. Triggs

    Triggs Active Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    Well, I'm a little unclear about what you're asking but I'll give it a shot.

    You can create a new table with two fields, userid / log.
    Insert the users latest action in the log.

    So, for example if someone posts a comment, do something like..

    
    // make sure to make the mysql connections first.
    mysql_query("INSERT INTO UserLogs (id, log) VALUES ('$useridvar', 'Posted Comment')");
    
    PHP:
    If you mean you want to have a user verified through MySQL with their password and username, then try this;

    
    $user = $_SESSION['userid'];
    $pass = $_SESSION['pass'];
    
    // make sure to make the mysql connections first.
    $result = mysql_query("SELECT * FROM users WHERE id = '$user' AND password = '$pass'");
    if (mysql_num_rows($result) == 0){
        echo 'Invalid User.';
    }
    
    PHP:
     
    Triggs, Jul 1, 2010 IP
  3. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    Triggs

    First off, thanks for responding, it’s much appreciated.

    What I really need, to capture, is the UserID. This is stored in the MySql user database.

    When they register, it is created then. It stays there and the user has no interaction with it. So they never type it, or even know it.

    I need it to be available to the session so that my system can capture it as they interact with the site.

    I think it needs to be done with something like:

    SELECT userid FROM user DB, WHERE username = $username

    Then I need some way of capturing it.

    Is this clearer?

    Sorry for the confusion.

    Thanks
     
    devo10, Jul 1, 2010 IP
  4. Triggs

    Triggs Active Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    Oh, yes.

    
    session_start();
    
    // make sure to do mysql connections and such before this
    $result = mysql_query("SELECT * FROM users WHERE username = '$username'");
    $_SESSION['userid'] = $result['userid'];
    
    PHP:
    Hope this is what you wanted.
     
    Triggs, Jul 1, 2010 IP
  5. jam4517549

    jam4517549 Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    I always includes UserID along w/ some other important stuffs like real IP, timestamp, on sessions, instead of actual usernames (considering you set the username as candidate key on your table). that is why u have primary key (UserID)on your tables, it represent the entire row
     
    Last edited: Jul 1, 2010
    jam4517549, Jul 1, 2010 IP
  6. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #6
    Thanks Triggs

    I'm trying to get my head around this a bit more. The code you gave looks like it will probable work. I just need to find out where to put it. I'm using a login system that has lots of files used to make it work and it is also object oriented.

    I'm gonna give it a try now and I'll get back to you to let you know how I went.

    cheers
     
    devo10, Jul 1, 2010 IP