您需要做两件事。首先,您需要将网站图标请求重定向到脚本。您可以通过两种方式执行此操作。第一种方法是将类似以下内容的内容添加到您的文件中.htaccess
RewriteEngine on
RewriteRule ^/favicon.ico /favicon.php [L]
或者您可以在html代码中发送另一个图标位置。但是,我不会使用它直接重定向到php脚本,因为如果它不是真正的或文件,某些浏览器在正确使用favicon时会遇到问题。也许您可以使用它重定向到其他位置,并将其与.我为所有设置使用了一个图标位置,这并不是真正需要的。但是通过这种方式,您知道如何改变它。.ico
.png
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">
由于您要重定向到 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;
}
?>
现在,在对网页的第一个请求中,这将很难跟踪。因为对主页的请求将首先发出,然后它将尝试加载 .因此,新/回头客的信息在php中不直接可用。在主页顶部检查它是否是回访者的最佳方法是favicon.ico
<?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
}
?>
如果您确实需要在主页上(或用户请求的任何其他页面作为此会话的第一页)上了解它,则最好的选择是在加载文档时进行ajax调用,甚至可能具有较短的超时时间,因为favicon.ico请求并不总是正文的一部分。