0

I need to find out if a specific file, on another host, is reachable on the local network (of the client)

This is what I would like to do

<?php
function isFileReachable(){
        $urlHome = 'http://192.168.2.2/FileFolder/File.txt';
        list($status) = get_headers($urlHome);
        if (strpos($status, '404') !== FALSE) {
           return false;
        }else if(strpos($status, '200') !== FALSE){ 
            return true;
        }
}
?>

I call this script when pressing a button a my website. but I always get HTTP/1.1 301 Moved PermanentlyWhat does this mean?

Is it possible to even do this? ifso, how do I do this? What am I doing wrong? Or are there better ways to do this?

vanlooverenkoen
  • 2,121
  • 26
  • 50

1 Answers1

1

look at this, https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2

its means the file you are looking for where moved to another url...

it will give you the url so you can make another request.

your code also needs to consider where the function returns false...

Javier Neyra
  • 1,239
  • 11
  • 12
  • I understand the redirect. And I know why this is happening. But how can I see if the client can communicate with that specific file on the clien-network – vanlooverenkoen Jan 18 '16 at 19:49
  • @KoenVanLooveren well... if you understand what a 301 redirect means, then you understand that "The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs"... in other words, the file you look for is somewhere else.... go there and get it.... – Javier Neyra Jan 18 '16 at 19:56
  • Yes I know why it happens. But look. I run a website. I want to test if the client is on my home network or if the client is not on my home network. If the client is on my home network I want to redirect to the 192.168.2.2 instead of my domain name. What I am doing at the moment is checking if I can get the file where I redirect. But I always get that the client is on the home network because, I think php does the request form the serverlocation (on my homenetwork) – vanlooverenkoen Jan 18 '16 at 19:59
  • if(client == onHomeNetwork){ redirectTo(192.168.2.2); }else { //do nothing } – vanlooverenkoen Jan 18 '16 at 20:00
  • This is what it looks like in my head but I can't figure it out how to do it – vanlooverenkoen Jan 18 '16 at 20:01
  • ok... so you want to redirect LOCAL CLIENTS, to you local webserver address.... try with $_SERVER['REMOTE_ADDR']... if the client is in your local network, you will see, either, an ip in your range . 192.x.x.x or, the request comming from your public internet address... so you can redirect your client there.... – Javier Neyra Jan 18 '16 at 20:04
  • something like this will probably be enough... if ( ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) ) { // is a local ip address } – Javier Neyra Jan 18 '16 at 20:07
  • Oke yes this works, but if I am at my friends house. I check my website and then the website will tell that it is possible to redirect because the ip is valid as well. – vanlooverenkoen Jan 18 '16 at 20:27
  • can you read man? FILTER_FLAG_NO_PRIV_RANGE FILTER_VALIDATE_IP Fails validation for the following private IPv4 ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Fails validation for the IPv6 addresses starting with FD or FC.... http://php.net/manual/en/filter.filters.flags.php – Javier Neyra Jan 18 '16 at 20:34
  • No I don't get it. my friends house has the same router as me. So his default gateway is 192.168.2.1, the same as mine. when you go to the website at his house. $_SERVER['REMOTE_ADDR']; will return 192.168.2.1 and will then say that it is a valid ip-address. because what I am doing right now is this: – vanlooverenkoen Jan 18 '16 at 20:41
  • $ip = $_SERVER['REMOTE_ADDR']; if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { return true; }else{ return false; } – vanlooverenkoen Jan 18 '16 at 20:42
  • and it returns true if I am on my desktop (homenetwork) with an ip of 192.168.2.185 and default gateway of 192.168.2.1 – vanlooverenkoen Jan 18 '16 at 20:42
  • when I am on my mobile device over LTE it returns false so it is not a valid ip. it is because I put a "!" before the statement that I know that the ip is valid do you understand? – vanlooverenkoen Jan 18 '16 at 20:44
  • lets do this.... explain what you want like if i have 4 years old... because i just dont get it.... – Javier Neyra Jan 20 '16 at 21:08
  • and let my clarify something... if you are at your friend house... and your friend's router it is not locally connected (like with a vpn or physical ethernet cable for say something) your website will not see the ip as local, will see your friend public ip address and will not be in those ranges... why you dont try the script... and let me know if it worked..... – Javier Neyra Jan 20 '16 at 21:12
  • Look, I'm creating a website for homeautomation. I have this website liked to my domain so I can acces the homeautomation-website from everywhere. Example: check if my garage door is open, if so I can close it (in case i forgot). So the website gives you extra features when you are on home network (for security reasons) What I want to do is check if I am on my home network is to chefck if I go to my domain name, and I am on my home network, the website wil be redirected to my local server addres – vanlooverenkoen Jan 20 '16 at 21:12
  • I want to do this because my whole family are computer noobs, So if I just give them the domain name they will always get the right acces, and the right homeautomation-tasks – vanlooverenkoen Jan 20 '16 at 21:16
  • well, the script i handled to you does exactly that. check if the ip address of the client sending the request to the webserver is in a local - range....thats why when you execute the request from your phone it goes for the false part in the if... – Javier Neyra Jan 20 '16 at 21:17
  • Oke, I haven't found the time to check this again, but When I do, I will give you feeback about it, thanks again ;) realy appreciate it! – vanlooverenkoen Jan 20 '16 at 21:18