Is there a way to "see" where the visitor came from?

Hi !

I had an idea to "see" where my visitors came from, I mean, if he clicked a link on a webpage to go to my site, which was it?

My idea where to, in some way, "modify" the Javascript History-function.

Something like $previouswebsite = javascript:history(-1);

Is there anyway i can do a function like tihs, or a function that has the smiular result and then store the result in a PHP variable?


Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Diablo84Connect With a Mentor Commented:
You could use the http_referer


which, if set, will contain the refering page however its not greatly reliable as it is client side and can be blocked from being sent etc.

I usually advise using sessions, eg:

$_SESSION['ref'] = $_SERVER['PHP_SELF'];

The refering page will always be stored in the session variable (you must have session_start(); at the TOP of every page).

A simple way of handling the updating is

if (isset($_SESSION['ref'])) $referer = $_SESSION['ref'];
$_SESSION['ref'] = $_SERVER['PHP_SELF'];

Then the refering page will be available in the local variable $referer for use and then the session will be updated without causing you code flow problems.
minichickenConnect With a Mentor Commented:
If the user search on google and clicks on the link to your site.

On your page of your site that got clicked from google, if you do a >> echo  = $_SERVER['HTTP_REFERER']; //should return
Diablo84Connect With a Mentor Commented:
Note that the session method will only work internally, ie. from page clicks on your site.

If you use the http_referer use a fail safe method in case it is not set, eg:

$referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "defaultpage.php";

Then if the referer is not set and you need to redirect them to a page or something you can instead send them to a default page.
i mean echo $_SERVER['HTTP_REFERER'];

and not echo  = $_SERVER['HTTP_REFERER'];

sorry about that mistake...
All Courses

From novice to tech pro — start learning today.