gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:10 25 4
gpt4 key购买 nike

更新 没关系,它工作正常,我写它是为了如果同一个网站被多次查看,它不会再次添加,而是递增...哈哈,我不知道我自己的代码

我在这里写了这个脚本,因为它写入了两个不同的表,所以它有点多余,但唯一的区别是一个计算浏览量,另一个计算新的 ips。我想我可以重做它们。

问题是跟踪器并不总是被触发。为什么会这样?

<?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();

?>

最佳答案

尝试

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

另外,检查 print_r( $_SERVER ) 以确保它提供您所期望的。

关于php - 我基于 php 的客户端 IP 跟踪器并不总是触发,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com