<!-- 反劫持 S -->
<script>
(function () {
//设置cookie
function setCookie(name, value, domain, path, expires) {
if(expires){
expires = new Date(+new Date() + expires);
}
var tempcookie = name + '=' + escape(value) +
((expires) ? '; expires=' + expires.toGMTString() : '') +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '');
//Ensure the cookie's size is under the limitation
if(tempcookie.length < 4096) {
document.cookie = tempcookie;
}
}
//获取cookie
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return (arr[2]);
else
return null;
}
//创建并发送请求
function createSender(url){
var img = new Image();
img.onload = img.onerror = function(){
img = null;
};
img.src = url;
}
/*
*防劫持逻辑所需参数在此处设置参数即可
*cookieName:用于记录连续被劫持的次数,为防止死循环,cookie值为3以上则不进行url重置
*cookieDomain:cookie所在的域
*reportUrl:非连续性劫持时上报的接口地址【如无需上报,可不填】
*reportUrl2:连续性劫持时上报的接口地址【如无需上报,可不填】
*/
var cookieName = 'hiijack';
var cookieDomain = '.readnovel.com';
var reportUrl = '<%= staticConf.domains.hiijack %>/ajax/safe/hiijackReport?times=1&_csrfToken='+ getCookie('_csrfToken')||'';
var reportUrl2 = '<%= staticConf.domains.hiijack %>/ajax/safe/hiijackReport?times=3&_csrfToken='+ getCookie('_csrfToken')||'';
//判断是否被iframe
if (top.location !== self.location) {
//用于记录被劫持的次数
var countHijack;
//如果未设置cookie,则需要set一下cookie,否则获取此cookie的值
if(!getCookie(cookieName)){
setCookie(cookieName,0 ,cookieDomain, '', 30*24*60*60*1000);
countHijack = 0;
}else{
countHijack = parseInt(getCookie(cookieName));
}
//如果连续被劫持的次数大于等于3次,则发请求上报此情况,否则上报非连续性的情况,同时累加被劫持次数、重置当前url
if(countHijack >= 3){
reportUrl2 && reportUrl2!='' && createSender(reportUrl2);
}else{
reportUrl && reportUrl!=''&& createSender(reportUrl);
countHijack ++;
setCookie(cookieName, countHijack ,cookieDomain, '', 30*24*60*60*1000);
top.location = self.location;
}
}
//每次成功进入页面则计数清0
setCookie(cookieName, 0 ,cookieDomain, '', 30*24*60*60*1000);
})();
</script>
<!-- 反劫持 E -->
劫持处理
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...