This article will introduce you to an example of php+mysql article views statistics and release time. If you need this, feel free to refer to it.
A web page can display the "file upload time" and "number of viewers", which is not only a record of the article's history, but also reflects the article's popularity with the audience. There must be many ways to record "file upload time" and "number of viewers". The author wrote one using php+mysql based on my own understanding. I don't know if the code is optimized enough, but it feels good to use and there are no problems. I will write it down and share it with everyone. .
Thoughts
1. When uploading an article, first write the "webpage address", "upload time time()" and "counting starting point 0" in the database.
2. When the user opens the webpage, first use $_SESSION["article"] to determine whether it is online. If it is not online, open the database, take out the original count, add 1, and then update the database. Prevent online users from "refreshing" and inflating the count.
3. Open and take out the last updated count and display it on the web page.
Example
In front of write:
The code is as follows
代码如下 |
复制代码 |
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 (empty($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 code
|
代码如下 |
复制代码 |
$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'];
?>
|
session_start();
$stsfile = "10001.php";
$nowtime = time();
date_default_timezone_set("Asia/Chongqing");//Set the time standard
If (!isset($_SESSION['article']) || $_SESSION['article'] != $stsfile ) //Determine whether the user is online
{
$link = mysql_connect("localhost","library name","password") or die ("Failed to open database");
mysql_select_db("Library name",$link); //Connect to database
mysql_query("set names 'utf8'"); //Set access encoding |
//Check whether the record of $stsfile already exists. If it does not exist, insert the time and counting base 0. If it exists, add 1 and update the count.
$sql = "SELECT * FROM statistics WHERE `StsFile`= '$stsfile'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
If (empty($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; //Write $_SESSION[]
}
}
?>
The display part of the web page is the part after :
The code is as follows
|
Copy code
$link = mysql_connect("localhost","library name","password") or die ("Failed to open database");
mysql_select_db("Library name",$link); //Connect to database
mysql_query("set names 'utf8'"); //Set access encoding
$sql = "SELECT * FROM statistics WHERE `StsFile`= '$stsfile'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
echo "Upload time:".date("Y-m-d H:i:s",$row['StsTime'])." Number of views: ".$row['StsNumb'];
?>
http://www.bkjia.com/PHPjc/632919.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632919.htmlTechArticleThis article will introduce you to an example of php+mysql article views statistics and publishing time. If you have any questions about this Please refer to it if you need to enter. A web page can display when files are uploaded...
|
|