SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Twitter: @SprightlyMedia Manpasand's Avatar
    Join Date
    Feb 2005
    Location
    Internet
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how IP address record?

    Is there a way to record the IP address of users? I would like to be able to verify the geographic location of a users.

  2. #2
    SitePoint Member
    Join Date
    May 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it depends what you have got on your server
    if you are using PHP the array element $_SERVER['REMOTE_ADDR'] contains The IP address from which the user is viewing the current page.

  3. #3
    Twitter: @SprightlyMedia Manpasand's Avatar
    Join Date
    Feb 2005
    Location
    Internet
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using html and cgi mail for form processing.

  4. #4
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a complete PHP function to return the correct user IP address:

    PHP Code:
    function return_user_ip()
    {
        
    $ip FALSE;

        if (!empty(
    $_SERVER['HTTP_CLIENT_IP']))
        {
            
    $ip $_SERVER['HTTP_CLIENT_IP'];
        }

        if (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            
    $ips explode (', '$_SERVER['HTTP_X_FORWARDED_FOR']);

            if (
    $ip)
            {
                
    array_unshift($ips$ip);
                
    $ip FALSE;
            }

            for (
    $i 0$i count($ips); $i++)
            {
                
    $long ip2long($ips[$i]);
                
    // RFC1918 (10.0.0.0/8, 192.168.0.0/16 and 172.16.0.0/12)
                
    if (!preg_match('^(10|192\.168)\.'$ips[$i]) AND 
                    !(
    $long >= -1408237568 AND $long <= -1407188993))
                {
                    if (
    version_compare(phpversion(), '5.0.0''>='))
                    {
                        if (
    $long != false)
                        {
                            
    $ip $ips[$i];
                            break;
                        }
                    }
                    else
                    {
                        if (
    $long != -1)
                        {
                            
    $ip $ips[$i];
                            break;
                        }
                    }
                }
            }
        }

        return (
    $ip) ? $ip $_SERVER['REMOTE_ADDR'];

    To use it:
    PHP Code:
    $user_ip return_user_ip(); 
    After you have the IP address, check it against a geo-location database like ip-to-country.com and you're all set
    ~ Daniel Macedo

  5. #5
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Quote Originally Posted by Manpasand
    I am using html and cgi mail for form processing.
    You can have an .htaccess to allow you to execute PHP inside the .cgi or even .html

    Just place:
    Code:
    AddType application/x-httpd-php .cgi
    in your .htaccess and in the .cgi you can now use the code above

    This assumes you're using Apache and your host has PHP installed
    ~ Daniel Macedo

  6. #6
    Twitter: @SprightlyMedia Manpasand's Avatar
    Join Date
    Feb 2005
    Location
    Internet
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your kind help.


Bookmarks

Posting Permissions

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