{"id":99,"date":"2016-10-23T00:24:51","date_gmt":"2016-10-22T15:24:51","guid":{"rendered":"http:\/\/www.dogrow.net\/php\/?p=99"},"modified":"2017-09-01T17:15:25","modified_gmt":"2017-09-01T08:15:25","slug":"blog8","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/php\/blog8\/","title":{"rendered":"(8) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #3 (\u4e8c\u91cd\u30ab\u30a6\u30f3\u30c8\u56de\u907f)"},"content":{"rendered":"<h1 class=my_h>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068<\/h1>\n<p><a href=\"\/\/www.dogrow.net\/php\/archives\/90\">\u300c(7) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #2\u300d<\/a> \u3092\u6539\u9020\u3057\u3001\u540c\u3058IP\u30a2\u30c9\u30ec\u30b9\u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u306f1\u65e5\u306b1\u56de\u3060\u3051\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3088\u3046\u306b\u5909\u66f4\u3059\u308b\u3002<br \/>\n<a href=\"\/\/www.dogrow.net\/php\/sample\/00008\/\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/00007_1.png\" alt=\"00007_1\" width=\"320\" height=\"212\" class=\"alignnone size-full wp-image-91\" style=\"margin-top:1rem;\" srcset=\"https:\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/00007_1.png 320w, https:\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/00007_1-300x199.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p>\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"\/\/www.dogrow.net\/php\/sample\/00008\/\" target=\"_blank\">\/\/www.dogrow.net\/php\/sample\/00008\/<\/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=my_pre>\r\n\u300c(7) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #2\u300d\u3068\u540c\u3058\r\n<\/pre>\n<div class=trow><\/div>\n<h3 class=my_h3>counter_img.php<\/h3>\n<pre class=my_pre>\r\n\u300c(7) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #2\u300d\u3068\u540c\u3058\r\n<\/pre>\n<h3 class=my_h>CCounter.php<\/h3>\n<p>\u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306e\u30c6\u30fc\u30d6\u30eb acclog \u3092\u65b0\u898f\u8ffd\u52a0\u3057\u305f\u3002<\/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  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\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  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ DB\u63a5\u7d9a\r\n  function connect_db(){\r\n    if($this-&gt;m_db == NULL){\r\n      \/\/ DB\u30d5\u30a1\u30a4\u30eb\u5b58\u5728\u78ba\u8a8d\r\n      $isDbExist = file_exists($this-&gt;m_db_fpath);\r\n      try {\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        \/\/ DB\u306f\u65b0\u898f\u4f5c\u6210\uff1f\r\n        if($isDbExist === FALSE){\r\n          \/\/ \u30ab\u30a6\u30f3\u30c8\u5024\u7528\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u3001\u30ab\u30a6\u30f3\u30c8\u5024\u30920\u3067\u521d\u671f\u5316\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;INSERT INTO `mytbl` (`cnt`,`dt_lastacc`) VALUES(0, &quot;.time().&quot;);&quot;);\r\n          \/\/ \u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u7528\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `acclog` (`ipaddr` TEXT, `accfrom` TEXT, `acctime` TEXT);&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  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ DB\u5207\u65ad\r\n  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  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ \u30ab\u30a6\u30f3\u30bf\u66f4\u65b0\uff06\u66f4\u65b0\u5024\u3092\u53d6\u5f97\r\n  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        \/\/ \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\r\n          $this-&gt;m_db-&gt;exec(&quot;UPDATE `mytbl` SET `cnt`=`cnt`+1,`dt_lastacc`='$dt_now';&quot;);\r\n          \/\/ \u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u66f4\u65b0\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        }\r\n        \/\/ \u30ab\u30a6\u30f3\u30bf\u306e\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      }catch(Exception $e){\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $cnt;\r\n  }\r\n\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ \u65e5\u4ed8\u5909\u66f4\u6642\u306b\u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u3092\u30af\u30ea\u30a2\r\n  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        \/\/ \u65e5\u4ed8\u5909\u66f4\uff1f\r\n        if($day_now != $day_last){\r\n          \/\/ \u30a2\u30af\u30bb\u30b9\u30ed\u30b0\u3092\u524a\u9664\r\n          $this-&gt;m_db-&gt;exec(&quot;DELETE FROM `acclog`;&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  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ \u4e8c\u91cd\u30ab\u30a6\u30f3\u30c8\u30c1\u30a7\u30c3\u30af\r\n  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 COUNT(*) FROM `acclog` WHERE `ipaddr`='$ipaddr';&quot;);\r\n        $row = $rs-&gt;fetchArray();\r\n        if($row&#x5B;0] &gt; 0){\r\n          $ret = TRUE;      \/\/ \u30a2\u30af\u30bb\u30b9\u5143IP\u30a2\u30c9\u30ec\u30b9\u304c\u767b\u9332\u6e08\u307f\u3002\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<p>\u3000<br \/>\n\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"\/\/www.dogrow.net\/php\/sample\/00008\/\" target=\"_blank\">\/\/www.dogrow.net\/php\/sample\/00008\/<\/a><\/p>\n<hr class=my_hr_bottom>\n","protected":false},"excerpt":{"rendered":"<p>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068 \u300c(7) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #2\u300d \u3092\u6539\u9020\u3057\u3001\u540c\u3058IP\u30a2\u30c9\u30ec\u30b9\u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u306f1\u65e5\u306b1\u56de\u3060\u3051\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3088\u3046\u306b\u5909\u66f4\u3059\u308b\u3002 \u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002 \/\/www.dogrow.net\/\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/php\/blog8\/\">\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,2],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-sqlite","category-2"],"views":2300,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/99","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=99"}],"version-history":[{"count":15,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":793,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/99\/revisions\/793"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}