Help - Search - Member List - Calendar
Full Version: Server IP Address
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
Fred Atkinson
Is there a PHP function that returns the IP address of the
Webserver (not the host)? Can you tell me what it is?


Fred

Exyle
Fred Atkinson wrote:
QUOTE
Is there a PHP function that returns the IP address of the
Webserver (not the host)?  Can you tell me what it is?


    Fred

There is a built in variable that contains the user's IP address, I

think it's $_SERVER[REMOTE_ADDR]

Philip Ronan
"Fred Atkinson" wrote:

QUOTE
Is there a PHP function that returns the IP address of the
Webserver (not the host)?  Can you tell me what it is?


Fred


I haven't tried this, but give it a go and see what happens:

$myDNS = dns_get_record($_SERVER['SERVER_NAME'], DNS_A);
print_r($myDNS);

More info at <http://php.net/dns_get_record>

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

Chris Hope
Philip Ronan wrote:

QUOTE
"Fred Atkinson" wrote:

Is there a PHP function that returns the IP address of the
Webserver (not the host)?  Can you tell me what it is?

I haven't tried this, but give it a go and see what happens:

$myDNS = dns_get_record($_SERVER['SERVER_NAME'], DNS_A);
print_r($myDNS);

More info at <http://php.net/dns_get_record

If you are wanting to use this function, make sure you read the note on
that manual page:

"Note: This function is not implemented on Windows platforms, nor does
it (currently) work on *BSD systems."

Also, it's only available from PHP 5 on.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Chris Hope
Fred Atkinson wrote:

QUOTE
Is there a PHP function that returns the IP address of the
Webserver (not the host)?  Can you tell me what it is?

What do you think the difference between the webserver and the host is?
I would think they are one and the same.

$_SERVER['SERVER_ADDR'] will give you IP address of the server the
script is running on.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Fred Atkinson
QUOTE
What do you think the difference between the webserver and the host is?
I would think they are one and the same.

$_SERVER['SERVER_ADDR'] will give you IP address of the server the
script is running on.

You're right. What I meant was client, not host. I want a
PHP function that returns the IP address of the Webserver rather than
the client machine.


Fred

Chris Hope
Fred Atkinson wrote:

QUOTE
What do you think the difference between the webserver and the host
is? I would think they are one and the same.

$_SERVER['SERVER_ADDR'] will give you IP address of the server the
script is running on.

You're right.  What I meant was client, not host.  I want a
PHP function that returns the IP address of the Webserver rather than
the client machine.

$_SERVER['SERVER_ADDR'] gives you the server
$_SERVER['REMOTE_ADDR'] gives you the client

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Fred Atkinson
On Tue, 05 Jul 2005 22:17:11 +1200, Chris Hope
<[Email Removed]> wrote:

QUOTE
$_SERVER['SERVER_ADDR'] gives you the server
$_SERVER['REMOTE_ADDR'] gives you the client

I tried the server one.

As I have the server on a private IP address on my network, it
returned a private IP address rather than the gateway. I didn't think
that would happen since that IP address was translated through my
network's NAT in my gateway router.

Is there a function that would return the gateway (public) IP
address instead?


Fred

Chris Hope
Fred Atkinson wrote:

QUOTE
On Tue, 05 Jul 2005 22:17:11 +1200, Chris Hope
<[Email Removed]> wrote:

$_SERVER['SERVER_ADDR'] gives you the server
$_SERVER['REMOTE_ADDR'] gives you the client

I tried the server one.

As I have the server on a private IP address on my network, it
returned a private IP address rather than the gateway.  I didn't think
that would happen since that IP address was translated through my
network's NAT in my gateway router.

Is there a function that would return the gateway (public) IP
address instead?

Now I see what you are trying to do. Is there any particular reason you
want to get that IP address?

If you do print_r($_SERVER) you'll get a list of all the variables you
can access. I don't know of any way you'd be able to get the gateway's
IP address.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Fred Atkinson
On Tue, 05 Jul 2005 23:30:09 +1200, Chris Hope
<[Email Removed]> wrote:

QUOTE
print_r($_SERVER)

I'll give this a shot.

I am running a dynamic IP address on a server. I am
discovering that my hosting provider changes my IP address from time
to time. When I pull up the server from another location, I want to
be able to keep tabs on what the IP address has changed to.

I have a client on the server that updates the DNS record
whenever the gateway IP address changes. So, I'll always be able to
pull up the Webserver even if the IP address changes. I just want to
easily display it.

Of course, I could ping the domain name, but I'd like to code
it into the page, if possible.


Fred

Fred Atkinson
On Tue, 05 Jul 2005 11:47:51 GMT, Fred Atkinson
<[Email Removed]> wrote:

QUOTE
print_r($_SERVER)

I ran this. Didn't see anything like what I was looking for.
But I'll play with it.


Fred

Chris Hope
Fred Atkinson wrote:

QUOTE
On Tue, 05 Jul 2005 23:30:09 +1200, Chris Hope
<[Email Removed]> wrote:

print_r($_SERVER)

I'll give this a shot.

I am running a dynamic IP address on a server.  I am
discovering that my hosting provider changes my IP address from time
to time.  When I pull up the server from another location, I want to
be able to keep tabs on what the IP address has changed to.

I have a client on the server that updates the DNS record
whenever the gateway IP address changes.  So, I'll always be able to
pull up the Webserver even if the IP address changes.  I just want to
easily display it.

Of course, I could ping the domain name, but I'd like to code
it into the page, if possible.

I have the same issue, but it's more to do with being able to ssh in to
my desktop machine, rather than trying to access a web server remotely.

There are some sites out there that you can request a page from and it
will return your IP address (ie the one of your gateway) - I'm sure if
you do a Google search you'll be able to find one. Some dynamic dns
providers provide scripts for you to do this without having to use a
3rd party.

Another alternative is to write a script that gets the IP address from
your router. This is how I do it myself; my router has a configuration
module which is browsable through a web browser, so I wrote a PHP
script which runs each minute via cron to grab the page off it which
shows the public IP address. If the IP address has changed at all it
then calls up the page necessary to change the dynamic address online.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Kimmo Laine
"Fred Atkinson" <[Email Removed]> wrote in message
news:[Email Removed]...
QUOTE
On Tue, 05 Jul 2005 23:30:09 +1200, Chris Hope
<[Email Removed]> wrote:

print_r($_SERVER)

I'll give this a shot.

I am running a dynamic IP address on a server.  I am
discovering that my hosting provider changes my IP address from time
to time.  When I pull up the server from another location, I want to
be able to keep tabs on what the IP address has changed to.

I have a client on the server that updates the DNS record
whenever the gateway IP address changes.  So, I'll always be able to
pull up the Webserver even if the IP address changes.  I just want to
easily display it.

Of course, I could ping the domain name, but I'd like to code
it into the page, if possible.

execute "ping www.yourdomain.com" and parse the IP it translates it to from
the output. For example in a windows machine executing:

C:>ping www.google.com

returns the following:

Pinging www.l.google.com [66.102.9.99] with 32 bytes of data:

Reply from 66.102.9.99: bytes=32 time=52ms TTL=242
Reply from 66.102.9.99: bytes=32 time=52ms TTL=242
Reply from 66.102.9.99: bytes=32 time=52ms TTL=242
Reply from 66.102.9.99: bytes=32 time=52ms TTL=242

Ping statistics for 66.102.9.99:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 52ms, Maximum = 52ms, Average = 52ms

From the output you can parse, with regexp for example, the ip 66.102.9.99.
It's just a matter of does your server allow execute.

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com

JimL
Here is a good fast site that gives you your IP address.

http://www.whatismyip.com/

Wish I knew how they did it.


In my case, it is a roadrunner isp that gives me a dynamic address.
I'm sitting behind a router and it lots of trouble to look it up and
so much easier using the above site.

The address is not very dynamic though. I've had the same address
since installing about 6 months ago. I suppose that is a good thing.

JimL








On Wed, 6 Jul 2005 11:00:38 +0300, "Kimmo Laine"
<[Email Removed]> wrote:

QUOTE
"Fred Atkinson" <[Email Removed]> wrote in message
news:[Email Removed]...
On Tue, 05 Jul 2005 23:30:09 +1200, Chris Hope
<[Email Removed]> wrote:

print_r($_SERVER)

I'll give this a shot.

I am running a dynamic IP address on a server.  I am
discovering that my hosting provider changes my IP address from time
to time.  When I pull up the server from another location, I want to
be able to keep tabs on what the IP address has changed to.

I have a client on the server that updates the DNS record
whenever the gateway IP address changes.  So, I'll always be able to
pull up the Webserver even if the IP address changes.  I just want to
easily display it.

Of course, I could ping the domain name, but I'd like to code
it into the page, if possible.

execute "ping www.yourdomain.com" and parse the IP it translates it to from
the output. For example in a windows machine executing:

C:>ping www.google.com

returns the following:

Pinging www.l.google.com [66.102.9.99] with 32 bytes of data:

Reply from 66.102.9.99: bytes=32 time=52ms TTL=242
Reply from 66.102.9.99: bytes=32 time=52ms TTL=242
Reply from 66.102.9.99: bytes=32 time=52ms TTL=242
Reply from 66.102.9.99: bytes=32 time=52ms TTL=242

Ping statistics for 66.102.9.99:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 52ms, Maximum = 52ms, Average = 52ms

From the output you can parse, with regexp for example, the ip 66.102.9.99.
It's just a matter of does your server allow execute.


Jeppe Uhd
JimL wrote:
QUOTE
Here is a good fast site that gives you your IP address.

http://www.whatismyip.com/

Wish I knew how they did it.

Quite easily:

<? echo 'Your ip: '.getenv("REMOTE_ADDR"); ?>

--
MVH Jeppe Uhd - NX http://nx.dk
Webhosting for nrder og andet godtfolk

Fred Atkinson
On Mon, 11 Jul 2005 14:42:24 +0200, "Jeppe Uhd" <[Email Removed]>
wrote:

QUOTE
Quite easily:

<? echo 'Your ip: '.getenv("REMOTE_ADDR"); ?

You didn't quite understand what I'm trying to do.

Let me illustrate (the IP addresses are hypothetical):

IP Web NAT Internet User's
------ Server Gateway Cloud Client
Private 192.168.15.10 192.168.15.1
Public 17.15.21.4 21.15.5.7

If the client pulled a Web page off the server with the
function you describe, it would return 192.168.15.10 (a private IP
address that is not reachable by http://191.168.15.10 from the User's
client machine). I want it to return 17.15.21.4 (or whatever it would
change to when DHCP would provide a different IP address).

I have a service running on the server that updates my DNS
service when the public address for the NAT gateway changes and I have
port 80 requests made upon the gateway's public IP address poined to
192.168.15.10. To the client pulling a page from my Webserver, the
server's IP address is 17.15.21.4. That is what I want it to return.

Any ideas?

Regards,


Fred

Fred Atkinson
I may have just figured out the means to a solution. Can
anyone suggest a way to do it?

Is there a function that would start at the beginning of a
file, check each line until it found the last IP address in the file
and return that last IP address it encounters that is written into
that file?

There is a log file for that service program (DynDNS by
SiteDevelopers) that records the current public IP address each time
it changes. If I could return that IP address with a PHP function,
that would do it. This IP address would be the gateway.

There is a copy of the first few lines of the log file below
(I changed the IP addresses and the subdomain names for security
purposes). If this were the entire file, the function should return
35.99.26.52.

Any ideas on how this could be done?

Regards,


Fred

11/11/2004 6:44:51 AM - Dynamic DNS Client's service has started.
11/11/2004 6:44:52 AM - Site index number: 0
11/11/2004 6:44:53 AM - IP Change Detected. New IP: 35.99.26.25
11/11/2004 6:48:12 AM - Dynamic DNS Client's service has started.
11/11/2004 6:48:15 AM - Site index number: 0
11/11/2004 6:48:19 AM - IP Change Detected. New IP: 35.99.26.74
11/11/2004 6:53:46 AM - Manual request to go offline.
11/11/2004 6:53:53 AM - www.slamdunk.com updated successfully to
127.0.0.1
11/11/2004 6:53:54 AM - www.dunkslam.com updated successfully to
127.0.0.1
11/11/2004 6:53:57 AM - Manual request to go online.
11/11/2004 6:53:57 AM - Site index number: 1
11/11/2004 6:53:57 AM - IP Change Detected. New IP: 35.99.26.52
11/11/2004 6:54:08 AM - Manual request to go offline.
11/11/2004 6:54:09 AM - Manual request to go online.

Hilarion
QUOTE
<? echo 'Your ip: '.getenv("REMOTE_ADDR"); ?

You didn't quite understand what I'm trying to do.

You didn't quite understand what Fred wrote. :)
He wrote how http://www.whatismyip.com/ shows your IP, not
how you can get it.

Hilarion

Hilarion
QUOTE
You didn't quite understand what Fred wrote. :)


I meant Jeppe, not you - Fred.


Hilarion

Hilarion
Fred Atkinson wrote:
QUOTE
I may have just figured out the means to a solution.  Can
anyone suggest a way to do it?

Is there a function that would start at the beginning of a
file, check each line until it found the last IP address in the file
and return that last IP address it encounters that is written into
that file?

There is a log file for that service program (DynDNS by
SiteDevelopers) that records the current public IP address each time
it changes.  If I could return that IP address with a PHP function,
that would do it.  This IP address would be the gateway.

There is a copy of the first few lines of the log file below
(I changed the IP addresses and the subdomain names for security
purposes).  If this were the entire file, the function should return
35.99.26.52.

Any ideas on how this could be done?

11/11/2004 6:44:51 AM - Dynamic DNS Client's service has started.
11/11/2004 6:44:52 AM - Site index number: 0
11/11/2004 6:44:53 AM - IP Change Detected.  New IP: 35.99.26.25
11/11/2004 6:48:12 AM - Dynamic DNS Client's service has started.
11/11/2004 6:48:15 AM - Site index number: 0
11/11/2004 6:48:19 AM - IP Change Detected.  New IP: 35.99.26.74
11/11/2004 6:53:46 AM - Manual request to go offline.
11/11/2004 6:53:53 AM - www.slamdunk.com updated successfully to
127.0.0.1
11/11/2004 6:53:54 AM - www.dunkslam.com updated successfully to
127.0.0.1
11/11/2004 6:53:57 AM - Manual request to go online.
11/11/2004 6:53:57 AM - Site index number: 1
11/11/2004 6:53:57 AM - IP Change Detected.  New IP: 35.99.26.52
11/11/2004 6:54:08 AM - Manual request to go offline.
11/11/2004 6:54:09 AM - Manual request to go online.


I do not see problems (the code is not tested, but the idea should
be clear):

<?php
define( 'IP_CHANGE', 'IP Change Detected. New IP: ' );
define( 'UPDATED', ' updated successfully to' );
$lines = file( 'log_file.log' );
for( $i=0; $i < count( $lines ); $i++ )
{
$line = trim( $lines[ $i ] );
$line = explode( ' - ', $line );
if (substr( $line[1], 0, strlen( IP_CHANGE ) ) == IP_CHANGE)
$new_ip = substr( $line[1], strlen( IP_CHANGE ) );
else if (substr( $line[1], strlen( UPDATED ) ) == UPDATED)
$i++;
}
if (isset( $new_ip ))
echo 'new IP is: ' . $new_ip;
else
echo 'no IP in log file.';
?>


Hilarion


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.