{"id":1472,"date":"2018-01-19T19:22:53","date_gmt":"2018-01-19T19:22:53","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/logging-with-php\/"},"modified":"2018-01-19T19:24:49","modified_gmt":"2018-01-19T19:24:49","slug":"logging-with-php","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/logging-with-php\/","title":{"rendered":"Logging With PHP"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Tim Perdue<\/div>\n<div class=\"\">on July 30, 2000<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/tim2.jpg\" class=\"articleAuthorImg\" alt=\"picture of Tim Perdue\" height=\"90\" width=\"100\" align=\"left\"\/><\/p>\n<div class=\"articlePara\">\nFiguring out where your hits are coming from and which<br \/>\npages are being viewed is not too difficult if you use a<br \/>\ngood log analyzer like Analog or something similar.<\/div>\n<div class=\"articlePara\">\nBut if you want to pull up reports on the fly at any time,<br \/>\nyou need to take a different route.<\/div>\n<div class=\"articlePara\">\nFor about six months, I was importing my Apache logs into<br \/>\nan SQL database and then running ad-hoc queries against that.<\/div>\n<div class=\"articlePara\">\nUnfortunately, that requires me to go out and manually grab<br \/>\nthose log files, tweak the format a bit, then import. I wanted<br \/>\nsomething better &#8211; something that was continually updated,<br \/>\nflexible enough to simultaneously support multiple sites,<br \/>\nand multiple actions (page view, ad click, click-in, click-out,<br \/>\nplus more if needed).<\/div>\n<div class=\"articlePara\">\nI sat down and came up with the following table:<\/div>\n<div class=\"example\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><br \/>\ncreate\u00a0table\u00a0tbl_activity_log\u00a0(<br \/>\n<br \/>fld_date\u00a0int,\u00a0\u00a0\u00a0\u00a0\/\/ex.\u00a019990101<br \/>\n<br \/>fld_hours\u00a0text,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/Which\u00a0hour\u00a0of\u00a0the\u00a0day?<br \/>\n<br \/>fld_remote_ip\u00a0text,\u00a0\u00a0\/\/IP\u00a0of\u00a0visitor<br \/>\n<br \/>fld_action\u00a0int,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/page\u00a0view,\u00a0click,\u00a0etc<br \/>\n<br \/>fld_special\u00a0text,\u00a0\u00a0\u00a0\u00a0\/\/which\u00a0page\u00a0or\u00a0category?<br \/>\n<br \/>fld_affil_num\u00a0int\u00a0\u00a0\u00a0\u00a0\/\/unique\u00a0for\u00a0each\u00a0web\u00a0site<br \/>\n<br \/>);<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nSomeone who really knows what they&#8217;re doing would have used a<br \/>\ntimestamp field rather than <i>int fld_date<\/i>, but this works<br \/>\nfor me. You could also add another column to hold the <i>$PATH_INFO<\/i>,<br \/>\nbut I decided the Special column gives me what I want.<\/div>\n<div class=\"articlePara\">\nI also defined these indexes, as I knew I would be querying on<br \/>\nany of these fields:<\/div>\n<div class=\"example\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><br \/>\nINDEXES<br \/>\n<br \/>idx_logger_date<br \/>\n<br \/>idx_logger_hour<br \/>\n<br \/>idx_logger_ip<br \/>\n<br \/>idx_logger_action<br \/>\n<br \/>idx_logger_special<br \/>\n<br \/>idx_logger_affil_num<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nThe next step was getting the info into the tables. It would be<br \/>\na perfect world if all pages were served through PHP and all of your<br \/>\nvarious web sites existed on one box. Unfortunately, I have an array<br \/>\nof servers scattered all over the country, and I want to collect this<br \/>\ninfo from every server for every page view 24 hours a day.<\/div>\n<div class=\"articlePara\">\nSo that required me to use the 1&#215;1 pixel GIF trick. I have a GIF<br \/>\non every page that looks like this:<\/div>\n<div class=\"example\">\n<pre>\n&lt;IMG SRC=\"http:\/\/www.yourserver.com\/util\/gif11.php3?c=4&amp;s=phpbuildercom&amp;b=77\" \nheight=1 width=1&gt;\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\n<b>gif11.php3<\/b> is a simple script that resides on my central server.<br \/>\nI have included the source on the next page.<br \/>\nSince the gif is on each page, and is forced to load due to the random<br \/>\nnumber at the end (the b=xxxx), a request is sent back to the central<br \/>\nserver for each and every page view.<\/div>\n<div class=\"articlePara\">\nIf all of your pages and the database reside on one server, you don&#8217;t<br \/>\nneed to use the GIF trick &#8211; you can insert the logging code into the<br \/>\nheader of your page. <\/div>\n<div class=\"articlePara\">\nThe same logging system can be used to track click-thrus on ad banners:<\/div>\n<div class=\"example\">\n<pre>\n&lt;A HREF=\"http:\/\/www.yourserver.com\/util\/adclick.php3?c=4&amp;goto= * &gt;\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nJust replace * with the URL you want to redirect to. Again here, the<br \/>\n$c variable should be changed for each web site.<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim199901304658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim199901309ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"tim199901304658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Tim Perdue on July 30, 2000 Figuring out where your hits are coming from and which pages are being viewed is not too difficult if you use a good log analyzer like Analog or something similar. But if you want to pull up reports on the fly at any&#8230; <a href=\"https:\/\/phpbuilder.com\/logging-with-php\/\" class=\"readmore\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1472","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1472"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1472\/revisions"}],"predecessor-version":[{"id":2247,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1472\/revisions\/2247"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}