Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get IP address that can pass current $user_ID?


  • function_im_awesome() {
    Participant

    @vegasparty607

    Hello. I am trying to put the user’s IP address in the group/members.php, or the group’s directory. I want the user listed’s IP address to show only if the user is the group admin or mod. But now what I am having trouble on, is that the IP address is the same for all users, (my IP), but I want it to be the user listed’s IP, not mine. Here is what I tried, but it’s not working.

    in my bp-cutsom.php I put:

    `function my_member_list_id() {
    global $members_template;
    return $members_template->member->user_id;
    }`

    and then in the file I put:

    ``

    I thought that would work, but it’s still the same. Any ideas?

Viewing 21 replies - 26 through 46 (of 46 total)

  • Brandon Allen
    Participant

    @cnorris23

    After seeing more of what you’re trying to do here (https://buddypress.org/community/groups/how-to-and-troubleshooting/forum/topic/how-to-display-the-user-post-count-in-topic-php/#post-96750) I should also point out, that this will only work if the person has logged in since you started using the code. What the code I gave does is to retrieve and save the user’s IP address each time they log in. If you can get in touch with someone who has an account with a different IP address than yourself, you can have them log out, the log in, and you should then see their IP address. To recap, here is the code you need in your bp-custom.php:

    `function my_get_IP( $user_id = false ) {
    if ( empty( $user_id ) )
    return false;

    $ip_address = esc_attr( trim( get_user_meta( $user_id, ‘ip_address’, true ) ) );

    if ( $ip_address = esc_attr( trim( get_user_meta( $user_id, ‘ip_address’, true ) ) ) )
    return $ip_address;
    else
    return false;
    }`

    `function my_update_user_IP() {
    $user_id = (int) get_current_user_id();

    if(!empty($_SERVER)) {
    $ip_address = $_SERVER;
    } else if(!empty($_SERVER)) {
    $ip_address = $_SERVER;
    } else if(!empty($_SERVER)) {
    $ip_address = $_SERVER;
    } else {
    $ip_address = ”;
    }
    if(strpos($ip_address, ‘,’) !== false) {
    $ip_address = explode(‘,’, $ip_address);
    $ip_address = $ip_address[0];
    }

    if ( update_user_meta( $user_id, ‘ip_address’, $ip_address ) )
    return $ip_address;
    else
    return false;
    }
    add_action( ‘wp_login’, ‘my_update_user_IP’ );`

    then add the following to your group/members.php template:

    ``

    I tied logging in, loggin out, Your code does not work, it just simply does not appear. Nothing appears.

    And I want the IP to display in the group’s member list, and topic page

    I have just about given up, seems like it’s just impossible

    idk I found one that seemed like it passed the $user_id for the comments

    `// applied to the comment author’s IP address prior to saving the comment in the database.
    function auto_reverse_proxy_pre_comment_user_ip()
    {
    if ( !empty($_SERVER) )
    {
    $X_FORWARDED_FOR=explode(“,”, $_SERVER);
    $REMOTE_ADDR=trim($X_FORWARDED_FOR[0]); //take the first element in the array
    } else {
    $REMOTE_ADDR=$_SERVER;
    }
    return $REMOTE_ADDR;
    }
    add_filter ( ‘pre_comment_user_ip’,’auto_reverse_proxy_pre_comment_user_ip’);`

    but i tried it, and the ips are still all mine.

    for some reason when i put your code in bp-custom.php it doesn’t work, so i think what is holding it up is the

    `if ( update_user_meta( $user_id, ‘ip_address’, $ip_address ) )`

    Actually, Brandon, do you have a membership at wpmu?

    Maybe you could ask this question there for further answers


    Virtuali
    Participant

    @gunju2221

    side note: Brandon said, You’ll also want to change

    return $members_template->member->user_id;

    to

    return $members_template->member->id;

    since $members_template->member->user_id doesn’t exist.

    `$members_template->member->user_id` does exist, it’s in the template-tags.


    Bigjimmysisco
    Member

    @bigjimmysisco

    I know that SMF has this feature, I found this code from it but Idk if it will help,

    `// Show the IP to this user for this post – because you can moderate?
    if ($context && !empty($message))
    echo ‘
    ‘, $message, ‘ (?)‘;
    // Or, should we show it because this is you?
    elseif ($message)
    echo ‘
    ‘, $message, ‘‘;
    // Okay, are you at least logged in? Then we can show something about why IPs are logged…
    elseif (!$context)
    echo ‘
    ‘, $txt[511], ‘‘;
    // Otherwise, you see NOTHING!
    else
    echo ‘
    ‘, $txt[511];

    echo ‘

    ‘;`

    Huh???? Is that the right code?

    doesn’t look like it


    Brandon Allen
    Participant

    @cnorris23

    Okay. I’ve finally been able to test the code I gave you. There was an issue with the retrieval of the $user_id during log in, so nothing was being saved. I’ll post below with the updated code. The code works as far as saving/retrieving/displaying the IP address. If the IP address continues to be your address, and you are using the code I give you below, then the problem is with the part of the function that determines the IP address. I have not tested whether that works. I’m trusting that you’ve done your homework and found the right one.

    These first two functions will go in your bp-custom.php file or in your themes functions.php file:

    `function my_update_user_IP( $user_login = ” ) {
    $user = get_user_by( ‘login’, $user_login );
    $user_id = (int) $user->ID;

    if(!empty($_SERVER)) {
    $ip_address = $_SERVER;
    } else if(!empty($_SERVER)) {
    $ip_address = $_SERVER;
    } else if(!empty($_SERVER)) {
    $ip_address = $_SERVER;
    } else {
    $ip_address = ”;
    }
    if(strpos($ip_address, ‘,’) !== false) {
    $ip_address = explode(‘,’, $ip_address);
    $ip_address = $ip_address[0];
    }

    if ( update_user_meta( $user_id, ‘ip_address’, $ip_address ) )
    return $ip_address;
    else
    return false;
    }
    add_action( ‘wp_login’, ‘my_update_user_IP’ );

    function my_get_IP( $user_id = false ) {
    if ( empty( $user_id ) )
    return false;

    $ip_address = esc_attr( trim( get_user_meta( $user_id, ‘ip_address’, true ) ) );

    if ( $ip_address = esc_attr( trim( get_user_meta( $user_id, ‘ip_address’, true ) ) ) )
    return $ip_address;
    else
    return false;
    }`

    The next bit of code will go within the members loop of your themes groups/members.php file:

    ``

    if you have tested it, does it work on your site?/

    When I put your updated code, nothing appears


    Brandon Allen
    Participant

    @cnorris23

    Yes I have tested it, which means it works on my site. It saves and outputs the IP address. It’s a local installation, so as I said above, I can’t speak to the validity of the IP address. I haven’t tested that portion, as I have no easy way to test.

    Where are you placing the code?

    im putting the functions in bp-custom

    then the call in groups/single/members.php


    Brandon Allen
    Participant

    @cnorris23

    Are you using the code exactly as I’ve placed it? Are you putting the html below

    but before

    ?

    How weird. Now the IP showed, but only with the admin account and the group owner account. All the others it did not appear


    Brandon Allen
    Participant

    @cnorris23

    It will likely take a while before everyone has logged in and had their IP’s updated. With cookies and general delay between an individual user’s login, not everyone will end up logging in every day, or especially multiple times a day. With the current code, the user has to log in before their IP address is saved.

    so everybody has to log in in one day for all of them to be showed? :( wow, bummer…

    why can’t we change that?


    r-a-y
    Keymaster

    @r-a-y

    You can’t change that because WordPress doesn’t save the user’s IP address by default, so you’ll have to wait until the user next logs in to your site to save the IP.

    ok, actually this looks fine so far, because it seems like once they have logged in, wordpress saves it for good.

    ray, if i put the function for the IP above, and the topic poster Id i was talking about before, it would work?


    Brandon Allen
    Participant

    @cnorris23

    Should be the same code as you use within the group members loop, just use `bp_get_the_topic_poster_id()` instead of `bp_get_group_member_id()`.

    ``

    have you tested it on your site? Like mine?

    So once the user logs in, it’s there forever right?


    Brandon Allen
    Participant

    @cnorris23

    I have not tested it. I have no way of testing it on your site. That’s why I gave the code to you, so that you could test it. It’s there forever. It will be updated each time the user logs in, so if their IP address changes, which most will, the old IP will be replace with the new IP.

Viewing 21 replies - 26 through 46 (of 46 total)
  • The topic ‘Get IP address that can pass current $user_ID?’ is closed to new replies.
Skip to toolbar