我的基于php的客户端IP跟踪器并不总是触发,为什么?

UPDATE Nevermind it is working fine, I wrote it so that if the same website was viewed more than once it wasn't added again rather incremented... haha I don't know my own code

I wrote this script here, it is somewhat redundant as it writes into two different tables but the only difference is that one counts views and the other one counts new ips. I suppose I could redo them.

The problem is that the tracker isn't always triggered. Why would that be?

<?php

mysqli_report(MYSQLI_REPORT_OFF);
error_reporting(E_ALL);
error_reporting(-1);
ini_set('display_errors',true);

function track() {

$ip = "my ip";

$client_ip = $_SERVER['REMOTE_ADDR'];

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR.'dbconnect.php');
$link = new mysqli("$servername", "$username", "$password", "$dbname");

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if($client_ip!=$ip){

$stmt = $link->prepare('SELECT website_url FROM website_views where website_url=?');
$stmt->bind_param('s',$actual_link);

if($stmt->execute())
{
    // means it exists
    $stmt->bind_result($website_url_from_db);
    if($stmt->fetch())
    {
    $link = new mysqli("$servername", "$username", "$password", "$dbname");
    $id = "";
    $stmt = mysqli_prepare($link, "UPDATE website_views SET views=views+1 where website_url=?");
    $stmt->bind_param('s',$actual_link);
    $stmt->execute();

    $link = new mysqli("$servername", "$username", "$password", "$dbname");
    $stmt = $link->prepare('SELECT client_ip FROM views where client_ip=?');
    $stmt->bind_param('s',$client_ip);
    if($stmt->execute()){

    $link = new mysqli("$servername", "$username", "$password", "$dbname");
    $stmt = mysqli_prepare($link, "UPDATE views SET num_times=num_times+1 where client_ip=? and website_url=?");
    $stmt->bind_param('ss',$client_ip,$actual_link);
    $stmt->execute();

    }else{

    $num_times = 1;
    $link = new mysqli("$servername", "$username", "$password", "$dbname");
    $stmt = mysqli_prepare($link, "INSERT INTO views VALUES (?,?,?,?)");
    $stmt->bind_param('issi',$id,$actual_link,$client_ip,$num_times);
    $stmt->execute();
    }
    }else{
$link = new mysqli("$servername", "$username", "$password", "$dbname");
$id = "";
$website_url = $actual_link;
$views = 1;
$ip = $client_ip;
$num_times=1;

$stmt = mysqli_prepare($link, "INSERT INTO website_views VALUES (?,?,?)");
$stmt->bind_param('isi',$id,$website_url,$views);
$stmt->execute();
$link = new mysqli("$servername", "$username", "$password", "$dbname");
$stmt = mysqli_prepare($link, "INSERT INTO views VALUES (?,?,?,?)");
$stmt->bind_param('issi',$id,$actual_link,$client_ip,$num_times);
$stmt->execute();
}

}
}
}

track();

?>

try

$ipAddress = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
    $ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}

also, check the print_r( $_SERVER ) to make sure it's giving you what you expect.