• home
  • forum
  • my
  • kt
  • download
  • Getting Your Visitor's Details using PHP

    Author: 2007-08-24 18:53:04 From:

    You can get quite a bit of information about your visitors without having to use a third party tracking software. I'll outline the PHP commands you can use to capture some of this data. The details you capture can be saved into a database, and retrieved later to check your site's performance and user details. The following information is captured using the server variable ($_SERVER) which is available from PHP 4.10 onwards.

    Visitor's IP address :

    You can get the visitor's IP address using the following command:

    $ip = $_SERVER['HTTP_CLIENT_IP'];

    This will give you the vistor's IP address. You can use this along with an ip to country converter database to see from which country your visitors are come in from. You can head over to http://ip-to-country.webhosting.info/ for one such script.

    You can use PHP to resolve the ip address to a domain name to get the visitor's ISP in most cases. The ISP's domain will show up if PHP is able to resolve the IP to a proper domain. You can do this as follows.

    $ip = $_SERVER['HTTP_CLIENT_IP'];
    $visitor_host = @getHostByAddr( $ip );

    Note : On some servers, using getHostByAddr to resolve domains may cause the script to slow down.

    Referring Page :

    You can capture the referring page, which will give you an indication of which site is sending traffic to your site.

    $referrer_page = $_SERVER['HTTP_REFERER'];

    This will give you the entire URL from which the visitor came to your site. For example if the visitor came from a google search for "i-pod", the referrer url would look something like this :

    http://www.google.co.in/search?q=i-pod&hl=en&lr=&ie=UTF-8&start=50&sa=N

    If you don't want the entire URL capture, but just the domain name stored into the database, you can strip the rest of the URL and save it to the database like so:

    $referrer_page = parse_url( htmlspecialchars( strip_tags( $_SERVER['HTTP_REFERER'] ) ) );

    This will save the referring page as :

    http://www.google.co.in/

    The page being Accessed :

    You can also capture the page being accessed on your server. This information will help you evaluate which parts of your site is getting more page views. A more advanced user can also use this information to create a click-stream of the user. A click-stream is the path that a user follows while he goes through your site. This lets you see how effective your site's navigation is.

    $requested_page = $_SERVER['REQUEST_URI'];

    If the visitor is accessing your page, http://www.yourdomain.com/guestbook.php, the requested page would be 'guestbook.php'

    About the Author
    Vinu Thomas is the author of vinuthomas.com. This tutorial is licensed under a Creative Commons License.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot