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

Posted on 2004-10-29
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
    LVL 12

    Assisted Solution

    LVL 12

    Expert Comment

    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
    LVL 27

    Accepted Solution

    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

    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

    i mean echo $_SERVER['HTTP_REFERER'];

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

    sorry about that mistake...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP ma…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now