Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

mysqli - MySQL PHP visitors tacking script only works from localhost

I have an interesting issue. I have developed a request tracking PHP script to mimic something of a web server log

I have my visitors.php

<?php
// This is my DB Connection
include_once 'system/config.php';

// Capture all the important request details and save them to variables
$visitor_ip = $_SERVER['REMOTE_ADDR'];
$request_time = date('d-m-Y H:i:s', $_SERVER['REQUEST_TIME']);
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$http_version = $_SERVER['SERVER_PROTOCOL']; // or ?
$visitor_url = $_SERVER['REQUEST_URI'];
$request_method = $_SERVER['REQUEST_METHOD'];
$response_code = http_response_code();
// If the Host is the production site (becuase i also have a dev domain) and the originating IP is not my local machine
if ($_SERVER['HTTP_HOST'] == 'example.com' && $_SERVER['REMOTE_ADDR'] !== '127.0.0.1') {
    $stmt = $link->prepare('INSERT INTO requests (time, method, ip, user_agent, url, response_code, http_version) VALUES (?, ?, ?, ?, ?, ?, ?)');
    $stmt->bind_param('sssssis', $request_time, $request_method, $visitor_ip, $user_agent, $visitor_url, $response_code, $http_version);
    $stmt->execute();
    $result = $stmt->get_result();
    mysqli_stmt_close($stmt);
    //mysqli_close($link);
}
?>

and I have included at the top of every page with

include 'visitors.php';

What's weird is that it works perfectly when I test it myself from my own machine, a.k.a $_SERVER['REMOTE_ADDR'] == '127.0.0.1' but it doesn't record anything in the db when browsing the website from outside. No errors coming up on display_errors = on

Edit: I just got a single hit from a random visitor but I can't still make any of my requests make an entry via my smart phone on 4G

question from:https://stackoverflow.com/questions/65649072/mysql-php-visitors-tacking-script-only-works-from-localhost

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...