{"id":1532,"date":"2016-07-07T16:33:00","date_gmt":"2016-07-07T08:33:00","guid":{"rendered":"http:\/\/www.youthtribe.com\/?p=1532"},"modified":"2016-07-07T16:33:00","modified_gmt":"2016-07-07T08:33:00","slug":"%e5%b0%86ip%e7%ba%af%e7%9c%9f%e5%ba%93%e7%9a%84%e6%95%b0%e6%8d%ae%e5%af%bc%e5%85%a5%e5%88%b0mysql%e4%b8%ad%e5%8e%bb%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.youthtribe.com\/archives\/1532","title":{"rendered":"\u5c06IP\u7eaf\u771f\u5e93\u7684\u6570\u636e\u5bfc\u5165\u5230mysql\u4e2d\u53bb\u7684\u65b9\u6cd5"},"content":{"rendered":"

<!DOCTYPE html PUBLIC “-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN” “http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd”>
\n<html xmlns=”http:\/\/www.w3.org\/1999\/xhtml”>
\n<head>
\n<meta http-equiv=”Content-Type” content=”text\/html; charset=utf-8″ \/>
\n<title>\u65e0\u6807\u9898\u6587\u6863<\/title>
\n<\/head><\/p>\n

<body>
\n<?php
\n\/\/mysql<\/p>\n

$dsn = ‘mysql:host=localhost’.’;dbname=name’;<\/p>\n

$pdo = new PDO($dsn, “username”, “ps”);
\n$pdo->query(“set names utf8”);
\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\n$handle = fopen(“ips.txt”, “r”);
\n$i = 0;
\nif ($handle) {
\nwhile (!feof($handle))
\n{
\n\/*
\nif ($i >100)
\n{
\nbreak;
\n}
\n$i++;
\n*\/
\n$str = fgets($handle, 4096);
\necho $str.”<br>”;
\n$ip_start = substr($str,0,15);
\n$ip_end = substr($str,16,15);
\n$ip_start = str_replace(” “,””,$ip_start);
\n$ip_end = str_replace(” “,””,$ip_end);<\/p>\n

$county_region = substr($str,32,strlen($str)-1);
\n\/\/$county_region = mb_convert_encoding($county_region, “utf-8”, “utf-8”);
\necho $ip_start.”==”.$ip_end.”==”.$county_region.”<br>”;<\/p>\n

$ip_start_to_long_int = ip_to_long_int($ip_start);
\n$ip_end_to_long_int = ip_to_long_int($ip_end);<\/p>\n

\/\/insert into mysql
\n$sql = “insert into `ips`(`ip_start`,`ip_start_number`,`ip_end`,`ip_end_number`,`info`) values( ‘$ip_start’,$ip_start_to_long_int ,’$ip_end’,$ip_end_to_long_int,’$county_region’)”;
\n$res = $pdo->exec($sql);
\nif ($res == 0)
\n{
\necho $sql.”<br>”;
\n}
\n}
\n}
\necho “ok”;
\nfunction ip_to_long_int($ip){<\/p>\n

$arr = explode(‘.’,$ip);
\n$res = $arr[0]*255*255*255 + $arr[1]*255*255 + $arr[2]*255 + $arr[3];
\nreturn $res;<\/p>\n

}
\n?>
\n<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"

<!DOCTYPE html PUBLIC “-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN” “http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd”> <html xmlns=”http:\/\/www.w3.org\/1999\/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text\/html; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,224,9],"tags":[265,202],"_links":{"self":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/1532"}],"collection":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/comments?post=1532"}],"version-history":[{"count":1,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/1532\/revisions"}],"predecessor-version":[{"id":1533,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/1532\/revisions\/1533"}],"wp:attachment":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/media?parent=1532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/categories?post=1532"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/tags?post=1532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}