Category Archives: 编程

[PHP]根据User Agent判断是否搜索引擎蜘蛛

Leave a comment
/* 判断搜索引擎 摘自Discuz x3.2 */
function checkrobot($useragent=''){
  static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
  static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');

  $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
  if(strpos($useragent, 'http://') === false && dstrpos($useragent, $kw_browsers)) return false;
  if(dstrpos($useragent, $kw_spiders)) return true;
  return false;
}
function dstrpos($string, $arr, $returnvalue = false) {
  if(empty($string)) return false;
  foreach((array)$arr as $v) {
    if(strpos($string, $v) !== false) {
      $return = $returnvalue ? $v : true;
      return $return;
    }
  }
  return false;
}

Continue reading

MySQL 百万级分页优化

Leave a comment

一般刚开始学SQL的时候,会这样写
SELECT * FROM table ORDER BY id LIMIT 1000, 10;
但在数据达到百万级的时候,这样写会慢死
SELECT * FROM table ORDER BY id LIMIT 1000000, 10;
也许耗费几十秒
网上很多优化的方法是这样的
SELECT * FROM table WHERE id >= (SELECT id FROM table LIMIT 1000000, 1) LIMIT 10;
是的,速度提升到0.x秒了,看样子还行了
可是,还不是完美的! Continue reading

[PHP]PHP获取服务器IP输出为数组

Leave a comment

用PHP执行ifconfig获得Linux服务器IP并输出为数组,下面是代码:

function getServerIp(){ //用ifconfig读取服务器IP并输出为数组
		$ss = exec('/sbin/ifconfig | sed -n \'s/^ *.*addr:\\([0-9.]\\{7,\\}\\) .*$/\\1/p\'',$arr);
		return $arr; 
		}
$ips=getServerIp();
foreach($ips as $k=>$v){//过滤IP
	if(substr($v,0,3)=='127' || substr($v,0,3)=='10.' || substr($v,0,7)=='192.168' || substr($v,0,6)=='172.16'){
		unset($ips[$k]);
	}
}
shuffle($ips);//重新排序
print_r($ips);