比较IP地址 [英] Comparing IP addresses

查看:99
本文介绍了比较IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function getIp() {
  return "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
}

我想调整或重做这个,以便某些行动只能从localhost或我的isp服务器。我可以使用一些JavaScript为127.0.0.1工作,但不能用于另一个。

I want to adapt or redo this so that certain actions can only be taken from localhost or my isp server. I can make it work with a little bit of JavaScript for 127.0.0.1 but not for the other.

感谢任何帮助;谢谢

推荐答案

您真的想在服务器上完成此操作。如果您在客户端检查IP,那么人们可以很容易地解决这个问题。例如,使用像Firebug这样的Web检查器在浏览器中修改代码。

You really want to do this completely on the server. If you check for IPs on the client side then people can very easily hack around that. For example by modifying your code in their browser using a web inspector like Firebug.

幸运的是,在服务器端执行它非常简单:

Fortunately it is pretty simple to do on the server side:

$ _ SERVER ['REMOTE_ADDR'] 变量是一个简单的字符串,因此您应该能够使用字符串比较来检查您的IP希望。

The $_SERVER['REMOTE_ADDR'] variable is a simple string so you should be able to use string comparison to check for the IPs that you want.

例如:

<?php
   if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1" || $_SERVER['REMOTE_ADDR'] == "1.2.3.4") {
       echo "Show secret things here";
   }
?>

这篇关于比较IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆