简体   繁体   中英

login on webpage only from the internet

First of all sorry if my question is dumb or if I use bad terminology but I'm totally new to web programming.

So, I've made an apache server on raspberry pi with few buttons on the webpage to controll lights in my room. I can access it with my home network ip 192.168.xxx.xxx and from internet using my public ip 188.75.xxx.xxx . (just to be clear - the raspi is connected to my router and in the router I'm forwarding port 80 to it)

I want to make some basic login form, but only if I'm connecting trough the 188.75.xxx.xxx ip. And if I'm conencting trough the 192.168.xxx.xxx the form will not show.

Is there any way to achieve this? Thanks, Adam N.

When the script is executed, check the IP address before showing anything to the user. Read here about the procedure. Once you have an IP address, you can parse it and see the first two groups. Try something like this to do that. Then compare the numbers and decide to grant or deny permission to access the page. Here you can read about login form in PHP. In pseudo code, that would look like that:

// this is pseudo code, don't try to run it - it will not work
$ip = get_ip();
$groups = parse_ip($ip);
if ( is_home_network($groups) )
{
    grant_permissions();
}
else
{
    request_login();
}

You can use the PHP filter_var Function with the validate filter FILTER_VALIDATE_IP and the flag filter FILTER_FLAG_NO_PRIV_RANGE .

Code Example:

if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) )
{
    // is not a local ip address
}

I hope i could help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM