Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-10-29
Medium Priority
Last Modified: 2006-11-17
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?


Question by:a_80
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 12

Assisted Solution

minichicken earned 1000 total points
ID: 12442825
LVL 12

Expert Comment

ID: 12442838
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 http://www.google.com
LVL 27

Accepted Solution

Diablo84 earned 1000 total points
ID: 12442839
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.
LVL 27

Assisted Solution

Diablo84 earned 1000 total points
ID: 12442848
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.
LVL 12

Expert Comment

ID: 12442855
i mean echo $_SERVER['HTTP_REFERER'];

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

sorry about that mistake...

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question