How to get server ipaddress

Hi

i want to retrieve the server ip address, i have used this code for getting server ip address


echo $_SERVER['SERVER_ADDR'];

this return local ip address instead of the displaying ip address in the url.

why ? can any one tell the reason… to solve this

if the url contain the 12… means it return local 10…

i want 12… .

Thanks for your answer deathshadow60. i will try this approach.

that is the ip address assign by some isp provider like if we go to What Is My IP Address? Lookup IP, Hide IP, Change IP, Trace IP and more… it will show the ip address provided by isp. my local ip 10.0… if i run my script under my local ip it return 10.0… i want to return ip address provided by isp.

That statement still makes no sense… I’m wondering if we have a terminology issue or something here. What do you mean “run it on my local IP”? Are you talking a local server like XAMPP or something? Something on your WAN that isn’t exposed on the other side of your router?

Either 1) you have the most bizzare hardware setup I’ve ever heard of, 2) you’re failing to grasp how this stuff works – or 3) I’m COMPLETELY missing your meaning entirely.

Wait, are you saying the public IP address isn’t the one the server is at? that’s screwy… behind a home router or something?

If you are accepting requests by IP address, you could always pull it from REQUEST_URI instead.

$domainPath=explode(‘/’,trim(str_replace(‘http://’,‘’,$_SERVER[‘REQUEST_URI’]),‘/’));

$domainPath[0] should then be either the IP address OR the domain name depending on the request type . If you NEED the IP address, test if it’s a valid IP addy (another explode and four is_numeric?), if it isn’t, then call gethostbyname.

that is i need the public ip address of the server ?how do i get it can anyone help me

thank you for your reply

gethostbyname is work when we know domain name. if the url contiains 123… how to i get ip address =123…

if i use $_SERVER[‘REMOTE_ADDR’] it will return local ip of the system, i want ipaddress which is displaying in the url bar

Query the DNS. PHP: gethostbyname - Manual

yes i want the domain name ip address how to i get it?

You are getting a local IP address because you are connecting to the server from a local IP address.
I assume you are wanting the IP address of your domain name, correct?

Huh? “in the url”?

Could you explain better? I can’t even make sense out of your post.

SERVER_ADDR should be giving you the IP address of wherever that PHP code is being run – aka the server it’s being run from/on, not the client… The phrase “IP address in the URL” makes no sense whatsoever…well, unless you’re calling the IP addy instead of by domain name…