Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用favicon跟踪用户访问网站的情况_Php_Jquery_Cookies_Favicon_Visited - Fatal编程技术网

Php 使用favicon跟踪用户访问网站的情况

Php 使用favicon跟踪用户访问网站的情况,php,jquery,cookies,favicon,visited,Php,Jquery,Cookies,Favicon,Visited,我了解到可以通过favicon网站跟踪用户: Favicon是第三种可能性——大多数浏览器以前都会这样要求 页面已加载,因此如果满足该请求,则客户端 他显然是个老顾客 如果这真的是可能的,它可以是一个很好的方式来检查用户是否访问过该网站之前,不需要使用cookies的porpouse 我不确定这是否是我可以用PHP或Javascript(jQuery)实现的。 如何做到这一点 编辑: 我的解释是,如果用户需要Favicon,他会打电话。如果他不打那个电话,那就意味着他已经有了favicon,所以

我了解到可以通过favicon网站跟踪用户:

Favicon是第三种可能性——大多数浏览器以前都会这样要求 页面已加载,因此如果满足该请求,则客户端 他显然是个老顾客

如果这真的是可能的,它可以是一个很好的方式来检查用户是否访问过该网站之前,不需要使用cookies的porpouse

我不确定这是否是我可以用PHP或Javascript(jQuery)实现的。 如何做到这一点

编辑


我的解释是,如果用户需要Favicon,他会打电话。如果他不打那个电话,那就意味着他已经有了favicon,所以他去了。因此,不需要在用户计算机中存储任何文件(例如cookie),也不需要在服务器中保留其IP。这是正确的吗?

使用“cookie”来跟踪不同的浏览器,或者使用PHP来跟踪IP地址。

要在Apache中获取有关favicon请求的信息,请编辑.htaccess文件,将favicon请求重新路由到您选择的脚本。然后,您需要记录请求IP地址或使用cookies来确定站点访问者是否刚刚请求了favicon

编辑

请记住在处理请求后返回favicon。

当浏览器从服务器请求任何内容时,它会发送所有cookies等。favicon也是如此

要让PHP“拦截”这些请求,您需要强制Apache(或您正在使用的任何HTTP服务器)将favicon视为PHP文件

在.htaccess类型中(再次假定为apache)


ForceType应用程序/x-httpd-php
SetHandler应用程序/x-httpd-php
现在,对于favicon,服务器需要一个php文件,客户端需要一个图标。因此,在php之外不能有任何空白,否则图标将无法工作。首先将您的real favicon移动到real-favicon.ico,然后使用以下命令创建一个新的favicon.ico

<?php
//do any processing you wish here, all cookies etc are available if needed 

header('Content-type: image/x-icon');
echo file_get_contents ("real-favicon.ico");
因此,关闭它可能更安全,因为一些编辑器会在底部添加空白,这会破坏图像

如果没有此方法,您将不得不解析apache日志以查找这些日志和匹配的IP


您仍然需要cookie信息,没有它您无法传递/跟踪信息

你需要做两件事。首先,需要将favicon请求重定向到脚本。你可以用两种方法来做。第一种方法是在
.htaccess
文件中添加如下内容

RewriteEngine on
RewriteRule ^/favicon.ico   /favicon.php  [L]
或者您可以在html代码中发送另一个favicon位置。但是,我不会使用它直接重定向到php脚本,因为如果favicon不是
.ico
.png
文件,则某些浏览器在正确使用favicon时会遇到问题。也许您可以使用它重定向到另一个
favicon.ico
位置,并将其与
.htaccess
结合使用。我对所有设置使用了一个图标位置,这并不是真正需要的。但这样你就知道如何改变它了

<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon" sizes="32x32">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" sizes="32x32">

如果您确实需要在主页(或用户请求的任何其他页面作为本次会话的第一个页面)上了解它,那么最好的选择是在加载文档时调用ajax,可能是因为favicon.ico请求并不总是正文的一部分,所以即使超时很短。

您也可以使用基于Web服务器的重定向/重写将favicon请求传递给您选择的服务器端语言。实现取决于您的Web服务器风格和语言。但是如果你想让一个脚本直接处理这个问题,你最好从页面开始跟踪

或者,如果不需要实时信息,您可以尝试读取日志文件。如果这是你的方式,你可能会得到更可靠的结果跟踪机器人.txt代替。此外,根据您的Web服务器,日志可能不包含您需要的所有信息(时间戳、IP等)


然而,我强烈建议不要为此使用Favicon。对于何时请求favicon,没有一套明确的规则,浏览器的处理方式也不同。他们可以在不访问网站的情况下请求favicon(只是为了更新书签),也可以定期请求。请求Favicon也不仅仅限于浏览器;许多Web应用程序使用Favicon在链接旁边显示。所有这些意外的点击可能会使统计数据变得非常不可靠。

您提到的答案并没有解释这项技术

favicon识别用户的技巧是为每个用户提供唯一的favicon url

此外,favicon还提供了需要浏览器重新验证的标题

当用户浏览器重新验证favicon时,将传递其唯一名称,例如,可以禁止ip地址

我不确定这是否是我可以用PHP或Javascript(jQuery)实现的。如何做到这一点

您可能想了解客户端和服务器端编程的区别

在您的案例中,您是从服务器的角度来看问题的,因此您希望利用服务器端功能(可能还有编程)。因此,这种非cookie favicon HTTP请求跟踪可以通过服务器端的服务器实现。您的服务器知道何时请求favicon,只需记录请求并解释数据

我的解释是,如果用户需要Favicon,他会打电话。如果他不打那个电话,那就意味着他已经有了favicon,所以他去了

没错,但是您还应该用一些场景来测试这一点。例如,浏览器可能考虑也可能不考虑。因此,了解该规范,了解其用途,根据这些规范对数据进行解释,并使用diff对其进行现场测试
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon" sizes="32x32">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" sizes="32x32">
<?php
//the location of the actual favicon
$favicon = '/favicon.ico';
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';

//try to get the file info, to be able to get the correct content type
//if it doesnt work, return 404 error
$size = @getimagesize($favicon);
if (!$size) {
  header($protocol . ' 404 Not Found');
  exit();
}

// Content type
header('Content-type: ' . $size[2]);

//when is the icon last modified
//Keep in mind that if you modify the icon, all returning visitors will be handled as new visitors
$last_modified_time = @filemtime($favicon);

header("Accept-Ranges:  bytes");
//set a long max-age with a recheck marker, so people check if the icon is still the same and thus access this script.
header("Cache-Control: max-age=15724800, public, must-revalidate");
header("Vary: Accept-Encoding");
//some say the Etag is bad, some say it isnt. You can remove this part if you dont want to use it.
header("Etag: " . md5($favicon . $last_modified_time));


// exit if not modified
if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
  if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) { 
      header($protocol .' 304 Not Modified'); 

      /*
      At this point you have a returning visitor.           
      */
      DoSomethingWithReturningVisitor();

      exit();
  }
}

// exit if not modified using Etag, remove it if you dont want to use it.
if (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) {
  if ($_SERVER['HTTP_IF_NONE_MATCH'] == md5($favicon . $last_modified_time)) { 
      header($protocol.' 304 Not Modified'); 


      /*
      At this point you have a returning visitor.           
      */
      DoSomethingWithReturningVisitor();


      exit();
  }
}

//you are sending a new image to the user. Add the last modified time.
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");


//log that he is a new visitor
//If you dont to this, the user will be marked as returning visitor when he visits the 2nd page of your website
$_SESSION['newVisitor'] = true;

//return the content of the actual image
echo file_get_contents($favicon);


//A single point to handle returning visitors
//make sure you dont have any output in this function, because you are still returning a valid favicon. If you have any output the returned icon will be corrupted.

function DoSomethingWithReturningVisitor() {
  if (!empty($_SESSION['newVisitor']) && $_SESSION['newVisitor'] === true) {
    //already marked as new visitor, so skip for this session
    return;
  }

  //do something to give this user special treatment
  $_SESSION['returningVisitor'] = true;
}
?>
<?php
if (empty($_SESSION['returningVisitor']) && empty($_SESSION['newVisitor'])) {
   //unknown if user is new or not
} else if (!empty($_SESSION['returningVisitor']) && $_SESSION['returningVisitor']===true) {
   //returning visitor
} else {
   //new visitor
}
?>