我目前正在为一个项目构建一个独特的点击计数器,如果用户 ip 不在数据库中,计数将增加一个,我将如何将“计数”实现到 html 中?
PHP
<?php
$connect_error = 'Sorry, we\'re experiencing connection problems.';
mysql_connect('localhost', 'root', 'password') or die($connect_error);
mysql_select_db('hit_counter_database') or die($connect_error);
$user_ip = $_SERVER['REMOTE_ADDR'];
function ip_exits($ip){
global $user_ip;
$query = "SELECT `ip` FROM `hits_ip` WHERE `ip` = '$user_ip'";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows) ==0) {
return false;
}else ($query_num_rows) >= 1){
return true;
}
}
function ip_add($ip) {
$query = "INSERT INTO `hits_ip` VALUES ('$ip')";
@$query_run = mysql_query($query);
}
function update_count(){
$query = "SELECT `count` FROM `hits_count`";
if (@$query_run = mysql_query($query)){
$count = mysql_result($query_run, 0, 'count');
$count_inc = $count + 1;
$query_update = "UPDATE `hits_count` SET `count` = '$count_inc'";
$query_update_run = mysql_query($query_update);
}
}
if(!ip_exists($user_ip){
update_count();
ip_add($user_ip);
}
?>