www.webdeveloper.com
Results 1 to 3 of 3

Thread: Logging IP Addresses

  1. #1
    Join Date
    Apr 2009
    Posts
    346

    Logging IP Addresses

    I want to be able to record and write to a file the IP address of users when they go to a certain page on a website. Can this be done with PHP?

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Can this be done with PHP?
    yes it can.

    The calling IP addresses are stored in a session array $_SERVER and can be got at by $_SERVER['REMOTE_ADDR'].

    You can then save the value to a file or database.

  3. #3
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    240
    REMOTE_ADDR does not always contain the right IP.

    PHP Code:
    function get_ip() {
        if(!empty(
    $_SERVER['HTTP_CLIENT_IP'])) return $_SERVER['HTTP_CLIENT_IP'];
        elseif(!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) return $_SERVER['HTTP_X_FORWARDED_FOR'];
        elseif(!empty(
    $_SERVER['HTTP_X_FORWARDED'])) return $_SERVER['HTTP_X_FORWARDED'];
        elseif(!empty(
    $_SERVER['HTTP_FORWARDED_FOR'])) return $_SERVER['HTTP_FORWARDED_FOR'];
        elseif(!empty(
    $_SERVER['HTTP_FORWARDED'])) return $_SERVER['HTTP_FORWARDED'];
        else return 
    $_SERVER['REMOTE_ADDR'];


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles