- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在为用户帖子创建一个访客计数系统,以显示首页上访问量最高的帖子。我现在有一个访客计数系统,但是所有页面刷新时都会注册一个 View 。我无法使用Google Analytics(分析)。
我需要的是一个访客计数器,只计算不重复访客的数量。就我而言,独特意味着一个人一天只能查看一个帖子?我认为甚至一个星期都可以。你可以在这里写那个php代码吗?如果您也喜欢的话,也可以给我链接一些优秀的教程。
这是代码需要执行的操作(或等效操作):
最佳答案
这是一个不错的教程,这是您所需要的。
(来源:coursesweb.net/php-mysql)
注册并显示在线用户和访客
使用MySQL表统计在线用户和访客
在本教程中,您可以学习如何注册,计数和在您的网页中显示在线用户和访问者的数量。
原理是这样的:每个用户/访问者都在文本文件或数据库中注册。每次访问网站页面时,php脚本都会删除所有超过特定时间(例如2分钟)的记录,添加当前用户/访问者,并显示剩余的记录数。
您可以将在线用户和访问者存储在服务器上的文件或MySQL表中。
在这种情况下,我认为使用文本文件来添加和读取记录要比将它们存储到需要更多请求的MySQL表中更快。
首先,它介绍了在服务器上以文本文件记录的方法,而不是使用MySQL表的方法。
要使用本教程中介绍的脚本下载文件,请单击-> Count Online Users and Visitors。
•这两个脚本都可以包含在“.php”文件(带有include()
)中,也可以包含在“.html”文件(带有<script>
)中,如您在本页底部提供的示例中所见;但是服务器必须运行PHP。
将在线用户和访客存储在文本文件中
要使用PHP在服务器上的文件中添加记录,必须为该文件设置CHMOD 0766(或CHMOD 0777)权限,以便PHP可以在其中写入数据。
userson.txt
)并为其授予CHMOD 0777
权限(在FTP应用程序中,右键单击该文件,选择“属性”,然后选择Read
,Write
和Execute
选项)。 usersontxt.php
),然后将此php文件复制到与userson.txt
相同的目录中。 usersontxt.php
;
<?php
// Script Online Users and Visitors - http://coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start(); // start Session, if not already started
$filetxt = 'userson.txt'; // the file in which the online users /visitors are stored
$timeon = 120; // number of secconds to keep a user online
$sep = '^^'; // characters used to separate the user name and date-time
$vst_id = '-vst-'; // an identifier to know that it is a visitor, not logged user
/*
If you have an user registration script,
replace $_SESSION['nume'] with the variable in which the user name is stored.
You can get a free registration script from: http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/
// get the user name if it is logged, or the visitors IP (and add the identifier)
$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;
$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i'; // regexp to recognize the line with visitors
$nrvst = 0; // to store the number of visitors
// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp)
$addrow[] = $uvon. $sep. time();
// check if the file from $filetxt exists and is writable
if(is_writable($filetxt)) {
// get into an array the lines added in $filetxt
$ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$nrrows = count($ar_rows);
// number of rows
// if there is at least one line, parse the $ar_rows array
if($nrrows>0) {
for($i=0; $i<$nrrows; $i++) {
// get each line and separate the user /visitor and the timestamp
$ar_line = explode($sep, $ar_rows[$i]);
// add in $addrow array the records in last $timeon seconds
if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) {
$addrow[] = $ar_rows[$i];
}
}
}
}
$nruvon = count($addrow); // total online
$usron = ''; // to store the name of logged users
// traverse $addrow to get the number of visitors and users
for($i=0; $i<$nruvon; $i++) {
if(preg_match($rgxvst, $addrow[$i])) $nrvst++; // increment the visitors
else {
// gets and stores the user's name
$ar_usron = explode($sep, $addrow[$i]);
$usron .= '<br/> - <i>'. $ar_usron[0]. '</i>';
}
}
$nrusr = $nruvon - $nrvst; // gets the users (total - visitors)
// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';
// write data in $filetxt
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists, or is not writable';
// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";
echo $reout; // output /display the result
?>
<script type="text/javascript" src="usersontxt.php?uvon=showon"></script>
该脚本(以及下面提供的其他脚本)与$ _SESSION一起使用。在使用它的PHP文件的开头,必须添加:session_start();。
create_userson.php
的代码:
<?php
header('Content-type: text/html; charset=utf-8');
// HERE add your data for connecting to MySQ database
$host = 'localhost'; // MySQL server address
$user = 'root'; // User name
$pass = 'password'; // User`s password
$dbname = 'database'; // Database name
// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);
// check connection
if (mysqli_connect_errno()) exit('Connect failed: '. mysqli_connect_error());
// sql query for CREATE "userson" TABLE
$sql = "CREATE TABLE `userson` (
`uvon` VARCHAR(32) PRIMARY KEY,
`dt` INT(10) UNSIGNED NOT NULL
) CHARACTER SET utf8 COLLATE utf8_general_ci";
// Performs the $sql query on the server to create the table
if ($conn->query($sql) === TRUE) echo 'Table "userson" successfully created';
else echo 'Error: '. $conn->error;
$conn->close();
?>
userson
表中创建用于插入,删除和选择数据的脚本(有关代码的说明,请参见脚本中的注释)。 usersmysql.php
)中添加以下代码:$host
,$user
,$pass
和$dbname
中添加用于连接MySQL数据库的个人数据。 <?php
// Script Online Users and Visitors - coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start(); // start Session, if not already started
// HERE add your data for connecting to MySQ database
$host = 'localhost'; // MySQL server address
$user = 'root'; // User name
$pass = 'password'; // User`s password
$dbname = 'database'; // Database name
/*
If you have an user registration script,
replace $_SESSION['nume'] with the variable in which the user name is stored.
You can get a free registration script from: http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/
// get the user name if it is logged, or the visitors IP (and add the identifier)
$vst_id = '-vst-'; // an identifier to know that it is a visitor, not logged user
$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;
$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i'; // regexp to recognize the rows with visitors
$dt = time(); // current timestamp
$timeon = 120; // number of secconds to keep a user online
$nrvst = 0; // to store the number of visitors
$nrusr = 0; // to store the number of usersrs
$usron = ''; // to store the name of logged users
// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);
// Define and execute the Delete, Insert/Update, and Select queries
$sqldel = "DELETE FROM `userson` WHERE `dt`<". ($dt - $timeon);
$sqliu = "INSERT INTO `userson` (`uvon`, `dt`) VALUES ('$uvon', $dt) ON DUPLICATE KEY UPDATE `dt`=$dt";
$sqlsel = "SELECT * FROM `userson`";
// Execute each query
if(!$conn->query($sqldel)) echo 'Error: '. $conn->error;
if(!$conn->query($sqliu)) echo 'Error: '. $conn->error;
$result = $conn->query($sqlsel);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// traverse the sets of results and set the number of online visitors and users ($nrvst, $nrusr)
while($row = $result->fetch_assoc()) {
if(preg_match($rgxvst, $row['uvon'])) $nrvst++; // increment the visitors
else {
$nrusr++; // increment the users
$usron .= '<br/> - <i>'.$row['uvon']. '</i>'; // stores the user's name
}
}
}
$conn->close(); // close the MySQL connection
// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. ($nrusr+$nrvst). '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';
// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";
echo $reout; // output /display the result
?>
usersmysql.php
文件。<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Counter Online Users and Visitors</title>
<meta name="description" content="PHP script to count and show the number of online users and visitors" />
<meta name="keywords" content="online users, online visitors" />
</head>
<body>
<!-- Includes the script ("usersontxt.php", or "usersmysql.php") -->
<script type="text/javascript" src="usersmysql.php?uvon=showon"></script>
</body>
</html>
这两个脚本(将数据存储在服务器上的文本文件中或MySQL表中)将显示如下结果:
关于php - 如何计算网站的唯一身份访问者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799808/
我正在尝试为玩具语言实现一个解析器。 我已经编写了语法,但是当我尝试从 CST 创建 AST 时遇到了问题。 我定义了一个继承自 MyParserVisitor 的类哪里ASTNode是一个虚拟类,我
我想创建子类对象,它会与其他子类对象做出不同的 react (类可以欢迎另一个类,但不是全部)代码原理源自访客设计模式: class A { public : virtual bool isW
我正在玩 boost A* 算法,从在以下位置找到的示例开始:http://www.boost.org/doc/libs/1_37_0/libs/graph/example/astar-cities.
我正在为我的网站构建一个访问者和点击率计数器,当然,访问者每天是一个唯一的 ip,点击率是每次请求页面时! 我已经创建了数据库和系统来插入和更新我保存统计信息的表! 我正在苦苦挣扎的是,如何对日期进行
如何放弃使用 IE6 浏览我的网站? 就像是 : 如果 ie6 => 死 我正在使用 ASP.Net 谢谢 最佳答案 在asp.net中你可以查看Request.Browser在 Session_st
我目前正在编写解析器。解析器生成一个 AST,然后我使用各种遍历来处理它。 AST 是(简化的): type LiteralExpr = { readonly kind: 'literal',
我正在使用 Antlr4 的 C++ 访问者 api 来遍历解析树。但是,我正在努力使其正常运行。也就是说,我不确定如何使用 visitChildren(ParseTree *tree) 调用。 我为
我的问题是理论性的,而不是如何去做。我想知道专家们如何处理我将在下面描述的情况。 我有一个使用 Angular 和 Breeze 的 SPA。身份验证是基于 token 的。我在 Angular 中设
我有一棵树(在图形意义上)表示一棵树(在物理意义上)。树表示为 BGL 邻接列表,其中每个顶点包含半径和位置属性,即,我的图以以下形式定义 struct TreeVertexType { doub
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
是否可以将域限制为仅允许使用特定浏览器的访问者? 我正在构建一个应用程序,到目前为止它只在 Chrome 中进行过测试,所以我只想在 Beta 测试期间允许 Chrome 用户。基本上,我想在进行测试
你好,我正在尝试实现一个 AST Clang 访问者,这是我的代码。 class ExampleVisitor : public RecursiveASTVisitor { private:
我想构建一个访问者(用于 dikstra),并将 initialise_vertex 用作“颜色映射”修饰符。我想根据条件从搜索中排除一些顶点。所以我想在算法的初始部分将一些顶点设置为“黑色”。 cl
我正在尝试编写一个网站,提示每 10,000 名访问者输入一封可以存储在文本文件中的电子邮件。 我设置了一个点击计数器,它将访问者总数输出到一个文本文件中,因此是否可以将脚本设置为类似于 "如果 nu
我正在尝试将 AST 与 ANTLR4 一起使用,并使用以下文件: 生成器.java import org.antlr.v4.runtime.ANTLRInputStream; import org.
在 BGL 中,我不太明白如何在 bfs/dfs 搜索期间访问图中顶点的固有颜色(白色表示未触及,灰色表示已访问,黑色表示已完成)。 有人可以说明如何从 dfs/bfs 访问者中访问顶点的颜色吗?例如
当用户访问您的网站时,如何获取用户的信息? IP地址 苹果地址 用户个人资料名称 操作系统名称 操作系统版本 操作系统注册到(姓名/公司) 计算机名 浏览器名称 浏览器版本 ISP 名称/Intern
我最近开始使用 Amazon S3 为访问者提供图像,因为这会减少服务器负载。现在,出现了一个新问题:今天我查看了我的 AWS 账单。我注意到我有一大笔账单等着我——20 天内总共有 4TB 的 AW
session 劫持 所以我有一个小问题。我正在尝试识别访问者,这很难通过 $_SERVER veriables 来实现,如以下问题所述:Preventing session hijacking .
我有一个带有两个边定义的图,如下所示: isDepartment: [organisation] -> [organisation] hasAccess: [user] -> [organisatio
我是一名优秀的程序员,十分优秀!