• Resolved Ali

    (@ali1024)


    Hello,

    I face a problem to get the real IP for the customers when they make a new order because I’m using the CloudFlare I put this inside wp-config:

    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
    }

    It’s Work for the Real IP for comments only, I tried this code inside function.php but it’s not working:

    add_filter('woocommerce_get_customer_ip_address', 'get_customer_real_ip', 10, 1);

    function get_customer_real_ip($customer_ip) {
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $customer_ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
    }
    return $customer_ip;
    }

    Please help to Get Real IP for the customers

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hey there, @ali1024! Thanks for contacting us.

    While we can’t provide support for code customization as per our support policy, we do our best to offer advice and direct you to appropriate resources.

    You can visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there too.

    I’m going to leave it open for a bit to see if anyone is able to chime in and help you out further.

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

    Stef

    (@serafinnyc)

    Hello @ali1024 Not sure where you got that snippet from but that wouldn’t really do the job for most instance. What’s your ultimate goal here? To display the real IP in the order details section? And you yourself being on CF has nothing to do with getting their real IP. Unless, I’m just not understanding your need.

    Anyone can use a VPN and hide behind it.

    Thread Starter Ali

    (@ali1024)

    Thank you for reply @serafinnyc Please see this Image:

    I want to Get the Real IP Address for the Customers when they make a new order.

    172.69.199.180 this IP belong to CloudFlare.

    Stef

    (@serafinnyc)

    I get that @ali1024 ,but this goes back to what I was iniquity saying. Anyone can hide behind a VPN. You cannot get the real IP without a court order. People mask their real IPs for security reasons.

    That being said your question is outside the terms of this forum. We can only help and assist with WooCommerce core related issues. This seems more like a legal matter than anything else.

    Thread Starter Ali

    (@ali1024)

    Hello @serafinnyc Not all customers use VPN service, please do you have a solution?

    Stef

    (@serafinnyc)

    Hi again @ali1024 If a customer isn’t using a VPN then their ISP provided IP (their real IP) is being served. Simple as that. Anything else is masked behind a proxy which you cannot obtain.

    • This reply was modified 7 months, 3 weeks ago by Stef.
    yolocalhost

    (@yolocalhost)

    1. Update functions.php with the following code:

    // Use the real IP address for customers when creating an order
    add_action('woocommerce_checkout_update_order_meta', 'set_real_customer_ip', 10, 2);

    function set_real_customer_ip($order_id, $data) {
    // Check if Cloudflare provides the real IP address in the HTTP_CF_CONNECTING_IP header
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $real_ip = sanitize_text_field($_SERVER['HTTP_CF_CONNECTING_IP']);
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // Fallback to another common proxy header
    $real_ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']);
    } else {
    // If neither header is set, use the default REMOTE_ADDR
    $real_ip = sanitize_text_field($_SERVER['REMOTE_ADDR']);
    }

    // Save the real IP address in the order meta under the '_customer_ip_address' key
    update_post_meta($order_id, '_customer_ip_address', $real_ip);
    }

    1. Hook woocommerce_checkout_update_order_meta:
    ? This action runs whenever a new WooCommerce order is created during the checkout process.

    2. Retrieve the Real IP Address:
    ? First, it checks if Cloudflare’s HTTP_CF_CONNECTING_IP header is available and uses it.
    ? If not, it falls back to HTTP_X_FORWARDED_FOR, which is used by other proxies.
    ? If neither header is present, it defaults to REMOTE_ADDR (the server-reported IP address).

    3. Save the Real IP Address:
    ? The real IP address is stored as order metadata under the key _customer_ip_address.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.