0

Scenario: I want to detect from which website my visitors are coming from an javacript SRC.

Visitor from google.com ----> My Website(Detect if visitor come from google)

<script type="text/javascript" src="ref.php"></script>

refer.php

<?php
if (strstr($_SERVER['HTTP_REFERER'], 'google.com') !== false) {
    header("Location: http://mywebsite.com/organic-traffic.js");
} 
else {
    header("Location: http://mywebsite.com/frequent-visitors.js");
}
?>

The above PHP code doesn't work. But if I use normal javascript "document.referrer" its detected.

If I access the file ref.php directly then the refer is detected also. Looks like its taking My website as referral and not google.com

Red Acid
  • 217
  • 1
  • 3
  • 14
  • You need 1. a status code indicating a redirect (307 or 302 in your case) and 2. [make sure it isn't getting cached](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers) – user2428118 Jun 20 '14 at 09:40
  • hosted the ref.php file on another domain that I have and still not working. Any hacker that could help with this? Very rare – Red Acid Jun 20 '14 at 09:51

3 Answers3

1

The problem is that the referer of the script is the page that includes the script. Not the page you came from before getting to that page.

What you could do is:

<script type="text/javascript" src="ref.php?referer=<?php
    echo urlencode($_SERVER['HTTP_REFERER']);
?>"></script>

Then you have free access to $_GET['referer'] instead of $_SERVER['HTTP_REFERER'] inside ref.php :)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I suspect this is in some way not possible for the OP (e.g. "the page that includes the script" not being dynamic - why isn't the source of `ref.php` being included from `/index.php` or equivalent). +1 for taking the time to point out the obvious and a workaround though. – AD7six Jun 20 '14 at 10:34
0

If u use mor_rewrite to redirect to your ref.php HTTP_REFERER will be NULL. If you do it, you add some code to .htaccess like this:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !www.example.com [NC]
RewriteRule \.php$ - [F,NC]

If you use ajax, also you should send Referer header by yourself. And so on. If Referer doesn't exests, so request send without it and u should simply add Referer header by yourself

irotaev
  • 69
  • 7
  • Thank you for your help. But I want a solution for the script src way. – Red Acid Jun 20 '14 at 08:10
  • I am thinking I have to open a session to catch the first refer google.com and not mywebsite.com where js is loading.... – Red Acid Jun 20 '14 at 08:13
  • Sorry, i miss a bit, that you mansioned src. I think one way is to path to your ref.php referer url as parameter, you can do it via GET params – irotaev Jun 20 '14 at 08:32
0

This is a pure javascript solution, as you asked for it

(document.referrer.match(/^https?:\/\/([^\/]+\.)?google\.com(\/|$)/i))
    ? document.write("\<script src='http://domain/hello-google.js' type='text/javascript'>\<\/script>"); 
    : document.write("\<script src='http://domain/hello-kitty.js' type='text/javascript'>\<\/script>");

1st UPDATE

Well, if you want to do it from a PHP file or snippet, then you can do it this way

<script type="text/javascript" src="
<?php
if (preg_match('/^https?:\/\/([^\/]+\.)?google\.com(\/|$)/', $_SERVER['HTTP_REFERER'])){
    echo "http://domain/hello-google.js"; 
}else{
    echo "http://domain/hello-kitty.js"; 
}   
?>"></script>

I believe you want to load a different .js file depending on referrer but not redirecting to a .js file since It doesn't make any sense.

2nd UPDATE

Just another silly thing; somehow I believe you just put my snippet into your ref.php but it won't work that way - since you're including PHP file your first line (the javascript directive) should look like this

<script type="text/javascript" src="<?php include('ref.php'); ?>"></script>
hex494D49
  • 9,109
  • 3
  • 38
  • 47
  • That solution is not what I am looking for. I want to detect the referral from the ref.php – Red Acid Jun 20 '14 at 08:40
  • thank you for your help. Tested the code that you provided and still not working. there's a mystery. But if I use document.referrer it works. I will discover why it doesn't work in php – Red Acid Jun 20 '14 at 09:33
  • @Red Acid I wouldn't share a buggy code; both snippets works here so you may have a problem elsewhere – hex494D49 Jun 20 '14 at 10:13
  • Worked fine with First and 2nd Update! thank you for your help. I've seen other posts with the same question all unresolved. – Red Acid Jun 20 '14 at 10:58
  • The bad it will not work from a external server and requires .php page. I am playing with it and I'll update if I find a better solution... But your solution works excellent! – Red Acid Jun 20 '14 at 11:19
  • @Red Acid I'm afraid in this case I don't get this `external server` thing :) but yes, the file has to have a `.php` extension, not `.html`, since you want to use/include the PHP solution/file. Otherwise, use the javascript solution. – hex494D49 Jun 20 '14 at 11:40