14 Replies - 1353 Views - Last Post: 03 November 2008 - 12:47 AM Rate Topic: -----

#1 ajaycode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-August 08

ip address

Post icon  Posted 02 November 2008 - 09:58 PM

Quote

how to find server ip address using php
i want solution
please help me
thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: ip address

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,309
  • Joined: 18-April 07

Re: ip address

Posted 02 November 2008 - 10:27 PM

The server's ip can be found using the $_SERVER["SERVER_ADDR"] global variable. That will give you the IP address of the server which the script is running on.

You can find about this and more at the appropriate PHP page.

PHP: $_SERVER - manual

Keep in mind that we also don't just hand out code. You are going to have to show some work in the future as a "best effort" at solving a problem. Just to let you know before you attempt to do a "give me the code please" style post.

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#3 ajaycode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-August 08

Re: ip address

Posted 02 November 2008 - 10:38 PM

View Postajaycode, on 2 Nov, 2008 - 09:58 PM, said:

Quote

how to find server ip address using php
i want solution
please help me
thanks in advance



Quote

thanks for your reply sir
but i did checked $_SERVER[''SERVER_ADDR']
is shows undefined index

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,309
  • Joined: 18-April 07

Re: ip address

Posted 02 November 2008 - 10:44 PM

Well make sure you are using two double quotes or single quotes (I noticed you had $_SERVER["SERVER_ADDR'] <--- notice the single quote).

Also make sure you are spelling it correctly and that you are putting in an underscore.

Also if you are running PHP on a windows machine with IIS 6, there will be no $_SERVER["SERVER_ADDR"]. In that case you can use...

<?php
     $ipAddress = gethostbyname($_SERVER['SERVER_NAME']);
?>



Hope that helps.

This post has been edited by Martyr2: 02 November 2008 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 engale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

Re: ip address

Posted 02 November 2008 - 10:52 PM

You may have a lower version of php try $HTTP_SERVER_VARS['SERVER_ADDR'];
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: ip address

Posted 02 November 2008 - 11:14 PM

Just to be sure, you are asking about the ip address where the php page is being hosted, & not the visitors ip address, yes?
Was This Post Helpful? 0
  • +
  • -

#7 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: ip address

Posted 03 November 2008 - 12:09 AM

well
as i am also having the similar problem, i just want to join this topic.

actually i want to get the ip address of the client who is visiting my page.

currently what i am trying is,
<?php  
	 $ipAddress = gethostbyname($_SERVER['SERVER_NAME']);  
	 print $ipAddress;
?>															  


but this giving my system's lan address.i.e
i am in a network of 3 systems and i am connecting to net through a router.
so i am not getting proper ip. instead i am getting my lan address like
192.****

when i tried in whoami site,
it is showing my real ip.

but above code showing lan ip.

so is there any way for me to track real ip

because, the same can happen if some one from an internet cafe visits my page. then i wil get his lan ip only and not real ip which is useless to save.

any help pls...
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: ip address

Posted 03 November 2008 - 12:13 AM

View Postkummu4help, on 3 Nov, 2008 - 02:09 AM, said:

so is there any way for me to track real ip

<?php
echo ' Client IP: ';
if ( isset($_SERVER["REMOTE_ADDR"]) )    {
    echo '' . $_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    {
    echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    {
    echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' ';
}
?>



Was This Post Helpful? 0
  • +
  • -

#9 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: ip address

Posted 03 November 2008 - 12:20 AM

Hi no2pencil,
still i am getting my lan ip only.

Quote

i am using windows xp, IIS6, php5.2.x


is there any other work around?

thanks
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: ip address

Posted 03 November 2008 - 12:27 AM

Are you viewing this on the same machine that you are serving the PHP code on? The same machine as IIS is running?
Was This Post Helpful? 0
  • +
  • -

#11 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: ip address

Posted 03 November 2008 - 12:31 AM

yes
i am running the code on my machine only.
and i am viewing the dic site on the same machine on which i am running the script

This post has been edited by kummu4help: 03 November 2008 - 12:31 AM

Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: ip address

Posted 03 November 2008 - 12:34 AM

View Postkummu4help, on 3 Nov, 2008 - 02:31 AM, said:

yes
i am running the code on my machine only.
and i am viewing the dic site on the same machine on which i am running the script

You'll need to view the site from another network. I don't believe it will work internally.

Check the following page : https://www.akroncdn...tup/real_ip.php
Was This Post Helpful? 0
  • +
  • -

#13 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: ip address

Posted 03 November 2008 - 12:38 AM

Yes
the site u pointed me is showing my real ip.
but why my code is showing my lan ip.
can u explain a bit pls... and what code that site might be using...?
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: ip address

Posted 03 November 2008 - 12:42 AM

I pasted the same code that I am using on that site...

Quote

26# cat real_ip.php
<?php
echo ' Client IP: ';
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
echo '' . $_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' ';
}
?>


As far as the web server is concerned (IIS) that ip address is a routable address for your local traffic. To my understanding, since the client is on the LAN, that's what you'll get for an external address. The external routable (real) ip address will not come into existence unless it is coming in through your routable interface. If you want to test it you will need to view it from outside the network. That's all there is to it.
Was This Post Helpful? 0
  • +
  • -

#15 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: ip address

Posted 03 November 2008 - 12:47 AM

Thanks
no2pencil. i wil try to test it from my hosting account
thanks for help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1