Home > Web Front-end > JS Tutorial > How to solve the problem of referer compatibility with major browsers under js_javascript skills

How to solve the problem of referer compatibility with major browsers under js_javascript skills

WBOY
Release: 2016-05-16 16:32:17
Original
1566 people have browsed it

HTTP Header referer mainly tells people where I come from, that is, which page I come from. It can be used to count the source of users who visit this website, and can also be used to prevent hot links. The best way to obtain this thing is js. If you obtain it on the server side (PHP method such as: $_SERVER['HTTP_REFERER']), it is unreliable. People can forge it. It is best to use js. It is difficult for people to forge it.

Method: Use the document.referer method of js to accurately determine the true origin of the web page. Currently, Baidu statistics, Google ads statistics, and CNZZ statistics all use this method. Anti-hotlinking is also very simple. If the source URL is judged in js and it is not from this site, the picture will not be displayed.

As we all know, we web developers hate IE because IE does not support standards and its default behavior outside of standards is often inconsistent with other browsers:

Use javascript to make a jump in IE, for example, use window.location.href = ""; If Google uses document.referrer, it cannot get the HTTP referrer requested by the browser because IE has cleared it

The other mainstream browsers Firefox and Chrome will retain the referrer. There is no way, this means that IE will enjoy "ministerial" special treatment again:

The following code can solve this problem in IE:
//Detect if it is an IE browser, then manually add a referer
The principle is to secretly add a link to the IE browser page, and then automatically click on the link, so the referrer can be retained.

Copy code The code is as follows:

var url = 'http://www.jb51.net';
if (/MSIE (d .d );/.test(navigator.userAgent) || /MSIE(d .d );/.test(navigator.userAgent))
{
var referLink = document.createElement('a');
​ referLink.href = url;  
Document.body.appendChild(referLink);
​ referLink.click();  
}
else
{
Location.href = url;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template