dongli564510 2018-08-10 13:50
浏览 44

如何插入页面来自我的网站和发布者的ID?

I need to be able to register the publisher's id in the reference data record.

site.com/url-of-post/?publishers=1435356P

The code to get the original URL from where it came to my website does not work

$refferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

My complete code is:

visitors_connections.php

<?php
$ServerName = "localhost";
$Username = "root";
$PassWord = "";
$DataBase = "visitor_publishers";

$con = new mysqli($ServerName, $Username, $PassWord, $DataBase);
if ($con->connect_error) {
  exit("Error de conexión: " . $con->connect_error);
}
if (!$con->set_charset("utf8")) {
  printf("Error cargando el conjunto de caracteres utf8: %s
", $con->error);
  exit();
}

function getBrowserType($user_agent){
  if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
  elseif (strpos($user_agent, 'Edge')) return 'Edge';
  elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
  elseif (strpos($user_agent, 'Safari')) return 'Safari';
  elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
  elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';
  return 'Other';
}

//https://stackoverflow.com/questions/6717926/function-to-get-user-ip-address
//https://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php/2031935#2031935
//https://stackoverflow.com/questions/13646690/how-to-get-real-ip-from-visitor
//http://itman.in/en/how-to-get-client-ip-address-in-php/
function GetIP(){
  foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
    if (array_key_exists($key, $_SERVER) === true){
      foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip){
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
          return $ip;
        }
      }
    }
  }
}

//https://stackoverflow.com/questions/12369615/serverhttp-referer-missing
$refferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
?>

visitor_tracking.php

<?php
require_once('visitors_connections.php');

$visitor_ip = GetIP();
$visitor_browser = getBrowserType($_SERVER['HTTP_USER_AGENT']);
$visitor_date = date("Y-m-d H:i:s");
$visitor_refferer = $refferer;
$visited_page = selfURL();
//$id_publishers = "";    

$stmt = $con->prepare("INSERT INTO visitors_table (visitor_ip, visitor_browser, visitor_date,
 visitor_refferer, visitor_page) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $visitor_ip,$visitor_browser,$visitor_date,$visitor_refferer,$visited_page);
$stmt->execute();
?>

The records of the table:

introducir la descripción de la imagen aquí

Another small validation error, there is no control to register the visits, such as avoiding the registration of the same ip next. That the same ip re-register but after 48 hrs.

  • 写回答

1条回答 默认 最新

  • douzhuijing4911 2018-08-10 15:19
    关注

    The problem with HTTP_REFERRER is that sometimes it will be sent, sometimes not. It's an entirely optional parameter which the client making the HTTP request can choose to send, or not send. Often, things like proxy servers will also modify it, so it may not be the original value. The spec doesn't even say what should be in it. So you can try to record it for sure, but you can never guarantee it will give you a full or even a correct picture of your users.

    Re your validation of IP addresses - you would have to select from the visitors table first to find the most recent entry for that IP address. If the address exists, and the last visit time was less than 48 hours ago, then don't proceed with the insert. I would be something like SELECT visitor_ip FROM visitors_table ORDER BY visitor_date DESC LIMIT 1 - I can't test it but that should get you the most recent visit by that IP address. If you get no rows, that IP address has never visited before

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题