Skip to content

Instantly share code, notes, and snippets.

Last active July 1, 2023 23:59
Show Gist options
  • Save papoms/3453137 to your computer and use it in GitHub Desktop.
Save papoms/3453137 to your computer and use it in GitHub Desktop.
Fake Referrer with Phantomjs
var system = require('system');
// Exit in case of wrong parameter count.
if (system.args.length !== 3) {
console.log('Usage: scriptname targetUrl referrer');
console.log('example: $> phantomjs fake-referrer.phantom.js');
// Set the important pieces
var targetUrl = system.args[1];
var referrer = system.args[2];
console.log('Going to open '+targetUrl+' with the referrer '+referrer);
var page = require('webpage').create();
// set our custom referer [sic]
page.customHeaders = {
"Referer" : referrer
page.onLoadFinished = function(status){
page.customHeaders = {};
// get the currentUrl
var currentUrl = page.evaluate(function() {
return document.location.href;
// get the referrer
var currentReferrer = page.evaluate(function() {
return document.referrer;
console.log('Loading ' + currentUrl + ' finished with status: ' + status+'. document.referrer is: '+currentReferrer);
// Only once do
if ( page.firstLoad ) {
page.firstLoad = false;
console.log('Injecting the Link.');
// Inject and Click a Link to our target
page.evaluate(function (href) {
// Create and append the link
var link = document.createElement('a');
link.setAttribute('href', href);
// Dispatch Click Event on the link
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, link);
}, targetUrl);
} else {
page.firstLoad = true;;
Copy link

Well, at least I know it's not just me…

$ phantomjs -v
$ phantomjs fake-referrer.phantom.js
Going to open with the referrer
Loading about:blank finished with status: fail. document.referrer is:
Injecting the Link.
Loading finished with status: success. document.referrer is:

Copy link

eldair commented Jun 29, 2016

Unfortunately not working :|

Copy link

yes it is not working

Copy link

Use Settings:

var settings = {
  headers: {
   "Referer": "referer_url"

And pass it as the second argument on function, like that:, settings, function(status) {

Copy link

dpkcse commented Jan 22, 2017

how it will use?

Copy link

ghost commented Feb 27, 2017


page.onResourceRequested = function(r,n){

	page.customHeaders = {};


This is where the customHeaders should be cleared, and NOT in onLoadFinished because all elements called by that first page would still have the referer instead of the page itself.
And you can always put a condition to call it only once for neatness but I doubt there would be any performance improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment