简体   繁体   English

仅从互联网登录网页

[英]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. 首先,对不起,如果我的问题很愚蠢,或者我使用了错误的术语,但是我对Web编程完全陌生。

So, I've made an apache server on raspberry pi with few buttons on the webpage to controll lights in my room. 因此,我在树莓派上制作了一个Apache服务器,网页上的几个按钮可以控制房间的灯光。 I can access it with my home network ip 192.168.xxx.xxx and from internet using my public ip 188.75.xxx.xxx . 我可以使用我的公用IP 188.75.xxx.xxx通过家庭网络ip 192.168.xxx.xxx和从Internet访问它。 (just to be clear - the raspi is connected to my router and in the router I'm forwarding port 80 to it) (只是要清楚-raspi已连接到我的路由器,并且在路由器中我将端口80转发到了它)

I want to make some basic login form, but only if I'm connecting trough the 188.75.xxx.xxx ip. 我想制作一些基本的登录表单,但是仅当我通过188.75.xxx.xxx IP连接时。 And if I'm conencting trough the 192.168.xxx.xxx the form will not show. 而且,如果我正在连接192.168.xxx.xxx ,则不会显示该表格。

Is there any way to achieve this? 有什么办法可以做到这一点? Thanks, Adam N. 谢谢亚当·N

When the script is executed, check the IP address before showing anything to the user. 执行脚本后,请在向用户显示任何内容之前检查IP地址。 Read here about the procedure. 在此处阅读有关程序。 Once you have an IP address, you can parse it and see the first two groups. 一旦有了IP地址,就可以解析它并查看前两个组。 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. 在这里,您可以阅读有关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 . 您可以将PHP filter_var函数与验证过滤器FILTER_VALIDATE_IP和标志过滤器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. 希望我能为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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