海运的博客

PHP和JS判断来路跳转到指定页面

发布时间:June 4, 2013 // 分类:PHP // No Comments

PHP根据referer跳转:

<?php
$ref = $_SERVER['HTTP_REFERER'];
        if(stripos($ref,"baidu") || stripos($ref,"google")
    {
        header("Location: https://www.haiyun.me"); 
        exit;
    }
?>

JS判断方法:

<script>
    var s=document.referrer;
    if(s.indexOf("baidu")>0||s.indexOf("soso")>0||s.indexOf("google")>0||s.indexOf("yahoo")>0||s.indexOf("sogou")>0||s.indexOf("youdao")>0||s.indexOf("bing")>0)
    {
    self.location="https://www.haiyun.me";
    }
</script>

根据UA跳转:

<?php
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if(stripos($userAgent,"Moz") || stripos($userAgent,"baidu"))
    {
        header("Location: https://www.haiyun.me");
    }
?>

Lnmp编译添加php imap模块

发布时间:May 11, 2013 // 分类:PHP // No Comments

cd php-5.2.17/ext/imap
/usr/local/php/bin/phpize 
./configure --with-php-config=/usr/local/php/bin/php-config --with-kerberos --with-imap-ssl
make
make install

编译时如遇到错误:

configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.

安装imap库:

yum install libc-client-devel

加载imap到php配置文件:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension=imap.so

Nginx模块GeoIP匹配处理IP所在国家、城市

发布时间:July 1, 2012 // 分类:Nginx // 5 Comments

Nginx可配合GeoIP模块定位IP所在物理位置并做相应处理,支持多个条件匹配:

#https://www.haiyun.me
$geoip_country_code #国家代码2位,如CN
$geoip_country_code3 #国家代码3位,如CHN
$geoip_country_name #国家完整名称,如China
$geoip_region #所在地区
$geoip_city #所在城市,如BeiJing
$geoip_postal_code #邮政编码
$geoip_city_continent_code #所在洲,如AS
$geoip_latitude #纬度
$geoip_longitude #经度

编译安装Nginx并添加GeoIP模块:

yum install geoip-devel #安装GeoIP解析库
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_geoip_module
make 
make install

下载GeoIP城市国家数据库:

#https://www.haiyun.me
mkdir -p /usr/local/nginx/geoip
cd /usr/local/nginx/geoip
wget http://www.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoIP.dat.gz 
gunzip GeoLiteCity.dat.gz 

编辑Nginx配置文件加载GeoIP数据库:

http
    [...]
geoip_country  /usr/local/nginx/geoip/GeoIP.dat; #国家数据库
geoip_city     /usr/local/nginx/geoip/GeoLiteCity.dat; #城市数据库
[...]

如需Nginx传递变量给PHP,编辑fastcgi_params添加:

fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

应用示例,Nginx判断如果访问者IP所在国家为美国或中国,返回404错误

server
    [...]
                if ($geoip_country_code ~* (US|CN)) {
                #return 404;
                }
[...]

新建PHP程序测试GeoIP:

<html>
<head>
 <title>IP地址检测,我的IP地址是多少?</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>您的代理IP地址是: ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>您的IP地址是 : $ipaddress";
    }
  $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
  $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
  $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
  $geoip_region = getenv(GEOIP_REGION);
  $geoip_city = getenv(GEOIP_CITY);
  $geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
  $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
  $geoip_latitude = getenv(GEOIP_LATITUDE);
  $geoip_longitude = getenv(GEOIP_LONGITUDE);
  echo "<br>国家 : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) ";
  echo "<br>地区 :  $geoip_region";
  echo "<br>城市 :  $geoip_city ";
  echo "<br>邮政编码 :  $geoip_postal_code";
  echo "<br>所在洲 :  $geoip_city_continent_code";
  echo "<br>纬度 :  $geoip_latitude ";
  echo "<br>经度 :   $geoip_longitude ";

?>
</body>
</html>

Nginx与PHP变量传递fastcgi_params

发布时间:July 1, 2012 // 分类:Nginx,PHP // No Comments

Nginx配置Fastcgi解析PHP时会调用fastcgi_params配置文件来传递服务器变量,默认内容如下:

#参数设定       #传递为PHP变量名    #Nginx自有变量,可自定义
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

可以修改配置文件设定自定义变量传递到php-cgi端:

fastcgi_param  haiyun      "Hello haiun";

新建php文件通过$_SERVER[""]变量测试:

<?
echo $_SERVER["haiyun"];
?>
curl www.haiyun.me/test.php
Hello haiyun

13-12-25更新,不推荐使用这种方式传递变量,会增加连接开销,可修改PHP-FPM配置文件传递变量:

env[haiyun] = test

HTTP代理级别及检测是否使用代理

发布时间:July 1, 2012 // 分类:PHP // No Comments

透明代理,Transparent Proxies

REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 代理服务器 IP (补充:这个字段由代理服务器填充,有时会填充网关信息等)
HTTP_X_FORWARDED_FOR = 您的真实 IP

匿名代理,Anonymous Proxies

REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 代理服务器 IP (补充:这个字段由代理服务器填充,有时会填充网关信息等)
HTTP_X_FORWARDED_FOR = 代理服务器 IP

高匿名代理,High Anonymity Proxies/Elite Proxies
REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 没数值或不显示
HTTP_X_FORWARDED_FOR = 没数值或不显示
PHP检测客户端是否使用代理:

<?php
header("Content-type: text/html; charset=utf-8");
if(!empty($_SERVER['HTTP_VIA']))    //使用了代理
{
  if(!isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  {
    echo "Anonymous Proxies    普通匿名代理服务器";
    //代理IP地址为 $_SERVER['REMOTE_ADDR']
  }
  else
  {
    echo "Transparent Proxies 透明代理服务器";
    //代理IP地址为 $_SERVER['REMOTE_ADDR']
    //真实ip地址为 $_SERVER['HTTP_X_FORWARDED_FOR']
  }
}
else    
{
  echo "没有代理或者是高匿名代理";
  //真实ip地址为 $_SERVER['REMOTE_ADDR']
}
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...