Forums

[resolved] IP address and wp database (4 posts)

  1. sparkyjoe
    Member
    Posted 4 years ago #

    Hi guys. I'm looking for a little help with something I'm wanting to do.
    Obviously you know that when a user leaves a comment their IP address
    is stored in the WP database. I want to be able to echo "Welcome back $user" (ip address being matched to the $user) the only thing is I don't know how to check the IP address from the person visiting against the IP addresses that are in the database. I am a newb with very little experience with php and sql queries. Any help is much appreciated guys. Thanks.

  2. MichaelH
    Volunteer
    Posted 4 years ago #

    Might want to look at some plugins that keeps track of things like that.

    One possibility
    http://wordpress.org/extend/plugins/wp-useronline

  3. haochi
    Member
    Posted 4 years ago #

    <?php
    $commenter_name = $wpdb->get_var("SELECT comment_author FROM $wpdb->comments WHERE comment_author_IP = '".$_SERVER['REMOTE_ADDR']."' ORDER BY comment_ID DESC LIMIT 1");
    
    if(!empty($commenter_name)){
     echo "Welcome back, $commenter_name";
    }else{
     echo "DISPLAY SOMETHING ELSE OR NOTHING";
    }
    ?>

    Tested.

  4. sparkyjoe
    Member
    Posted 4 years ago #

    Thanks for your help haochi! Thats exactly what I was looking for.

Topic Closed

This topic has been closed to new replies.

About this Topic