If you already have your own webserver/webspace on the Internet and some programming knowledge, you can save yourself a service like DynDNS.org & Co. without much effort if you want to access parts of your home network from the road. Such services provide a host name/URL (e.g. my-home-network.dyndns.org) which is then forwarded to the IP address of the home network. In addition to a user account, which is often subject to a fee, you then only have to configure your router at home so that it automatically transmits this to the DynDNS service in the event of an IP change.
Today, many Internet connections at home already have more or less fixed IP addresses, at least as long as the cable modem, for example, is not disconnected from the power supply for a short time. So if you want to be safe again, you can set up a DynDNS solution or build on the fast one yourself.
Table of contents:
- Create your own DynDNS-Service
- Step 1 and 2 - PHP Scripting
- Step 3 - Call PHP script from home network
- Conclusion - Build your own DynDNS
Create your own DynDNS-Service
1.) Write a PHP script that receives, stores and outputs the new IP address of the home network.
2.) Install the PHP script on a webserver/webspace that is publicly accessible. (for example http://tinkering-sascha.com/my_dyndns/index.php)
3.) Configure your home server (e.g. Raspberry Pi ) (for example Cronjob or Task-Scheduler), so the PHP-Script is called periodically to tell the new IP-Address.
Step 1 and 2 - PHP Scripting
To keep the whole thing as simple as possible, I did it without a database connection. So the IP address of the home network is simply written into a file.
/* filename -> maybe change it.; config_file which will receive the IP-Address of the home network; */
$config_file = "myip.txt";
To add a touch of security to the script, we next define two variables, each with a random string. These strings must be transmitted when writing ($secret) and reading ($getip) the IP address of the home network to ensure that only authorized people have access to the functionality. If this is not enough, you can encrypt the connection via SSL and build a login system around it. 😉
//This string is passed by the home network server via GET, whereupon the new IP address is stored in the config-file.;
$secret = "1234567890";
//This string must be passed by GET to get the current IP address of the home network;
$getip = "0987654321";
Now a few lines of code will follow to write, read and output the IP address.
if (strcmp($secret, $_GET['secret']) == 0)
//secret korrekt; IP-Adresse speichern;
$ip = $_SERVER['REMOTE_ADDR'];
$f = fopen($config_file, "w");
else if (isset($_GET['getip']))
if (strcmp($getip, $_GET['getip']) == 0)
//secret korrekt; IP-Adresse herausgeben;
$f = fopen($config_file, "r");
$ip = fread($f, 32);
The script is now ready for a test, so upload it. If you call the URL of the web space where the script is located with the parameter secret=1234567890, the IP address of the calling computer is stored. A call with getip=0987654321 on the other hand returns the IP address.
Depending on how you want to use the access to the home network, it might make sense to use the header ("Location: http://".$ip."/deeplink-ins-heimnetz/") to forward the user or the calling computer directly instead of displaying the IP address with echo. If you only want to retrieve the IP address instead, e.g. via an app, in order to create your own paths or calls into the network, the echo of the IP address is sufficient at this point.
Link to the PHP-Script that contains the code for your own DynDNS-Server:
-= Download DynDNS-Server =-
Step 3 - Call PHP script from home network
In my case there is a Raspberry Pi (Model B) in the home network, which acts as server. My webrouter has been configured (IP forwarding) so that the RaspberryPi responds to requests from the Internet on port 80 (http). To get the Pi to do this, let's say every 10 minutes we call the PHP script to tell us the new IP address of the home network, open the /etc/crontab file and add the following line:
*/10 * * * * pi wget -O /dev/null "http://my-webspace.de/index.php?secret=1234567890"
Now the Pi passes the IP address of the home network at 10-minute intervals. - Simple solution! 🙂
Conclusion - Build your own DynDNS
Of course this is not a professional solution, but it is a simple and easy solution to realize your own DynDNS service. All in all, it does what it should do! 🙂
As already briefly mentioned, the script has to be adapted with regard to security and data protection. In any case, strong passwords ($secret and $getip) and a random name of at least 16 characters for the file in which the IP address of the home network is stored would make sense. ($config_file -> e.g. jkSD43jkdmrwf4993jk_43s4AA!.txt).
SSL encryption wouldn't be bad either, but the security issue is relative in this case, at least as far as reading the IP address is concerned. Because every service running in the home network and which should be accessible from outside, be it cloud or media/web server, should have its own access control or login with user name and password, so that it is not too tragic if someone gets to the IP address of the home network, so that the IP address could also be displayed without the $getip parameter when calling the script. So it's simply up to you to decide how much effort you want to put into the protection....
This might also be interesting for youTelegram Messenger and the Bot API - Part 2
In the first part - Telegram Messenger and the Bot API - I explained some basics how to set up a telegram bot and how to use it to send messages, states, informations and more to your . Since this tool has proven to be very practical, I decided to take up the previous article … Continue reading "Telegram Messenger and the Bot API - Part 2"
Programming: PHP image captcha code
Telegram Messenger and the Bot API
Telegram is basically a messenger similar to Whatsapp or Threema , free and easy to use. I don't want to say that it is better or worse than other messengers, but I like the Telegram Bot API so much that I want to write a little tutorial here. Table of contents:Using Telegram Messenger and the … Continue reading "Telegram Messenger and the Bot API"
Flashing firmware - ESP8266-WiFi-Module
Since I recently started to deal more intensively with the topics "Smart Home" and "Home automation" I decided to publish some of my construction sites, stumbling blocks and solutions here. As already mentioned in the article about my Smart-Home-Project V1 I use the WLAN module , because it is small, reliable and cheap. In this … Continue reading "Flashing firmware - ESP8266-WiFi-Module"
Smart Home V1 - Do it yourself
I've been planning my smart home for some time now. Besides the cost factor for all the great things I imagine, I often lack the necessary time for detailed planning and implementation. So I decided to start somewhere and write down this DIY project in parallel...let's see where it leads...I ask for your indulgence if … Continue reading "Smart Home V1 - Do it yourself"