环境ThinkPHP+Redis
1.IP保存文件,文件名自定义,与后文对应
2.获取IP信息脚本.sh文件- #!/bin/bash
- #variables
- ip_txt_path=/www/wwwroot/checkip/china_ip.txt;
- ip_url='http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest';
- php_path=/www/server/php/72/bin/php
- script_path=/www/wwwroot/checkip/putip2redis.php
- #mv old txt
- cur_time=$(date +"%Y%m%d%H%M%S");
- if [ -f ${ip_txt_path} ];then
- mv ${ip_txt_path} ${ip_txt_path}_${cur_time};
- fi
- #download
- /usr/bin/curl ${ip_url} | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' >${ip_txt_path}
- #parse 2 redis
- echo "begin parse ip\n";
- ${php_path} ${script_path}
复制代码 3.将IP信息保存到Redis中,上图中putip2redis.php文件- [/code]4.运行.sh文件初始化redis数据
- 5.添加类文件到TP的extend下
- [img]https://img2023.cnblogs.com/blog/1799841/202303/1799841-20230317230050481-1332488805.png[/img]
- [code]
复制代码 6.引用类调用函数判断IP- use ipxx\Ipcheck;
- public function test()
- {
- define("REDIS_SERVER", "127.0.0.1");
- define("REDIS_PORT", "6379");
- define("IP_HASH_NAME", "china_ip_hash");
- $ipcheck = new Ipcheck();
- $redis_link = new \Redis();
- $redis_link->connect(REDIS_SERVER,REDIS_PORT);
- $userip=$_SERVER['REMOTE_ADDR'];
- $is_in = $ipcheck->is_ip_in_china($userip);//判断一个ip是否国内的ip
- if ($is_in == true) {
- echo "china";
- } else {
- echo "foreign";
- }
- }
复制代码
[code][/code]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |