SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting ID of user currently logged in

    Hello everone,

    I am buidling a small job application website, and i'm using a the basis of a login system taken from a Nettuts tutorial.
    The logging in works fine, but I am having trouble getting the details for the currently logged in user, for example if a user enters their personal details, i can process the data into the database but it's not linked to which user!
    I need a way to identify the user currently logged in, so i can update my insert statements to something like ...
    'UPDATE cv where userID = $userID'. any help much appreciated, thx in advance

    Aaron

    Here's the code i'm using to login a user:

    PHP Code:
    require_once 'includes/constants.php';

    class 
    Mysql {
        private 
    $conn;
        
        function 
    __construct() {
            
    $this->conn = new mysqli(DB_SERVERDB_USERDB_PASSWORDDB_NAME) or
                          die(
    'There was a problem connecting to the database.');
        }
        
        function 
    verify_Username_and_Pass($un$pwd) {
                    
            
    $query "SELECT *
                    FROM users
                    WHERE username = ? AND password = ?
                    LIMIT 1"
    ;
                    
            if(
    $stmt $this->conn->prepare($query)) {
                
    $stmt->bind_param('ss'$un$pwd);
                
    $stmt->execute();
                            
                if(
    $stmt->fetch()) {
                    
    $stmt->close();
                    return 
    true;
        
                }
            }
            
        }


    Let me know if you need any further info.

    Thx in advance,

    Aaron

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What do you do to remember if the user has logged in? A session variable? Just add another session variable that contains the username.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What do you do to remember if the user has logged in? A session variable? Just add another session variable that contains the username.
    Yes it is, here's the code:

    PHP Code:
    require 'Mysql.php';

    class 
    Membership {
        
        function 
    validate_user($un$pwd) {
            
    $mysql = New Mysql();
            
    $ensure_credentials $mysql->verify_Username_and_Pass($unmd5($pwd));
            
            
    // if above = true
            
    if($ensure_credentials) {
                
    $_SESSION['status'] = 'authorized';
            
                
    header("location: ../myIWC.php");
            } else return 
    "Please enter a correct username and password";
            
        } 
        
        function 
    log_User_Out() {
            if(isset(
    $_SESSION['status'])) {
                unset(
    $_SESSION['status']);
                
                if(isset(
    $_COOKIE[session_name()])) 
                    
    setcookie(session_name(), ''time() - 1000);
                    
    session_destroy();
            }
        }
        
        function 
    confirm_Member() {
            
    session_start();
            if(
    $_SESSION['status'] !='authorized'header("location: ../login.php");
        }
        

    How would i add another session variable for the user id? something like this i would guess? (near line 11)?

    PHP Code:
    // if above = true
            
    if($ensure_credentials) {
                
    $_SESSION['status'] = 'authorized';
                    
    $_SESSION['username'] = '$un';
                
    header("location: ../myIWC.php");
            } 
    thx for your speedy reply

    Aaron

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    $_SESSION['username'] = $un;

    Exactly
    No need for the quotes around $un, though

  5. #5
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you a whole big bunch so for your advice n guidance, very helpful

    Aaron

  6. #6
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jus a quick note... i suppose i can now identify users by

    $currentUser = $_SESSION['username'];

    ....update cv where username = $currentUser.

    Could i alter my code to grab the user ID field from the database?

    Thx

    Aaron

  7. #7
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know how your class works, but this query:


    Code MySQL:
            $query = "SELECT *
                    FROM users
                    WHERE username = ? AND password = ?
                    LIMIT 1";
    Looks good (I don't know what the ? mark does though),

    Anyways, you should be able to get the id from this one query.

    PHP Code:
    $_SESSION['username'] = $un;
    $_SESSION['userID'] = $however_you_fetched_the_username['id']; // just do it for the "id" row 

  8. #8
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx for your input, will test it in a moment, the ? are used to bind parameters as my db queries are done using prepared statements.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •