-2

I have 3 php file, I want to get referer of 1st in 3rd file with php.

I need to check the referrer of hits coming in php only.

I can't use document.referrer in file3 as it can be easily cheated, my best bet is to rely somewhat on php referrer.

I am doing this way

file1.php content

<a href="file2.php">file2.php</a>

file2.php content

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

file3.php content

alert("<?php echo $_SERVER['HTTP_REFERER'];?>");

I get alert as http://localhost/site/file2.php but I want alert as http://localhost/site/file1.php

Please see and suggest any possible way to do this in php only.

Thanks

  • 1
    This cannot be really trusted. – Praveen Kumar Purushothaman Apr 28 '16 at 11:00
  • 1
    Why do you have ` – GolezTrol Apr 28 '16 at 11:02
  • @PraveenKumar Not a duplicate at all. – GolezTrol Apr 28 '16 at 11:03
  • @TallboY I know, bot you shouldn't add script tags in a JavaScript file. You only use script tags to include javascript in an HTML document. Separate javascript files don't need these, and actually they will break the syntax. – GolezTrol Apr 28 '16 at 11:06
  • If you don't have access to File2, then I think this is not possible. The information is simply not available anymore when file3 is included. And for a good reason, otherwise Google, jQuery and other 3rd party CDN providers could track the user of your website in an undesired fashion. Or in this cause, *you* could track the users of *somebody else's* website in an undesired fashion. – GolezTrol Apr 28 '16 at 11:11
  • They get it from the party they work for. I happen to work at an online retailer, and we explicitly help our affiliate partners to get the information they need (and nothing more). This information is tracked using cookies or gathered on the server and sent to them by us. – GolezTrol Apr 28 '16 at 11:49
  • Ad networks usually only need to check the current page. They do not need to know the previous page. Google ads are based on the current page only, or maybe based on assumptions based on Googles indexed data and on their history on the visitor itself (if they are logged in in Google/Chrome). For parties that do need a history, like price comparison websites or ad companies who want to know about a converion, they need to co-operator with the party they work for. – GolezTrol Apr 28 '16 at 11:58

3 Answers3

2

You can use document.referrer. You don't have to load PHP for that, simply use a JS file (not PHP).

cmizzi
  • 188
  • 1
  • 14
  • Yes I can but I want to do it in php only for some reason –  Apr 28 '16 at 11:18
  • You can't get `file1` because you're loading a PHP file. So, the server response the previous file used. In your case, you're loading first the `file1`. You've been redirected to the `file2` and load the `file3` with PHP, so the referer will be the `file2`. If you want the `file1`, you have to do that with JS. – cmizzi Apr 28 '16 at 11:20
0

You cannot do it unless you control file2.php, which you said you aren't. The referrer header may be missing altogether, or at best it is pointing to the URL which redirected to your file3.php. And there is exactly 0 or 1 referrer header (in other words, it is optional).

Reference: Wikipedia: HTTP_referer

Tudor Ilisoi
  • 2,934
  • 23
  • 25
0

I have 3 php file, I want to get referer of 1st in 3rd file with php

The HTTP referer is only intended to show the previous URL. However in a lot of cases it won't show any URL at all, and in some cases (Safari) it is somewhat broken and can show URLs from further back in the history.

You need a better explanation of what you are trying to achieve before you will get a definitive answer.

Assuming that you want to check the user visited file1.php at some point before in the session before visiting file3.php, then you need to capture and store their visit to the former then read that value back at the latter.

This could done with a LSO or with PHP sessions.

Ensuring that file1.php was the page immediately before the page which led to file3.php is more complex.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • Please update your question with this and include more details on acceptable and unacceptable page flows. – symcbean Apr 28 '16 at 11:34
  • ...and with all the other details about the problem domain which are scattered about in comments. – symcbean Apr 28 '16 at 11:35