How to Get the External Ip of My Server Using PHP

How do I get the external IP of my server using PHP?

Just query a host that returns your IP address:

$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
$externalIp = $m[1];

or, set up a service that simply echoes just the IP, and use it like this:

$externalIp = file_get_contents('http://yourdomain.example/ip/');

Set up the service yourself by simply echoing the remote IP address, or pay someone to host it. Do not use somebody else's server without permission. Previously, this answer linked to a service of mine that's now being hit multiple times a second.

Note that in an IP network with one or more NATs, you may have multiple external IP addresses. This will give you just one of them.

Also, this solution of course depends on the remote host being available. However, since there is no widely implemented standard (no ISP and only some home routers implement UPnP), there is no other way to get your external IP address. Even if you could talk to your local NAT, you couldn't be sure that there isn't another NAT behind it.

how to get public ip address of my localhost system

try this please:

$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
$externalIp = $m[1];

Or use httpbin.org/ip as Priyesh Kumar suggests

How can I connect to a database on an external ip?

First Check Your Public IP Address [ Public Ip Means External Ip Address] From Here : https://www.whatismyip.com/

And Now Check Your System Ip Address ( Internal Ip Address using ipconfig in cmd )

Both Are Differect So You Need To Used Public Ip Address Which One You Get From https://www.whatismyip.com

Now, you have to Give Access permission to Database for Specific iP Address, By Default Mysql is not allowing to Access it SO You Need To Fire This Command for .

Like This :

SQL> GRANT ALL PRIVILEGES ON database.* TO 'user'@'your_database_pc_public_ip' IDENTIFIED BY 'newpassword';

Example::

 SQL> GRANT ALL PRIVILEGES ON database.* TO 'any_name'@'public_ip' IDENTIFIED BY 'any_password';

Then Its Work Fine For You :)

PHP - Get the public ip

Try using $_SERVER['REMOTE_ADDR'] to fetch the user's IP address.

http://php.net/manual/en/reserved.variables.server.php

How do I get the external IP address in a Symfony2 controller?

I hate to answer my own question, and will gladly mark a better answer (short and elegant and independent of outside servers unlike below, maybe using the network stack, traceroute or some such wizardry), but for now for a task this small we needed something quick for staging or production, so:

  $CustomerIp =  system("curl -s ipv4.icanhazip.com") ;  // returns string '93.9.29.389' (length=11)


Related Topics



Leave a reply



Submit