1

How to track users origin in PHP , i see people using this ref= often i don't have any idea how does this get request works .

My code

 $calling_url = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
Wasim Ahmad
  • 409
  • 3
  • 14
  • Don't use `$_SERVER['HTTP_REFERER']` not all browsers support this feature. – Daan May 12 '15 at 06:49
  • ok so let me try $_GET['ref'] – Wasim Ahmad May 12 '15 at 06:51
  • you can check existing post regarding this. [http://stackoverflow.com/questions/3931485/how-to-track-users-location-region-in-php][1] [1]: http://stackoverflow.com/questions/3931485/how-to-track-users-location-region-in-php – Kamal Sharma May 12 '15 at 06:53
  • @Daan It's fine to use `$_SERVER['HTTP_REFERER']` for tracking or whatever, just don't rely on it for anything critical. It's no more or less safe than trusting query string data or other input. – Wesley Murch May 12 '15 at 07:35
  • @Daan - and whats the alternative or to be more precise: in what way is $_GET["ref"] better? I think this information always depends on the client-browser... – Wolfgang May 12 '15 at 07:41

2 Answers2

3

URL:

http://example.com/index.php?ref=http://externalsite.com/page.html OR
http://example.com/index.php?ref=http%3A%2F%2Fexternalsite.com%2Fpage.html

PHP:

$ref = mysql_real_escape_string(rawurldecode($_GET["ref"]));
// $ref will be "http://externalsite.com/page.html" in both cases

Resources for URL tracking:

Community
  • 1
  • 1
Drakes
  • 23,254
  • 3
  • 51
  • 94
0

Without knowing more about what you're trying to achieve.

If users are coming from outside your site, the best bet is to use HTTP_REFERER. If someone asks your server for a page, you'll have to take their word for it as far as the referrer is concerned. Whether that's in the URL or in the browser. If they don't want to tell you, you can't make them.

Even though it's not that reliable it's going to be more reliable than depending on someone else's application. Unless there is a transaction involved that would encourage the referrers to format the url correctly.

If users are moving around within your own site. You can use the ref= strategy, or you can use session variables.

Loopo
  • 2,204
  • 2
  • 28
  • 45