How do I grab the session ID or user id of a logged in user?

Discussion in 'PHP' started by mynameisdi, Mar 23, 2009.

  1. #1
    So from my existing code, I can tell that on some occasions the code grabs the users ID with

    a) <?if($comments[$i]['uid'] == user_id){?> - user_id in this case is what I need
    b) $_SESSION[user][id]

    But the problem is that none of these work when I display them in other parts of the code.

    Basically the session user ID here is the number of the registered user (when logged in) in my database

    And I need to grab that number.

    Perhaps I wasn't displaying a) right? I tried $user_id, 'user_id' in echo's, didn't work.

    I can't believe how helpful this forum has been to me and really appreciate all the help from you guys. You're the best.
     
    mynameisdi, Mar 23, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    try
    1) print $_SESSION['userid'];
    2) print_r($_SESSION); //this one will print an array with every values of session
     
    crivion, Mar 23, 2009 IP
  3. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Whoah, that helped.

    I don't know why, but echo didn't display $_SESSION['id'] - but this one did

    and I found the [id] variable through the second print

    Very helpful

    (for google)

    If you want to display all of your sessions,
    print_r($_SESSION);

    Thanks a lot man, I hope this helps others :)
     
    mynameisdi, Mar 23, 2009 IP
  4. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, this is weird

    It was working fine, but after I logged in from another computer, it just vanished.

    print_r($_SESSION); still displays what I need

    but print $_SESSION['id'] doesn't work anymore

    wtf? 0_o
     
    mynameisdi, Mar 23, 2009 IP
  5. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #5
    I'm not sure, please check what's it value into print_r array
    If not, double check things I dont know, try logging with different username on other computer, and put an error_reporting(E_ALL); line to showup errors
     
    crivion, Mar 23, 2009 IP
    mynameisdi likes this.
  6. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Strange.

    So at first it worked through $_SESSION[id]

    Then it stopped working.

    But now it works through the initial $_SESSION[user][id]

    I have no idea how that worked out, but at least it works :)
     
    mynameisdi, Mar 23, 2009 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Wild guess - different $_SESSION-ids for admins, moderators, user etc.? Dunno if that might be a solution...
     
    PoPSiCLe, Mar 23, 2009 IP
  8. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #8
    good point dude
     
    crivion, Mar 23, 2009 IP
  9. freebiefinder

    freebiefinder Peon

    Messages:
    431
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Will try it thanks alot
     
    freebiefinder, Mar 24, 2009 IP
  10. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think that bug occured because I've logged in from another computer on the same network (IP). My website checks both IPs and Cookies.

    IPs were a match, cookies weren't, so it kind of messed itself up for some odd reason. But adding [user] to the mix fixed it.

    Although I didn't bother with cross-browser testing. Screw IE. If my users don't use FF, they can go die xD
     
    mynameisdi, Mar 24, 2009 IP
  11. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #11
    Try storing the session ID as 'loggedin' and add a value of 1 to the field in the db, and calling the user id from where the user is logged in. GET_$userid WHERE $userloggedin = 1
     
    scottlpool2003, Mar 24, 2009 IP