php+mysql文章浏览次数统计及发布时间
一个网页能显示出"文件上传时间"和"浏览人数",不仅是文章历史的纪录,也能反映出该文章的受众欢迎度,"文件上传时间"和"浏览人数"记录方法肯定很多,笔者根据自己的理解用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[] } }
Copy after login
网页显示部分即后面部分,代码如下:
<?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'];
Copy after login
文章网址:
随意转载^^但请附上教程地址。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
