一个网页能显示出"文件上传时间"和"浏览人数",不仅是文章历史的纪录,也能反映出该文章的受众欢迎度,"文件上传时间"和"浏览人数"记录方法肯定很多,笔者根据自己的理解用php+mysql写了一个,不知代码够不够优化,但使用起来感觉不错,没有问题,今写出来和大家一起分享.
思路:
1、文章上传时先在数据库中写入"网页地址"、"上传时间time()"和"计数起点0"。
2、当用户打开网页时,先通过$_SESSION["article"]判断是否在线。如果不在线,则打开数据库,取出原有的计数,并加 1 ,再更新数据库。防止在线用户"刷新",虚增计数。
3、打开并取出最后更新的计数,显示在网页上。
举例,在前面写下如下代码:
<?php session_start(); $stsfile = "10001.php"; $nowtime = time(); date_default_timezone_set("Asia/Chongqing"); //设置时间标准 If (!isset($_SESSION['article']) || $_SESSION['article'] != $stsfile) //判断用户是否在线 { $link = mysql_connect("localhost", "库名", "密码") or die("打开数据库失败"); mysql_select_db("库名", $link); //连接数据库 mysql_query("set names 'utf8'"); //设置存取编码 //查询$stsfile的记录是否已经存在,如果不存在就插入时间及计数基数0,如果存在,则+1,更新计数。 $sql = "SELECT * FROM statistics WHERE `StsFile`= '$stsfile'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); If (emptyempty($row['StsID'])) { mysql_query("INSERT INTO `statistics`(`StsFile`,`StsTime`,`StsNumb`) VALUES ('$stsfile','$nowtime','0')"); session_start(); $_SESSION['article'] = $stsfile; } Else { $numb = $row['StsNumb'] + 1; mysql_query("UPDATE `statistics` SET `StsNumb` = '$numb' WHERE `StsFile` = '$stsfile'"); session_start(); $_SESSION['article'] = $stsfile; //写下$_SESSION[] } }
网页显示部分即后面部分,代码如下:
<?php $link = mysql_connect("localhost", "库名", "密码") or die("打开数据库失败"); mysql_select_db("库名", $link); //连接数据库 mysql_query("set names 'utf8'"); //设置存取编码 $sql = "SELECT * FROM statistics WHERE `StsFile`= '$stsfile'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo "上传时间:" . date("Y-m-d H:i:s", $row['StsTime']) . " 浏览数:" . $row['StsNumb'];
文章网址:
随意转载^^但请附上教程地址。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
