{"id":247,"date":"2016-11-13T06:42:40","date_gmt":"2016-11-12T21:42:40","guid":{"rendered":"http:\/\/www.dogrow.net\/php\/?p=247"},"modified":"2017-09-01T17:13:19","modified_gmt":"2017-09-01T08:13:19","slug":"blog16","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/php\/blog16\/","title":{"rendered":"(16) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #4 (\u65e5\u5225\u8a2a\u554f\u6570\u3092\u8a18\u9332)"},"content":{"rendered":"<h1 class=my_h1>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068<\/h1>\n<p><a href=\"http:\/\/www.dogrow.net\/php\/?p=81\">(6) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #1<\/a><br \/>\n<a href=\"http:\/\/www.dogrow.net\/php\/?p=90\">(7) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #2 (\u753b\u50cf\u5316)<\/a><br \/>\n<a href=\"http:\/\/www.dogrow.net\/php\/?p=99\">(8) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #3 (\u4e8c\u91cd\u30ab\u30a6\u30f3\u30c8\u56de\u907f)<\/a><br \/>\n\u306e\u7d9a\u304d\u3001\u4eca\u56de\u306fDB\u306b\u65e5\u5225\u306e\u30a2\u30af\u30bb\u30b9\u6570\u3092\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306b\u6539\u9020\u3057\u3066\u307f\u308b\u3002<\/p>\n<p>\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"http:\/\/www.dogrow.net\/php\/sample\/00016\/\" target=\"_blank\">http:\/\/www.dogrow.net\/php\/sample\/00016\/<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/11\/Image1.gif\" \/><\/a><\/p>\n<h1 class=my_h>\u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210<\/h1>\n<h3 class=my_h>index.php<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;utf-8&quot; \/&gt;\r\n&lt;title&gt;SQLite\u30b5\u30f3\u30d7\u30eb&lt;\/title&gt;\r\n&lt;style type=&quot;text\/css&quot;&gt;\r\nimg.imini {\r\n  height:1.2rem;\r\n  vertical-align:middle;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;\/body&gt;\r\n\r\n&lt;?php\r\n  echo &quot;&lt;img src=\\&quot;counter_img.php?typ=1&amp;dig=4\\&quot;&gt;&lt;br \/&gt;\\n&quot;;\r\n  echo &quot;&lt;img src=\\&quot;counter_img.php?typ=2&amp;dig=6\\&quot;&gt;&lt;hr \/&gt;\\n&quot;;\r\n  for($i=0 ; $i&lt;=30;$i++){\r\n    echo $i.&quot;\u65e5\u524d &lt;img class=imini src=\\&quot;counter_img.php?typ=1&amp;dig=3&amp;pst=&quot;.$i.&quot;\\&quot;&gt;&lt;br \/&gt;\\n&quot;;\r\n  }\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3 class=my_h>counter_img.php<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n  define(&quot;LC_DIG_IMG_DIR&quot;, &quot;.\/_counter\/&quot;);\r\n  \/\/----------------------------------------------\r\n  \/\/ \u5f15\u6570\u3092\u53d6\u5f97\r\n  $typ = $_GET&#x5B;'typ'];          \/\/ \u753b\u50cf\u30bf\u30a4\u30d7\r\n  $dig = $_GET&#x5B;'dig'];          \/\/ \u6841\u6570\r\n  \/\/ \u904e\u53bb\u65e5\u6307\u5b9a\r\n  if(array_key_exists('pst',$_GET)===TRUE){\r\n    $pst = intval($_GET&#x5B;'pst']);    \/\/ \u904e\u53bb\u65e5\u6307\u5b9a(0\uff5en)\r\n    if($pst &gt; 9999){ $pst = 0; }\r\n  }else{\r\n    $pst = -1;    \/\/ \u975e\u904e\u53bb\u65e5\u6307\u5b9a\uff08\u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7\u3042\u308a\uff09\r\n  }\r\n  \/\/ \u8868\u793a\u6841\u6570\r\n  if($dig &gt; 10){  \/\/ \u6700\u592710\u6841\u8868\u793a\u307e\u3067\r\n    $dig = 10;\r\n  }\r\n  \/\/----------------------------------------------\r\n  \/\/ \u30ab\u30a6\u30f3\u30bf\u64cd\u4f5c\r\n  require_once(LC_DIG_IMG_DIR.&quot;CCounter.php&quot;);\r\n  $cCnt = new CCounter();\r\n  $cCnt-&gt;connect_db();          \/\/ DB\u63a5\u7d9a\r\n  if($pst &lt; 0){                 \/\/ \u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7\uff06\u30c8\u30fc\u30bf\u30eb\u5024\u3092\u53d6\u5f97\r\n    $cnt = $cCnt-&gt;count_up();\r\n  }else{                        \/\/ \u904e\u53bb\uff11\u65e5\u6307\u5b9a\uff1f\r\n    $cnt = $cCnt-&gt;get_cnt_pastday($pst);\r\n  }\r\n  $cCnt-&gt;disconnect_db();       \/\/ DB\u5207\u65ad\r\n  \/\/----------------------------------------------\r\n  \/\/ \u753b\u50cf\u4f5c\u6210\r\n  $digImg = array();\r\n  $w = 0; $h = 0;\r\n  for($i=0 ; $i &lt; $dig ; $i++){\r\n    $v = $cnt % 10;\r\n    $imgPath = LC_DIG_IMG_DIR.$typ.&quot;\/&quot;.$v.&quot;.gif&quot;;\r\n    $digImg&#x5B;] = imagecreatefromgif($imgPath);\r\n    if($w == 0){\r\n      list($w, $h) = getimagesize($imgPath);\r\n    }\r\n    $cnt = intval($cnt \/ 10);\r\n  }\r\n  $digImg = array_reverse($digImg);\r\n  \/\/ \u7d50\u5408\u753b\u50cf\u3092\u4f5c\u6210\r\n  $newImg = imagecreate($w * $dig, $h);\r\n  $x = 0;\r\n  foreach($digImg as $oneImg){\r\n    imagecopy($newImg, $oneImg, $x, 0, 0, 0, $w, $h);\r\n    imagedestroy($oneImg);\r\n    $x += $w;\r\n  }\r\n  \/\/----------------------------------------------\r\n  \/\/ \u753b\u50cf\u51fa\u529b\r\n  header('Content-Type: image\/gif');\r\n  imagegif($newImg);\r\n  imagedestroy($newImg);\r\n?&gt;\r\n<\/pre>\n<h3 class=my_h>CCounter.php<\/h3>\n<p>\u65e5\u5225\u30a2\u30af\u30bb\u30b9\u6570\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306e\u30c6\u30fc\u30d6\u30eb accdays \u3092\u65b0\u898f\u8ffd\u52a0\u3057\u305f\u3002<br \/>\n<span class=\"my_fs_small1 my_fc_darkgray\">\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u3054\u3068\u304d\u3067\u3053\u3093\u306a\u306b\u5927\u304d\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u306a\u308b\u306e\u306f\u4f55\u304b\u304a\u304b\u3057\u3044\u304b&#8230;<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nclass CCounter {\r\n  private $m_db;        \/\/ SQLite3 instance\r\n  private $m_db_fpath;  \/\/ DB\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\r\n\r\n  \/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\r\n  function __construct( $fpath = &quot;cnt.db&quot; ) {\r\n    $this-&gt;m_db = NULL;\r\n    $this-&gt;m_db_fpath = $fpath;\r\n  }\r\n\r\n  \/\/ DB\u63a5\u7d9a\r\n  public function connect_db(){\r\n    if($this-&gt;m_db == NULL){\r\n      $isDbExist = file_exists($this-&gt;m_db_fpath);\r\n      \/\/ DB\u63a5\u7d9a\r\n      $this-&gt;m_db = new SQLite3($this-&gt;m_db_fpath);\r\n      $this-&gt;m_db-&gt;enableExceptions(TRUE);\r\n      if($isDbExist === FALSE){   \/\/ DB\u306f\u65b0\u898f\u4f5c\u6210\uff1f\r\n        try {\r\n          $this-&gt;m_db-&gt;exec('begin');\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `mytbl` (`cnt` INTEGER, `dt_lastacc` INTEGER);&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `acclog` (`ipaddr` TEXT, `accfrom` TEXT, `acctime` INTEGER);&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `accdays` (`dcnt` INTEGER, `dt_today` INTEGER);&quot;);\r\n          $dt_now = time();\r\n          $dt_today = strftime(&quot;%G%m%d&quot;, $dt_now);\r\n          $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `mytbl` (`cnt`,`dt_lastacc`) VALUES('0','&quot;.$dt_now.&quot;');&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `accdays` (`dcnt`,`dt_today`) VALUES('0','&quot;.$dt_today.&quot;');&quot;);\r\n          $this-&gt;m_db-&gt;exec('commit');\r\n        }catch(Exception $e){\r\n          $this-&gt;m_db-&gt;exec('rollback');\r\n          echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ DB\u5207\u65ad\r\n  public function disconnect_db(){\r\n    if($this-&gt;m_db != NULL){\r\n      $this-&gt;m_db-&gt;close();\r\n      $this-&gt;m_db = NULL;\r\n    }\r\n  }\r\n\r\n  \/\/ \u30ab\u30a6\u30f3\u30bf\u66f4\u65b0\uff06\u66f4\u65b0\u5024\u3092\u53d6\u5f97\r\n  public function count_up(){\r\n    $cnt = 0;\r\n    if($this-&gt;m_db != NULL){\r\n      \/\/ \u65e5\u4ed8\u5909\u66f4\u306a\u3089\u3070\u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u3092\u524a\u9664\r\n      $this-&gt;clear_acclog_if_date_changed();\r\n      try {\r\n        $this-&gt;m_db-&gt;exec('begin');\r\n        \/\/ \u4e8c\u91cd\u30ab\u30a6\u30f3\u30c8\u30c1\u30a7\u30c3\u30af\r\n        if($this-&gt;is_ipaddr_exist() === FALSE){\r\n          $dt_now = time();\r\n          \/\/ \u30ab\u30a6\u30f3\u30bf\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3001\u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u3092\u66f4\u65b0\r\n          $this-&gt;m_db-&gt;exec(&quot;UPDATE `mytbl` SET `cnt`=`cnt`+1,`dt_lastacc`='$dt_now';&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `acclog` (`ipaddr`,`accfrom`,`acctime`) VALUES('&quot;.$_SERVER&#x5B;&quot;REMOTE_ADDR&quot;].&quot;','','$dt_now');&quot;);\r\n          \/\/ \u4eca\u65e5\u306e\u30a2\u30af\u30bb\u30b9\u6570\u3092\u66f4\u65b0\r\n          $dt_today = strftime(&quot;%G%m%d&quot;, $dt_now);\r\n          $this-&gt;m_db-&gt;exec(&quot;UPDATE `accdays` SET `dcnt`=`dcnt`+1 WHERE `dt_today`='$dt_today';&quot;);\r\n        }\r\n        $this-&gt;m_db-&gt;exec('commit');\r\n      }catch(Exception $e){\r\n        $this-&gt;m_db-&gt;exec('rollback');\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n      \/\/ \u30ab\u30a6\u30f3\u30bf\u5024\u3092\u53d6\u5f97\r\n      $rs = $this-&gt;m_db-&gt;query(&quot;SELECT `cnt` FROM `mytbl`;&quot;);\r\n      $row = $rs-&gt;fetchArray();\r\n      $cnt = $row&#x5B;'cnt'];\r\n    }\r\n    return $cnt;\r\n  }\r\n\r\n  \/\/ n\u65e5\u524d\u306e\u30a2\u30af\u30bb\u30b9\u6570\u3092\u53d6\u5f97\r\n  public function get_cnt_pastday( $nday ){\r\n    $ret = 0;\r\n    if($this-&gt;m_db != NULL){\r\n      try {\r\n        $dt_past = strftime(&quot;%G%m%d&quot;, time() - (60 * 60 * 24 * $nday));\r\n        $rs = $this-&gt;m_db-&gt;query(&quot;SELECT * FROM `accdays` WHERE `dt_today`='$dt_past';&quot;);\r\n        if($rs !== FALSE){\r\n          $row = $rs-&gt;fetchArray();\r\n          if(isset($row&#x5B;'dcnt'])){\r\n            $ret = $row&#x5B;'dcnt'];\r\n          }else{\r\n            $ret = 0;   \/\/ \u6307\u5b9a\u65e5\u306e\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306f\u3053\u3061\u3089\u306b\u5165\u308b\u3002\r\n          }\r\n        }\r\n      }catch(Exception $e){\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $ret;\r\n  }\r\n\r\n  \/\/ \u65e5\u4ed8\u5909\u66f4\u6642\u306b\u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u3092\u30af\u30ea\u30a2\r\n  public function clear_acclog_if_date_changed(){\r\n    if($this-&gt;m_db != NULL){\r\n      $day_now = strftime(&quot;%G%m%d&quot;, time());\r\n      try {\r\n        \/\/ \u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u6700\u7d42\u66f4\u65b0\u65e5\u3092\u53d6\u5f97\r\n        $rs = $this-&gt;m_db-&gt;query(&quot;SELECT `dt_lastacc` FROM `mytbl`;&quot;);\r\n        $row = $rs-&gt;fetchArray();\r\n        $day_last = strftime(&quot;%G%m%d&quot;, $row&#x5B;'dt_lastacc']);\r\n        if($day_now != $day_last){    \/\/ \u65e5\u4ed8\u5909\u66f4\uff1f\r\n          $this-&gt;m_db-&gt;exec(&quot;DELETE FROM `acclog`;&quot;);   \/\/ \u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u524a\u9664\r\n          \/\/ \u65e5\u5225\u30a2\u30af\u30bb\u30b9\u6570\u8a18\u9332\u7528\u30ec\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\r\n          $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `accdays` (`dcnt`,`dt_today`) VALUES('0','$day_now');&quot;);\r\n        }\r\n      }catch(Exception $e){\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ \u4e8c\u91cd\u30ab\u30a6\u30f3\u30c8\u30c1\u30a7\u30c3\u30af\r\n  public function is_ipaddr_exist(){\r\n    $ret = FALSE;\r\n    if($this-&gt;m_db != NULL){\r\n      try {\r\n        $ipaddr = $_SERVER&#x5B;&quot;REMOTE_ADDR&quot;];\r\n        $rs = $this-&gt;m_db-&gt;query(&quot;SELECT `ipaddr` FROM `acclog` WHERE `ipaddr`='$ipaddr' LIMIT 1;&quot;);\r\n        if($rs !== FALSE){              \/\/ \u30af\u30a8\u30ea\u6210\u529f\uff1f\r\n          $row = $rs-&gt;fetchArray();\r\n          if(isset($row&#x5B;'ipaddr'])){    \/\/ \u78ba\u304b\u306b\u53d6\u5f97\u3067\u304d\u3066\u3044\u308b\uff1f\r\n            $ret = TRUE;      \/\/ \u30a2\u30af\u30bb\u30b9\u5143IP\u30a2\u30c9\u30ec\u30b9\u304c\u767b\u9332\u6e08\u307f\u3002\r\n          }\r\n        }\r\n      }catch(Exception $e){\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $ret;\r\n  }\r\n}\r\n?&gt;\r\n<\/pre>\n<hr class=my_hr_bottom>\n","protected":false},"excerpt":{"rendered":"<p>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068 (6) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #1 (7) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #2 (\u753b\u50cf\u5316) (8) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #3 (\u4e8c\u91cd\u30ab\u30a6\u30f3\u30c8\u56de\u907f) \u306e\u7d9a\u304d\u3001\u4eca\u56de\u306fDB\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/php\/blog16\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"views":2247,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":30,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/247\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}