Home > Software engineering >  Ipify returning website ip instead of user ip
Ipify returning website ip instead of user ip

Time:08-22

So far I’m looking into using ipify’s php api for returning an ip but every time I try it returns my website ip. I want it to show user ip instead what can I do to fix that?

Here’s the code:

<?php
    $ip = file_get_contents('https://api4.ipify.org');
    echo "My public IP address is: " . $ip;
?>

Edit:

I understand there is this way of doing it

<?php
    $ip = $_SERVER['REMOTE_ADDR'];
    echo "My public IP address is: " . $ip;
?>

but i prefer $_SERVER["HTTP_CF_CONNECTING_IP"] and doing so they dont always return 1pv4. Is there any way to make it enforce ipv4 format without modifying website settings?

CodePudding user response:

This should do it, no need for the API to access the IP address of the user:

<?php
    $ip = $_SERVER['REMOTE_ADDR'];
    echo "My public IP address is: " . $ip;
?>

Something to note though, is that this method will get whatever the IP is, so if they are using a VPN or Proxy, you will get the IP of that service.

CodePudding user response:

Nevermind i originally didnt wanna modify web settings but i secure my website with cloudflare and they give option 'Pseudo IPV4' and i set it to 'overwrite headers' and so far i havent got any ipv6 only ipv4

  • Related