Professional Web Applications Themes

Server IP Address - PHP Development

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

  1. #1

    Default Server IP Address

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


    Fred

    Fred Guest

  2. #2

    Default Re: Server IP Address

    Fred Atkinson wrote: 
    There is a built in variable that contains the user's IP address, I
    think it's $_SERVER[REMOTE_ADDR]
    Exyle Guest

  3. #3

    Default Re: Server IP Address

    "Fred Atkinson" wrote:
     

    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/


    Philip Guest

  4. #4

    Default Re: Server IP Address

    Philip Ronan wrote:
     
    >
    > 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>[/ref]

    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 Guest

  5. #5

    Default Re: Server IP Address

    Fred Atkinson wrote:
     

    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
    Chris Guest

  6. #6

    Default Re: Server IP Address

    >What do you think the difference between the webserver and the host is? 

    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

    Fred Guest

  7. #7

    Default Re: Server IP Address

    Fred Atkinson wrote:
     
    >
    > 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.[/ref]

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

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

  8. #8

    Default Re: Server IP Address

    On Tue, 05 Jul 2005 22:17:11 +1200, Chris Hope
    <com> wrote:
     

    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

    Fred Guest

  9. #9

    Default Re: Server IP Address

    Fred Atkinson wrote:
     
    >
    > 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?[/ref]

    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
    Chris Guest

  10. #10

    Default Re: Server IP Address

    On Tue, 05 Jul 2005 23:30:09 +1200, Chris Hope
    <com> wrote:
     

    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 Guest

  11. #11

    Default Re: Server IP Address

    On Tue, 05 Jul 2005 11:47:51 GMT, Fred Atkinson
    <com> wrote:
     [/ref]

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


    Fred

    Fred Guest

  12. #12

    Default Re: Server IP Address

    Fred Atkinson wrote:
     
    >
    > 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.[/ref]

    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
    Chris Guest

  13. #13

    Default Re: Server IP Address

    "Fred Atkinson" <com> wrote in message
    news:com... 
    >
    > 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.[/ref]

    execute "ping www.yourdomain.com" and p 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 p, 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


    Kimmo Guest

  14. #14

    Default Re: Server IP Address


    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"
    <com> wrote:
     
    >>
    >> 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.[/ref]
    >
    >execute "ping www.yourdomain.com" and p 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 p, with regexp for example, the ip 66.102.9.99.
    >It's just a matter of does your server allow execute.[/ref]

    JimL Guest

  15. #15

    Default Re: Server IP Address

    JimL wrote: 

    Quite easily:

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

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


    Jeppe Guest

  16. #16

    Default Re: Server IP Address

    On Mon, 11 Jul 2005 14:42:24 +0200, "Jeppe Uhd" <dk>
    wrote:
     

    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 Guest

  17. #17

    Default Re: Server IP Address

    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.

    Fred Guest

  18. #18

    Default Re: Server IP Address

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

    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 Guest

  19. #19

    Default Re: Server IP Address

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


    I meant Jeppe, not you - Fred.


    Hilarion
    Hilarion Guest

  20. #20

    Default Re: Server IP Address

    Fred Atkinson wrote: 


    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
    Hilarion Guest

Similar Threads

  1. Server address?
    By kjeft in forum Macromedia Flash Flashcom
    Replies: 2
    Last Post: September 17th, 07:38 AM
  2. Testing Server problem: Server name or address couldnot be resolved!
    By fwilkinson in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: November 9th, 08:03 PM
  3. New Web Server IP Address
    By Deborah in forum Windows Server
    Replies: 4
    Last Post: July 19th, 06:51 PM
  4. DNS Name Server with one IP address
    By Ivan in forum Linux Setup, Configuration & Administration
    Replies: 12
    Last Post: January 27th, 05:29 AM
  5. Can't find server name for address....
    By cljlk in forum Sun Solaris
    Replies: 5
    Last Post: August 12th, 08:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139