Get your external IP Address
You may also know the problem. You know your home network very good and can name your gadgets by its IP addresses, but you do not know how your IP address from the world wide web. Normally ISPs gives you an IP address which changes each time you boot your router or modem.
Motivation
You may ask: why do I need this information?
- if you want to play a network game online and host a server, then you need to tell your friends your IP address so they can join
- if you want to remote control your network or one machine (for example with SSH or VNC)
I think there may be more cases where you need it, but I think you get it.
If you are happy and a owner of a fix IP address, then you do not have a problem, but such fixed IP addresses are normally only available with business contracts with 50€ or more a month. I am a self employed software developer and I do not need such business contracts. With a normal contract you only have a dynamic IP address. You can check that if you restart your modem or router and check the external IP.
But how can I check my external IP? Thats simple. You may open Open Port Checker in your Browser. There you can see your IP. There you can also find information on open ports.
The problem
From your local network, you can easily get your external IP as I showed you. But if you want to get it from remote, how is that possible? Think of the problem when you spend your holiday somewhere and there was a energy loss? Then your router will have a new IP which you can not know.
For this problem, there is a simple solution.
PHP Script
<?php $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/\b(?:\d{1,3}\.){3}\d{1,3}\b/', $externalContent, $m); $externalIp = $m[0]; mail('office@ziegelwanger-edv.at', 'Externe IP', $externalIp);
This little script gets your IP address from a IP checker and sends it as an email. This script should run every day with a cron job or/and after each system restart. With this you have always your current external IP!
Problems
For this script you need sendmail, which may not be installed. So if you get the following error message:
sh: 1: /usr/sbin/sendmail: not found
then you need to install sendmail:
apt-get install sendmail
how i make this to run every 6 hours ?
i need it on my rapsbery pi
Hi! very easy, just define a new cronjob. For this, open /etc/crontab and add a new line:
0 */6 * * * sh /home/pi/script.sh
remember: 0 and */6 says that this script will be computed every 6 hours at minute 0. You will need to set the path to the script. If you need more information about cronjobs, look at this.
I want to get users real current IP using php how to get this?
You can use $_SERVER[‚REMOTE_ADDR‘] to get the most relevant IP address from the user… Normally this is his external IP, which should be the same when he has a static IP address or it changes (maybe daily) for dynamic IP addressing. But it can be also anything else, if the user uses VPN or things like TOR network.