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. How would I get the total amount of visitors? Need this:
Total amount of visitors = X Unique = 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 ...
Unique: SELECT COUNT(DISTINCT field) 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();
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.