-1

I have website link http://example.com/link/

How can I handle if it returns from such as: http://facebook.com

I want to check and process event by something like this:

if(return from facebook) {
}

Jquery or PHP is ok.Thank you for your advice.

Hai Tien
  • 2,929
  • 7
  • 36
  • 55
  • 3
    Would phps `$_SERVER['HTTP_REFERER'];` do the job? http://php.net/manual/en/reserved.variables.server.php – atoms Oct 21 '17 at 00:28
  • 1
    [How to know if user came from a Facebook link?](https://stackoverflow.com/q/19485746/1415724) - You should read that. It's going to answer what you're hoping will work; it won't. Same thing here [How reliable is HTTP_REFERER?](https://stackoverflow.com/a/6023980/1415724) – Funk Forty Niner Oct 21 '17 at 01:04
  • Thank you, @Fred-ii-. I will read it now.Update: Thank you so much. – Hai Tien Oct 21 '17 at 01:09

2 Answers2

2

You could find the referer in PHP using:

$_SERVER['HTTP_REFERER']

if($_SERVER['HTTP_REFERER'] == 'https://facebook.com'){ }

However, you'd probably want to catch anything from facebook;

$sReg = '.facebook.+[a-zA-Z](\/*)';

if(preg_match( $sReg, $_SERVER['HTTP_REFERER'] == 1 ){

}

Note that HTTP_REFERER isn't a sure way of getting the referrer. Often it'll be missing.

See PHP manual for more info

atoms
  • 2,993
  • 2
  • 22
  • 43
  • Thank for your answer but if "HTTP_REFERER isn't a sure way of getting the referrer" then can you use another method for sure? – Hai Tien Oct 21 '17 at 00:41
  • unfortunately some browsers wont supply it, be it for privacy or other configuration reasons. See https://stackoverflow.com/questions/6023941/how-reliable-is-http-referer – atoms Oct 21 '17 at 00:46
  • 2
    No, you can not. Users can decide not to let their browsers to send a referrer, or a fake one. And websites can these days also specify whether they want the browser to send a full referrer, the domain name only, or none at all. – CBroe Oct 21 '17 at 00:47
  • Thank atoms and Cbroe for your support. I understood. – Hai Tien Oct 21 '17 at 00:49
1

The solution in javascript

if(document.referrer == 'https://facebook.com') {
    /* Do somethings */
}

Using Regular Expression:

var myRe = new RegExp('facebook.+[a-zA-Z](\/*)');
if(myRe.test(document.referrer)) {
    /* Do somethings */
}
Yulio Aleman Jimenez
  • 1,642
  • 3
  • 17
  • 33