HTTP_REFERER of Originating URL

Hi guys,

So I’m using <input type="hidden" value="<?php echo $_SERVER['HTTP_REFERER'] ?>" name="Referer" /> in a form to log what page the form was submitted on. This works fine, but is there a way I can retrieve the originating external address as well?

As an example, if the user were to come from Facebook and then submit the form then HTTP_REFERER would show Facebook - great! But if the user were to come from Facebook, then browse a few pages on my site, and then submit the form, then the HTTP_REFERER would show the last page visited (an internal page) not the originating one.

How can I log the originating external URL too?


You would have to store the referrers in your session as a referrer only refers to the previous page.

Thanks Dormilich, how would I achieve this please?

$_SESSION['referrer.history'][] = $_SERVER['HTTP_REFERER'];

I would rather make it like this

$host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
if ($host && $host != '')
    $_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];

so it will store only external referrer

you should be starting a session on all your pages to do so though

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.