PHPでサイト訪問数を表示する
ConoHA環境にて
サイト下にディレクトリーを作成 サイト/xxxxx
index.phpを作成
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>最近の訪問数</title>
</head>
<body bgcolor="#daefef">
<h1> 最近の訪問数 </h1>
<?php
header('X-FRAME-OPTIONS: SAMEORIGIN');
try {
$pdo = new PDO('mysql:host=aaa.bbb.ccc.ddd;dbname=database_name;charset=utf8','user_id','password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $Exception) {
die('error con'.$Exception -> getMessage());
}
try{
$sql = " SELECT wp_statistics_visitor.last_counter ,wp_statistics_visit.visit,count(*) as ninzu FROM wp_statistics_visitor ";
$sql .=" left outer join wp_statistics_visit on wp_statistics_visitor.last_counter = wp_statistics_visit.last_counter ";
$sql .=" group by last_counter ";
$sql .=" order by last_counter DESC limit 31";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row_count = $stmt->rowCount();
foreach ($stmt as $row) {
$rows[] = $row;
}
$pdo = null;
}catch(PDOException $Exception){
die('error sql:' .$Exception->getMessage());
}
?>
日数:[ <?php echo $row_count; ?> ]
<table border='1' width="310">
<tr><td align="center" bgcolor="pink">訪 問 日</td><td align="center" bgcolor="pink">訪 問 数</td><td align="center" bgcolor="pink">訪 問 者</td></tr>
<?php
foreach($rows as $row){
?>
<tr>
<td align="center" bgcolor="white"><?php echo htmlspecialchars($row['last_counter']); ?></td>
<td align="right" bgcolor="white"><?php echo htmlspecialchars($row['visit']); ?></td>
<td align="right" bgcolor="white"><?php echo htmlspecialchars($row['ninzu']); ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>