学习笔记1(将访问记录写入到列表并且显示在浏览器)

来源:互联网 发布:电脑网络防火墙设置 编辑:程序博客网 时间:2024/06/06 07:48

mysql出现登陆不进去的问题可以重启试试

TP报错storage_write_error
solution:chmod -R 777 usr/share/ngnix
case1:将访问记录写入到列表并且显示在浏览器
Log.class.php

<?php /* * 日志记录 * 每天生成一个日志文件,当文件超过指定大小则备份日志文件并重新生成新的日志文件 **/class Log {    private $maxsize = 1024000; //最大文件大小1M    //写入日志    public function writeLog($filename,$msg){        $res = array();        $res['msg'] = $msg;        $res['logtime'] = date("Y-m-d H:i:s",time());        //如果日志文件超过了指定大小则备份日志文件        if(file_exists($filename) && (abs(filesize($filename)) > $this->maxsize)){            $newfilename = dirname($filename).'/'.time().'-'.basename($filename);            rename($filename, $newfilename);        }        //如果是新建的日志文件,去掉内容中的第一个字符逗号        if(file_exists($filename) && abs(filesize($filename))>0){            $content = ",".json_encode($res);        }else{            $content = json_encode($res);        }        //往日志文件内容后面追加日志内容     file_put_contents($filename, $content, FILE_APPEND);    }    //读取日志    public function readLog($filename){        if(file_exists($filename)){            $content = file_get_contents($filename);            $json = json_decode('['.$content.']',true);        }else{            $json = '{"msg":"The file does not exist."}';        }        return $json;    }} ?>

index.php文件

<?phprequire_once('Log.class.php');$filename = "logs/log_" . date("Ymd", time()) . ".txt";$msg = array(    'ip' => $_SERVER["REMOTE_ADDR"],    'user' => getBrowser());$Log = new Log();$Log->writeLog($filename, $msg);$loglist = $Log->readLog($filename);//获取浏览器类型function getBrowser() {    $user_OSagent = $_SERVER['HTTP_USER_AGENT'];    if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {        $visitor_browser = "Maxthon(Microsoft IE)";    } elseif (strpos($user_OSagent, "Maxthon 2.0")) {        $visitor_browser = "Maxthon 2.0";    } elseif (strpos($user_OSagent, "Maxthon")) {        $visitor_browser = "Maxthon";    } elseif (strpos($user_OSagent, "Edge")) {        $visitor_browser = "Edge";    } elseif (strpos($user_OSagent, "Trident")) {        $visitor_browser = "IE";    } elseif (strpos($user_OSagent, "MSIE")) {        $visitor_browser = "IE";    } elseif (strpos($user_OSagent, "MSIE")) {        $visitor_browser = "MSIE 较高版本";    } elseif (strpos($user_OSagent, "NetCaptor")) {        $visitor_browser = "NetCaptor";    } elseif (strpos($user_OSagent, "Netscape")) {        $visitor_browser = "Netscape";    } elseif (strpos($user_OSagent, "Chrome")) {        $visitor_browser = "Chrome";    } elseif (strpos($user_OSagent, "Lynx")) {        $visitor_browser = "Lynx";    } elseif (strpos($user_OSagent, "Opera")) {        $visitor_browser = "Opera";    } elseif (strpos($user_OSagent, "MicroMessenger")) {        $visitor_browser = "微信浏览器";    } elseif (strpos($user_OSagent, "Konqueror")) {        $visitor_browser = "Konqueror";    } elseif (strpos($user_OSagent, "Mozilla/5.0")) {        $visitor_browser = "Mozilla";    } elseif (strpos($user_OSagent, "Firefox")) {        $visitor_browser = "Firefox";    } elseif (strpos($user_OSagent, "U")) {        $visitor_browser = "Firefox";    } else {        $visitor_browser = "其它";    }     return $visitor_browser;}?><!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; charset=utf-8" />         <title>PHP添加日志到txt文件并在表格上读取日志</title>        <meta name="keywords" content="PHP添加日志函数,PHP日志类演示,PHP如何记录项目日志"/>        <meta name="description" content="我们在调试过程中,经常遇到日志打印,有的时候我们直接打印是看不到的,比如app接口传参,在线支付调试等。本文中的添加日志函数,可以解决这样的问题。"/>        <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"/>    </head>    <body>        <div style="width:800px;margin:auto">            <table class="table table-hover">                <thead>                    <tr>                        <th>#</th>                        <th>时间</th>                        <th>IP</th>                        <th>浏览器</th>                    </tr>                </thead>                <tbody>                    <?php                    $i = 1;                    foreach ($loglist as & $val) {                        ?>                        <tr>                            <th scope="row"><?php echo $i; ?></th>                             <td><?php echo $val['logtime']; ?></td>                            <td><?php echo $val['msg']['ip']; ?></td>                            <td><?php echo $val['msg']['user']; ?></td>                        </tr>                        <?php $i++;                    } ?>                </tbody>            </table>        </div>    </body></html><!-- 以下是统计及其他信息,与演示无关,不必理会 --><div style="clear:both"></div><p class="vad">    <a href="http://www.sucaihuo.com/" target="_blank">sucaihuo.com</a>    <a href="http://www.sucaihuo.com/js/903.html" target="_blank">说 明</a>    <a href="http://www.sucaihuo.com/js/903.html" target="_blank">下 载</a></p><style type="text/css">    .vad { margin: 120px 0 5px; font-family: Consolas,arial,宋体,sans-serif; text-align:center;}    .vad a { display: inline-block; height: 36px; line-height: 36px; margin: 0 5px; padding: 0 50px; font-size: 14px; text-align:center; color:#eee; text-decoration: none; background-color: #222;}    .vad a:hover { color: #fff; background-color: #000;}    .thead { width: 728px; height: 90px; margin: 0 auto; border-bottom: 40px solid #fff;}</style>

1要自己创建logs文件夹否则浏览器的列表显示会一直为空
2列表没有内容可能也是没有写入日志的权限
3file_put_contents 返回存入数据的字节数

流程:
1信息存入数组
2声明日志变量
3信息写入日志
4读出日志内容
5在html显示

0 0
原创粉丝点击