IP of the server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vijayanand16
Forum Newbie
Posts: 12
Joined: Fri Jan 30, 2004 4:57 pm

IP of the server

Post by vijayanand16 »

How to get the ip address of the server from the PHP code
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

You could just open a socket and retrieve the information from http://www.whatismyip.com and then parse through that information to get your IP.
vijayanand16
Forum Newbie
Posts: 12
Joined: Fri Jan 30, 2004 4:57 pm

Post by vijayanand16 »

How i get to know the server name?
Last edited by vijayanand16 on Fri Jan 30, 2004 5:50 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

A MUCH MUCH easier method is to use the built in $_SERVER array.

$_SERVER[SERVER_ADDR] should get you what you need. Check the PHP manual at: http://ca.php.net/reserved.variables for more info.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
vijayanand16
Forum Newbie
Posts: 12
Joined: Fri Jan 30, 2004 4:57 pm

Post by vijayanand16 »

I need IP of the server , not the client's IP
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ya, I know. $_SERVER[SERVER_ADDR] will get you the server address. $_SERVER[REMOTE_ADDR] will get you the client's ip. Run this command in a PHP page and you'll see what I mean: "print_r($_SERVER);". That will dump $_SERVER to your browser.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

I looked on that page, but couldn't find the array for it. And, in my opinion, the only actual way to find your external IP, is for another machine to tell you what it is.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

My fault. That particular index (SERVER_ADDR) is not listed on the page I mentioned. However, when you print out the server vars, you WILL see it. I don't know what else to say. The only problems I can see is if you're running PHP from the command line, or if for some reason, your server doesn't create certain variables. Beyond that, it's there.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Check what's in the $_SERVER array by doing:

Code: Select all

echo '<pre>';
print_r($_SERVER);
echo '</pre>';
Mac
vijayanand16
Forum Newbie
Posts: 12
Joined: Fri Jan 30, 2004 4:57 pm

Post by vijayanand16 »

Thanks
Post Reply