This commit is contained in:
teasiu
2023-12-24 22:17:16 +08:00
parent 005e9e30d4
commit 1f79c5b439
4 changed files with 87 additions and 55 deletions

View File

@@ -7,8 +7,8 @@
/* 让页面内容充满整个视口 */ /* 让页面内容充满整个视口 */
html, body { html, body {
height: 100%; height: 100%;
margin: 0; margin: 10px;
padding: 0; padding: 10px;
} }
/* 居中显示内容 */ /* 居中显示内容 */
@@ -24,64 +24,65 @@
max-width: 100%; max-width: 100%;
height: auto; height: auto;
} }
/* 页脚样式 */
footer {
position: fixed;
bottom: 0;
width: 100%;
background-color: #f0f0f0;
text-align: center;
padding: 5px 0;
}
</style> </style>
</head> </head>
<body> <body>
<br/>
<h1>是兄弟就找我,无兄弟不传奇!</h1> <h1>是兄弟就找我,无兄弟不传奇!</h1>
<a href="https://www.521f.com"> <a href="?hinas" onclick="trackVisit()">
<img src="ad.jpg" alt="广告图片"> <img id="adImage" src="ad.jpg" alt="广告图片">
</a> </a>
<script language="JavaScript">
<!-- <footer>
var caution = false <p>点击链接访问次数: <span id="visitCount">加载中...</span></p>
function setCookie(name, value, expires, path, domain, secure) { </footer>
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") + <script>
((path) ? "; path=" + path : "") + // 获取并显示访问次数
((domain) ? "; domain=" + domain : "") + function getVisitCount() {
((secure) ? "; secure" : "") var xhr = new XMLHttpRequest();
if (!caution || (name + "=" + escape(value)).length <= 4000) xhr.onreadystatechange = function() {
document.cookie = curCookie if (xhr.readyState === XMLHttpRequest.DONE) {
else if (xhr.status === 200) {
if (confirm("Cookie exceeds 4KB and will be cut!")) document.getElementById('visitCount').innerText = xhr.responseText;
document.cookie = curCookie } else {
} console.error('获取访问次数失败');
function getCookie(name) { }
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
} }
} };
function fixDate(date) { xhr.open('GET', 'ad_get_visit_count.php', true);
var base = new Date(0) xhr.send();
var skew = base.getTime() }
if (skew > 0)
date.setTime(date.getTime() - skew) // 点击图片时触发的函数,用于向后端发送访问计数请求
} function trackVisit() {
var now = new Date() var xhr = new XMLHttpRequest();
fixDate(now) xhr.open('GET', 'ad_visit.php', true);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000) xhr.onreadystatechange = function() {
var visits = getCookie("counter") if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
if (!visits) getVisitCount(); // 更新访问次数显示
visits = 1 }
else };
visits = parseInt(visits) + 1 xhr.send();
setCookie("counter", visits, now)
document.write("您是到访的第" + visits + "位用户!") // 更新图片的点击链接,可以将其指向实际的广告目标链接
// --> document.getElementById('adImage').parentNode.href = "https://www.521f.com";
</script> }
// 页面加载完成后获取并显示访问次数
window.onload = function() {
getVisitCount();
};
</script>
</body> </body>
</html> </html>

1
static/ad_count.txt Normal file
View File

@@ -0,0 +1 @@
232

View File

@@ -0,0 +1,12 @@
<?php
// 用于获取当前访问次数并返回给前端
$file = 'ad_count.txt'; // 存储访问次数的文件
if (file_exists($file)) {
$visitCount = (int)file_get_contents($file); // 从文件中获取访问次数
echo $visitCount;
} else {
echo '0';
}
?>

18
static/ad_visit.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
// 在这里记录访问次数或者其他必要的处理
// 模拟访问次数的记录,可以存储到数据库中或文件中
$file = 'ad_count.txt'; // 存储访问次数的文件
$visitCount = 0; // 初始访问次数
if (file_exists($file)) {
$visitCount = (int)file_get_contents($file); // 从文件中获取访问次数
}
$visitCount++; // 每次访问增加1次访问次数
file_put_contents($file, $visitCount); // 将访问次数写入文件
// 返回访问次数以便前端 JavaScript 进行显示
echo $visitCount;
?>