Joanne Joanne - 21 days ago 11
PHP Question

Redirection based on IP doesn't work anymore, maybe something hostname based instead?

I have a question; I have been using the two examples below for ages so it always points me to my correct script. For example Sabnzbd and Sonarr.

However out of the blue, my home ISP decided to change my IP on (almost) a daily base. Also not just the last range of the IP, but also the number in front. This makes adapting my script kinda pointless and now I wondering if there is a way to make use of the hostname instead.

I only use this "script" at 2 important locations. The rest is not important. The home provider has "caiway.nl" in the hostname and at the other location it has "ziggo.nl" in the end of the hostname.

I use several scripts to redirect me to the right location, however the ones below are examples. Maybe someone has an idea or workaround for this issue once and for all.

<?php
// Sabnzbd
$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/62.45.*.*/",$visitor)) {
header('Location: http://192.168.1.55:9900');
} else {
header('Location: http://place.dnsforwarder.eu:9900/');
};
?>

<?php
// Sonarr
$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/62.45.*.*/",$visitor)) {
header('Location: http://192.168.1.55:3300');
} else {
header('Location: http://place.dnsforwarder.eu:3300/');
};
?>


So instead of something IP-based, I want to make use of the hostname instead, so it doesn't matter what IP am given at any day.

I already thought of switching it around, however at the other place my IP also changes frequently. Sigh.

I hope someone understand what I mean and can come up with some kind of workable solution and/or example to use with the above.... Thanks.

[edit/update/more info]

Apparently it's not very clear. I use the above code on a personal starting (web)page. So I can click on those 2 links and it will redirect to my local IP and port number, otherwise it will a dynamic dns service (afraid.org) to redirect me correctly. In this case it's "place.dnsforwarder.eu" as a dns forward (hence the name obviously).

However, as explained, both ISP's (different ones) are changing my IP number on a daily basis. So the code doesn't work anymore as it should (on 1 location at least). Now instead of some kind of IP check, I want to know if it's possible to use something hostname based instead (based on my 2 ISP's; ziggo.nl or caiway.nl). That way it doesn't matter if they would change my IP every minute, I would always get forwarded to the right place.

Sidenote; I could ofcourse create 2 different starting pages, one for each location, however that brings a ton of work along and more importantly; it wouldn't work correctly because browsers are in sync nowadays and it would mean I would have to change my starting page (1 or 2) everytime I go to one of the 2 locations...

Answer

I believe you're looking for something along the lines of:

// Sabnzbd
$visitor = $_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($visitor);
if (stristr($hostname, 'caiway.nl')) {
      header('Location: http://192.168.1.55:9900');
} else {
      header('Location: http://place.dnsforwarder.eu:9900/');
}

// Sonarr
$visitor = $_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($visitor);
if (stristr($hostname, 'ziggo.nl')) {
      header('Location: http://192.168.1.55:3300');
} else {
      header('Location: http://place.dnsforwarder.eu:3300/');
}

gethostbyaddr will resolve the IP address to a hostname.