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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.