<?php
require 'lesson44.php';
$user_ip = $_SERVER ['REMOTE_ADDR'];
//ipaddress of this server
function ip_exists($ip){
global $mycon;
$query = "SELECT `ip` FROM `hits_ipaddress`";
if($query_run = mysqli_query($mycon, $query)){
$num_rows = mysqli_num_rows($query_run);
//num_rows gives bow many rows or whats the row
if($num_rows == 0){
return false;
} else {
while($row = mysqli_fetch_array($query_run) ) {
$e_ip = $row['ip'];
if ($ip == $e_ip){
return true;}
}
}
}}
function add_ip ($ip){
global $mycon;
// if we want to use outside variable in function
$query = "INSERT INTO `hits_ipaddress` (`id`, `ip`) VALUES(NULL, '$ip')";
// write a statement or query to insert into table
mysqli_query($mycon, $query);
//run this query on this database
}
//to update in data base , we need query
function update_count() {
global $mycon;
// if we want to use outside variable in function
$query = "SELECT `hits` FROM `hits_count` WHERE `id` = 1 ";
// write a statement or query to select table
if ($query_run = mysqli_query($mycon, $query)) {
// execute query on this database
$result = mysqli_fetch_row($query_run);
// it takes a single row not necessary while , get the row data, and itis in a array
$current_hits =$result[0];
// access the array
$hits_inc = $current_hits +1;
// incriment the hits , for every refresh it executes this functioon, it incriments
$update_query = "UPDATE `hits_count` SET `hits` = $hits_inc WHERE `id` = 1";
// which one is updated, and updat that to database
mysqli_query($mycon, $update_query);
//run this query on this database
}
}
//change to unknown ipaddress, and check it is updating or not
if (!ip_exists($user_ip))
{
update_count();
add_ip($user_ip);
}
?>
No comments:
Post a Comment