Current IP Adress

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Oct 14, 2014 12:09 pm

Current IP Adress

Post by loulou2522 »

HI,
How can i find my current IP ( Provider adress delivered by my isp and no local adress) when i use Purebasic
Thnaks in advance
BarryG
Addict
Addict
Posts: 4130
Joined: Thu Apr 18, 2019 8:17 am

Re: Current IP Adress

Post by BarryG »

Marc56us's code is better.
Last edited by BarryG on Fri Feb 21, 2020 12:28 pm, edited 1 time in total.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Current IP Adress

Post by Marc56us »

loulou2522 wrote:How can i find my current IP ( Provider adress delivered by my isp and no local adress) when i use Purebasic
Many website provide some WebService, use new PB function HTTPRequest() to query it :wink:
ie: https://www.ipify.org/

Need PureBasic Version >= 5.70 LTS (2nd January 2019)

Code: Select all

; Know my WAN IP
; Thanks https://www.ipify.org/

EnableExplicit

InitNetwork()

Define HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://api.ipify.org/")
If HttpRequest
    Debug "WAN IP: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
    FinishHTTP(HTTPRequest)
Else
    Debug "Request failed"
EndIf

End
BarryG
Addict
Addict
Posts: 4130
Joined: Thu Apr 18, 2019 8:17 am

Re: Current IP Adress

Post by BarryG »

The only risk to this is that we don't know how long https://api.ipify.org will allow parsing like that. They could stop it at any time, or limit the number of calls. Just something to be aware of. If you have your own website, you can set up your own PHP script to return the public IP, and it'll always be there for you. It's what I do.

The other risk is if https://api.ipify.org returns an error message (say if you're disconnected without realizing); so you need to check that an actual IP address is returned, instead of a message like "Couldn't resolve host 'api.ipify.org'".
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Current IP Adress

Post by IdeasVacuum »

Hi loulou2522

Do you really need your own IP Address? If so, that's fine, as discussed by BarryG, but if your App is going to be used by others, that's a different kettle of fish. Also, some ISPs provide home Users with dynamic IP addresses, so it could be different every day.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
loulou2522
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Oct 14, 2014 12:09 pm

Re: Current IP Adress

Post by loulou2522 »

BarryG wrote:Marc56us's code is better.
Wher can i find Marc56us's Code please ?
Post Reply