{"id":221,"date":"2017-01-08T11:26:02","date_gmt":"2017-01-08T03:26:02","guid":{"rendered":"http:\/\/www.cxybj.com\/?p=221"},"modified":"2017-01-08T11:26:08","modified_gmt":"2017-01-08T03:26:08","slug":"php%e9%99%90%e5%88%b6ip%e5%8f%8a%e6%8f%90%e4%ba%a4%e6%ac%a1%e6%95%b0%e9%99%90%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.cxybj.com\/?p=221","title":{"rendered":"PHP\u9650\u5236IP\u53ca\u63d0\u4ea4\u6b21\u6570\u9650\u5236"},"content":{"rendered":"<p>\u4e00\u3001\u539f\u7406<\/p>\n<p>\u63d0\u4ea4\u6b21\u6570\u662f\u80af\u5b9a\u8981\u5f80\u6570\u636e\u5e93\u91cc\u5199\u6b21\u6570\u8fd9\u4e2a\u6570\u636e\u7684\uff0c\u6bd4\u5982\u7528\u6237\u767b\u9646\uff0c\u5f53\u7528\u6237\u51fa\u9519\u65f6\u5c31\u5fd8\u6570\u636e\u5e93\u5199\u5165\u51fa\u9519\u6b21\u65701\uff0c\u5e76\u4e14\u51fa\u9519\u65f6\u95f4\uff0c\u518d\u51fa\u9519\u51992\uff0c\u5f53\u6ee1\u6bd4\u59825\u6b21\u65f6\u63d0\u793a\u4e0d\u5141\u8bb8\u518d\u767b\u9646\uff0c\u8bf7\u660e\u5929\u518d\u8bd5\uff0c\u7136\u540e\u7528DateDiff\u8ba1\u7b97\u51fa\u9519\u65f6\u548cnow()\u7684\u65f6\u95f4\uff0c\u5982\u679c\u5927\u4e8e24\u5c31\u518d\u5f00\u653e\u8ba9\u4ed6\u8bd5\u3002<\/p>\n<p>\u5c01IP\u7684\u8bdd\u7279\u522b\u662f\u7ed9IP\u65ad\u5c31\u6bd4\u8f83\u7b80\u5355\u4e86\uff0c<br \/>\n\u5148\u8bf4\u7ed9IP\u6bb5\u5f00\u653e\u7684\u60c5\u51b5\uff1a\u5148\u53d6\u51fa\u5ba2\u6237\u8bbf\u95ee\u7684IP\uff0c\u4e3a\u4e86\u89e3\u91ca\u65b9\u4fbf\uff0c\u8bbe\u6709IP192.168.6.2<br \/>\n\u73b0\u8981\u5f00\u653eIP\u6bb5\u4e3a192.168.*.*\u7684\u65ad\uff0c\u7ed9\u4e2a\u6bd4\u8f83\u5bb9\u6613\u770b\u61c2\u7684\u4ee3\u7801\u5427\uff1a<br \/>\nurl=split(ip,&#8221;.&#8221;) &#8216;\u8fd9\u91cc\u7684ip\u4e3a\u5ba2\u6237\u7aefIP<br \/>\nfsip=&#8221;192.168.*.*&#8221;\u00a0 \u00a0&#8216;\u5141\u8bb8\u7684\u6bb5\uff0c\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u53d6\u51fa\uff0c\u4e5f\u53ef\u4ee5\u8fd9\u4e48\u5b9a\u4e49<br \/>\nfip=split(fsip,&#8221;.&#8221;)<br \/>\nif fip(0)=url(0) and fip(1)=url(1)\u00a0\u00a0then<br \/>\nresponse.write &#8220;\u60a8\u7684IP\u88ab\u5c01&#8221;<br \/>\nelse response.write &#8220;\u53ef\u4ee5\u901a\u8fc7&#8221;<br \/>\nend if<br \/>\n\u5176\u5b9e\u7981\u67d0IP\u5c31\u8ddf\u4e0a\u9762\u7684\u65b9\u6cd5\u4e00\u6837\u3002\u6216\u8005\u5e72\u8106\u4ece\u6570\u636e\u5e93\u91cc\u53d6\u51faIP\u548c\u5ba2\u6237\u7aef\u7684\u76f4\u63a5\u6bd4\u8f83\u5c31\u597d\u4e86\u3002<br \/>\n\u4e00\u822c\u4f1a\u628a\u6570\u636e\u5e93\u91cc\u7684IP\u6bb5\u8bbe\u4e3a\u53cc\u7cbe\u5ea6\u578b\u7684\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8fd9\u4e9b\uff1a<br \/>\nif Request.ServerVariables(&#8220;HTTP_X_FORWARDED_FOR&#8221;)=&#8221;&#8221; then<br \/>\nIP=Request.ServerVariables(&#8220;REMOTE_ADDR&#8221;)<br \/>\nelse<br \/>\nIP=Request.ServerVariables(&#8220;HTTP_X_FORWARDED_FOR&#8221;)<br \/>\nend if<br \/>\nsip=IP<\/p>\n<p>cip=split(ip,&#8221;.&#8221;)<br \/>\nip=256*256*256*cip(0)+256*256*cip(1)+256*cip(2)+cip(3)-1<br \/>\n\u4e0a\u9762\u7684\u60c5\u51b5\u90fd\u662f\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u9762\uff0c\u6ca1\u6d89\u53cacookies\u7b49\u3002<\/p>\n<p>\u5176\u5b9e\u5c01IP\u4e0d\u662f\u5f88\u7406\u60f3\uff0c\u4f1a\u5f71\u54cd\u65e0\u8f9c\u7684\u4eba\uff0c\u6709\u5173\u7684\u6216\u8bb8\u662f\u56e0\u4e3a\u52a8\u6001IP\u9003\u8fc7\u4e86\u3002<\/p>\n<p>\u4e8c\u3001\u5c01\u5b9e\u4f8b<\/p>\n<p>&lt;?php<br \/>\nclass IP{\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\/\/\u83b7\u53d6\u5ba2\u6237IP\u5730\u5740<br \/>\nfunction getIpAdr(&amp;$ip){<br \/>\n$ip1=getenv(&#8220;HTTP_X_FORWARDED_FOR&#8221;);<br \/>\n$ip2=getenv(&#8220;HTTP_CLIENT_IP&#8221;);<br \/>\n$ip3=getenv(&#8220;REMOTE_ADDR&#8221;);<br \/>\nif($ip1&amp;&amp;$ip1!=&#8217;unknow&#8217;)<br \/>\n$ip=$ip1;<br \/>\nelse if($ip2&amp;&amp;$ip2!=&#8217;unknow&#8217;)<br \/>\n$ip=$ip2;<br \/>\nelse if($ip3&amp;&amp;$ip3!=&#8217;unknow&#8217;)<br \/>\n$ip=$ip3;<br \/>\nelse<br \/>\n$ip=&#8217;127.0.0.1&#8242;;<br \/>\n}<br \/>\n}<br \/>\nfunction get_netip($myip){\u00a0 \u00a0\/\/\u53ea\u7559\u5ba2\u6237IP\u5730\u5740\u7684\u524d\u4e09\u4f4d<br \/>\n$temp=explode(&#8220;.&#8221;,$myip);<br \/>\n$netip.=$temp[0];<br \/>\n$netip.=&#8221;.&#8221;;<br \/>\n$netip.=$temp[1];<br \/>\n$netip.=&#8221;.&#8221;;<br \/>\n$netip.=$temp[2];<br \/>\nreturn $netip;<br \/>\n}<br \/>\n$filename=&#8221;test.ini&#8221;;\u00a0 \u00a0\u00a0\u00a0\/\/\u5b9a\u4e49\u64cd\u4f5c\u6587\u4ef6<br \/>\n$ip_lib=file($filename);\u00a0\u00a0\/\/\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5230\u6570\u7ec4\u4e2d<br \/>\n$allow=0;<br \/>\n$IP=new IP;<br \/>\n$thisip=&#8221;&#8221;;<br \/>\n$IP-&gt;getIpAdr(&amp;$thisip);<br \/>\n$thenetip=get_netip($thisip);<\/p>\n<p>for($i=0;$i&lt;count($ip_lib);$i++){<br \/>\nif(ereg($thenetip,$ip_lib[$i])){<br \/>\n$allow=1;<br \/>\nbreak;<br \/>\n}<br \/>\n}<\/p>\n<p>if ($allow==1)<br \/>\n{<br \/>\necho &#8220;\u9a8c\u8bc1\u901a\u8fc7&#8221;;<br \/>\n}<br \/>\nelse {<br \/>\necho &#8220;&lt;script&gt;location.href=&#8217;Error.php&#8217;;&lt;\/script&gt;&#8221;;<br \/>\n}<\/p>\n<p>\u4e09\u3001\u9650\u5236\u8f93\u5165\u6b21\u6570<\/p>\n<p>1.\u9875\u9762\u9700\u8981\u5148session_start();2.\u70b9\u51fb\u767b\u9646\u7684\u65f6\u5019\u505a\u5224\u65ad,\u5982\u679c\u786e\u5b9a\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u662f\u9519\u8bef\u7684<br \/>\nif(\u7528\u6237\u7684\u5bc6\u7801\u662f\u9519\u8bef\u7684){<br \/>\nif(!empty($_SESSION[&#8216;login_error&#8217;])){if($_SESSION[&#8216;login_error&#8217;] == 3){ exit(&#8220;\u8fd9\u91cc\u5df2\u7ecf\u662f\u7b2c\u4e09\u6b21\u4e86&#8221;);}else{$_SESSION[&#8216;login_error&#8217;] = $_SESSION[&#8216;login_error&#8217;]++;} }else{$_SESSION[&#8216;login_error&#8217;] = 1;}}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u539f\u7406 \u63d0\u4ea4\u6b21\u6570\u662f\u80af\u5b9a\u8981\u5f80\u6570\u636e\u5e93\u91cc\u5199\u6b21\u6570\u8fd9\u4e2a\u6570\u636e\u7684\uff0c\u6bd4\u5982\u7528 &hellip; <a href=\"https:\/\/www.cxybj.com\/?p=221\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/221"}],"collection":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=221"}],"version-history":[{"count":1,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/221\/revisions\/222"}],"wp:attachment":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}