Alv*_*aro 19 php cookies favicon jquery visited
我在这个回答中读到了"网站如何在清除浏览器cookie之后跟踪用户",可以通过网站favicon跟踪用户:
Favicons是第三种可能性 - 大多数浏览器在页面加载之前请求,因此如果满足该请求,则客户端显然是重复访问者.
如果这实际上是可能的,那么它可以是检查用户之前是否访问过该网站的好方法,不需要为该porpouse使用cookie.
我不确定这是否可以用PHP或Javascript(jQuery)完成.怎么能实现这一目标?
编辑:
我解释的是,如果用户需要Favicon,他就会打电话.如果他没有打那个电话,那就意味着他已经有了这个电子邮件,所以他去了.因此,无需在用户计算机中存储任何文件(例如cookie)或将其IP保留在服务器中.它是否正确?
Hug*_*ing 15
你需要做两件事.首先,您需要将favicon请求重定向到脚本.你可以用两种方式做到这一点.第一是增加类似下面的到你的.htaccess文件
RewriteEngine on
RewriteRule ^/favicon.ico /favicon.php [L]
Run Code Online (Sandbox Code Playgroud)
或者你可以在HTML代码中发送另一个favicon位置.但是,我不会使用它来直接重定向到php脚本,因为如果它不是真正的文件.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">
Run Code Online (Sandbox Code Playgroud)
由于您要重定向到PHP脚本,因此可以使用以下代码来处理实际请求.
<?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;
}
?>
Run Code Online (Sandbox Code Playgroud)
现在,在第一次请求您的网页时,这将很难跟踪.因为首先会向您的主页发出请求,然后会尝试加载favicon.ico.所以新的/回访者的信息不能直接在php中获得.检查主页顶部的返回访问者是否类似的最佳方法
<?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
}
?>
Run Code Online (Sandbox Code Playgroud)
如果你真的需要在主页上知道它(或者用户请求作为此会话的第一页的任何其他页面),你最好的选择是在文档加载时进行ajax调用,甚至可能是因为短暂的超时favicon.ico请求并不总是身体的一部分.
要获取有关Apache中的favicon请求的信息,请编辑.htaccess文件以将favicon请求重新路由到您选择的脚本.然后,您需要记录请求IP地址或使用cookie来确定站点访问者是否刚刚请求了favicon.
编辑
记住在处理请求后返回favicon.
| 归档时间: |
|
| 查看次数: |
3645 次 |
| 最近记录: |