Faking the Referer Header in PhantomJS is doesn't work

这一生的挚爱 提交于 2019-12-11 06:29:18

问题


I want to make my code fake the refferer header in analytic systems(such as google analytics) , but It doesn't work. I have add 'var settings ={...//...}' and add 'page.onLoadStarted = function() {page.customHeaders = {};' and add - 'page.open(...,settings, ...' , but it still recognised like direct traffic in the analytics. Here is the code:

var page = require('webpage').create();
var settings = {
  headers: {
   "Referer": "http://google.com"
  }
};
var urls = ['http://china.com/','http://usa.com/','http://emirates.com/'];
var i = 0;

function OpenPage(){
    setTimeout(function(){
        page.onLoadStarted = function() {
    page.customHeaders = {};
};
        page.open(urls[i],settings, function(status) {
            if (status == 'success') {
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },5000);
}    
OpenPage();

I get this code from this question https://stackoverflow.com/a/42468998/4999509 and it worked like a charm, respect for coder - Flash Thunder !


回答1:


Try this:

var page = require('webpage').create();

page.onLoadStarted = function() {
    page.customHeaders = {};
};

var urls = ['http://china.com/','http://usa.com/','http://emirates.com/'];
var i = 0;

function OpenPage(){
    setTimeout(function(){
        page.customHeaders = {
            "Referer": "https://google.com"
        };
        page.open(urls[i], function(status) {
            if (status == 'success') {
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },5000);
}    
OpenPage();



回答2:


This works for Google Analytics,

var settings = {
    headers: {
        "Referer": "http://www.google.com/"
    }
};

page.open(urlToVisit, settings, function (status) {
    ...
}

Basically, you need to make your custom referer available using a second argument on page.open() function in the page context for the Google Analytics code to be able to read it



来源:https://stackoverflow.com/questions/42472913/faking-the-referer-header-in-phantomjs-is-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!