简体   繁体   English

获取网站PHP MYSQL的访问者总数

[英]Get total amount of visitor count to website PHP MYSQL

Assignment for uni. 大学的分配。 As part of it I have to get the total number of visitors to the website and also the total number of unique visitors to the website. 作为其中的一部分,我必须获得网站访问者的总数以及网站的唯一访问者总数。 I have got the unique visitor count using IP addresses and storing them in a DB. 我使用IP地址获取了唯一的访客数,并将其存储在数据库中。 How would I get the total amount of visitors? 我如何获得访客总数? Need this: 需要这个:

Total amount of visitors = X Unique = X 访客总数= X唯一= X.

This is the code for getting unique visitors: Thanks in advance. 这是获取唯一身份访问者的代码:提前致谢。

try {
$DBH=new pdo("mysql:host=$webserver;dbname=$db", $user,$password);
} catch (PDOException $e) {
echo "Not connected".$e->getMessage();
}

//get IP
$ipAddress = $_SERVER['REMOTE_ADDR'];

//check if the ip address already exists in DB
$query1 = "SELECT IP FROM counter WHERE IP='$ipAddress'"; 
$check = $DBH->prepare($query1);
$check->execute();
$checkIP=$check->rowCount();
if ($checkIP==0) {
$query2 = "INSERT INTO counter(IP) VALUES('$ipAddress')";
$insertIP=$DBH->prepare($query2);
$insertIP->execute();
}

$number=$DBH->prepare("SELECT IP FROM counter");
$number->execute();
$visitor=$number->rowCount();


?>

Everything: SELECT COUNT(*) FROM ... 一切:SELECT COUNT(*)FROM ...

Unique: SELECT COUNT(DISTINCT field) FROM ... 唯一:SELECT COUNT(DISTINCT字段)FROM ...

You can group visitors by IP and have the number of visits for each one and also the number of uniq visitors via $visitor = $check->rowCount(); 您可以按IP对访问者进行分组,并且每个访问者的访问次数以及通过$visitor = $check->rowCount();的uniq访问者数量$visitor = $check->rowCount();

SELECT IP, count(*) as nb FROM counter WHERE IP='$ipAddress' GROUP BY IP

Then you can have the sum with 然后你可以得到总和

$results = $check->fetchAll(PDO::FETCH_COLUMN, 1);
$visitors = array_sum($results);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM