Php+mysql article browsing times statistics and publishing time

Source: Internet
Author: User
Tags file upload set time

A Web page can show "File upload Time" and "number of visitors", not only the history of the article record, but also reflects the popularity of the article's audience. "File Upload Time" and "number of visitors" record method must be many, the author according to their own understanding with Php+mysql wrote a, I do not know the code is not enough to optimize, but it feels good to use, no problem, write to share with everyone.

Ideas

1, the article upload first in the database to write "Web address", "Upload Time ()" and "Count start 0".

2, when users open the Web page, first through the $_session["article"] to determine whether online. If not, open the database, remove the original count, add 1, and then update the database. Prevent online users from "refreshing", virtual increment count.

3, open and remove the last updated count, displayed on the Web page.

Example

Write it in front of

The code is as follows Copy Code

<?php
Session_Start ();
$stsfile = "10001.php";
$nowtime = time ();
Date_default_timezone_set ("asia/chongqing")/Set time standard
if (!isset ($_session[' article ')) | | $_session[' article ']!= $stsfile)//Determine if the user is online
{
$link = mysql_connect ("localhost", "Library name", "password") or Die ("Open database Failed");
mysql_select_db ("library name", $link); Connecting to a database
mysql_query ("Set names ' UTF8 '"); Set access encoding

Query $stsfile whether the record already exists, if it does not exist insert time and count cardinality 0, if present, then +1, update 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 down $_session[]
}
}
?>

The page Display section is the

  code is as follows copy code

<?php
$link = mysql_connect ("localhost", "Library name", "password") or Die ("Open database Failed");
mysql_select_db ("library name", $link);//Connection Database
mysql_query ("Set names ' UTF8");//Set access encoding
$sql = "SELECT * FROM Statisti CS WHERE ' stsfile ' = ' $stsfile ';
$result =mysql_query ($sql);
$row = mysql_fetch_array ($result);
Echo Upload time:. Date ("Y-m-d h:i:s", $row [' Ststime ']). "Browse Number:". $row [' Stsnumb '];

 ?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.