?
Solved

How do I find a WEB-visitor's IP address?

Posted on 2006-03-22
16
Medium Priority
?
341 Views
Last Modified: 2013-12-24
Hello!   Everyone,

This is a second step of Country-Language automatic correllation (redirection) WEB page development.    I learned from Experts-Exchange, that with IP_to_Country General Public Lisence Table, I can figure out from IP number to a country.    

Now, when a WEB-surfer visits a site, I want to detect the surfer's IP number.   I think there is a way to do it.   For example, my business WEB-hit log shows all IP number or Domain Name of all surfers(visitors) in the hit log-table.    But I didn't design that WEB-hit log table, but my contracted ISP programmed it.   So, please let me know (help me), how to figure it out?   Thanks.
0
Comment
Question by:mkido
  • 10
  • 4
  • 2
16 Comments
 
LVL 1

Accepted Solution

by:
hkey_current_user earned 200 total points
ID: 16266447
Well, I've done something like this in php.

In php there's a predefined variable that represents the client IP address ($_SERVER['REMOTE_ADDR'])

So i declare a variable called 'ip that is equal to $_SERVER['REMOTE_ADDR']

and then i submit the value of 'ip' to a database.

PHP/MySQL example:

$ip = $_SERVER['REMOTE_ADDR'];
$db_host = "localhost";
$db_username = "databse_user";
$db_password = "password";
$db_name = "database_name";

mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

$query = "INSERT INTO ip_table (ip) VALUES ('$ip')";
mysql_query($query);

mysql_close();

----------
and then when you want to put the info on a page:

$db_host = "localhost";
$db_username = "databse_user";
$db_password = "password";
$db_name = "database_name";

mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

$query = "SELECT * FROM ip_table";
$result = mysql_query($query);

while($row=mysql_fetch_array($result))
{
echo"Vistor IP address: $row[ip]";
echo"<hr />";
}

----------
It will put on the page something like

Visitor IP address: 1.1.1.1

Visitor IP address: 2.2.2.2

Visitor IP address: 3.3.3.3

Whatever it reads from the database.

You could put other stuff in there as well like browser etc, but im just assuming you're using php. If you're not it could probably be done in a similar fashion in another language i guess.
0
 

Author Comment

by:mkido
ID: 16271603
Thank you, hkey_current_user!

PHP and MySQL are getting very popular, and I'd better to get started.   I only use PERL for all my CGIs up to now.   Could you translate the simplest PERL code below to the PHP code.   Then I may able to get started.

In PERL

#! /usr/local/bin/perl

print "Hello! world!\n";

How does this go in PHP?   PHP is a kind of PERL like script language?   How about in mySQL?   Or any PHP book you learned useful?   Thanks.
0
 
LVL 1

Expert Comment

by:hkey_current_user
ID: 16278804
Print is written in php more or less the exact same way ;)
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 11

Assisted Solution

by:siliconbrit
siliconbrit earned 200 total points
ID: 16288054

In perl the remote users IP address is in the environment variable REMOTE_ADDR.  This is the same for any CGI script.

You can access the value you need as follows:

   $ENV{'REMOTE_ADDR'}

The following perl code will print all environment variables available to you:

 
#!/usr/local/bin/perl

   print "Content-type: text/html\n\n";
   print "<tt>\n";
   foreach $key (sort keys(%ENV)) {
      print "$key = $ENV{$key}<p>";
   }
 
There is a full list and explanation of web environment variables available to CGI scripts at: http://www.perlfect.com/articles/cgi_env.shtml


0
 

Author Comment

by:mkido
ID: 16328562
Hello!   Everyone.    

I got one book of PHP/MySQL and then I am getting understand that how PHP works.   It is very similar with PERL.   One moment, I will try out some suggested codes.  
0
 

Author Comment

by:mkido
ID: 16328815
Dear silionbrit,

I tried your codes in PERL-CGI, and it prints my ISP's environmental variable perfectly.   By the way what is

  "<tt>\n"      in front of    foreach-loop ??

And another question is, by using PERL-cgi, is there a convenient way to put it your PERL code at the main "index.html" page at main WEB access site.   In order to see who (which IP address) is visiting.   My impresssion is that PHP (hkey_current_user) may be convenient to handle this job.    Thank you for your comments!

0
 
LVL 11

Assisted Solution

by:siliconbrit
siliconbrit earned 200 total points
ID: 16330657

The <tt> just tells the browser to write the output in fixed-width font, it just formats nicely.

    http://www.w3schools.com/tags/tag_font_style.asp

The only reason I wrote this in perl was because you said you had experience with perl/CGI.  

Here is a PHP script that gives you what you want:

   <html>
      <head>
         <title>Print remote users IP Address</title>
      </head>
      <body>
         <?php
            echo "<h3>Your IP Address is: " . $_SERVER{'REMOTE_ADDR'} . "</h3>";
         ?>
      </body>
   </html>

You need to place everything from <html> to </html> into a file named "index.php" and DELETE any file named index.html or index.htm from the same directory.

Your web server will look for an index file with an .htm or .html extension and serve this first, if one does not exist, it will look for index.php, so it is important you name this file correctly and dont have an alternative index file.

0
 

Author Comment

by:mkido
ID: 16407848
Thank you, siliconbrit !!

I just got "PHP Cooking", and also am getting static IP Internet at home.    So, let me try now on.   Let me back to here for my feedback a little later.  
0
 
LVL 11

Expert Comment

by:siliconbrit
ID: 16407982

OK, while you are waiting for a static IP, you can use a free dynamic DNS service to point to your dynamic IP address at home:

   http://www.dyndns.org

If you use a router, most have the ability to register your IP address with the Dynamic DNS service when it changes.  If you don't, then there's plenty of software you can install on your workstation to do the same job.  Here's a perl method:

   http://www.dyndns.com/support/kb/archives/using_ddclient_with_dyndns_services.html

Good Luck.
0
 

Author Comment

by:mkido
ID: 16441661
Hello!    

My static IP is working at my home for PC, Macintosh, Linux (Fedora).   I am looking carefully all of your codes now.      Thank you about Dynamic DNS.      I found PHP is very interesting approach, combining PERL and HTML approaches.    
0
 

Author Comment

by:mkido
ID: 16442048
PERL appraoch is successul on my "www.tunefind.net", and here is the IP address and environment returns.

Codes from "siliconbrit,"
#!/usr/local/bin/perl

   print "Content-type: text/html\n\n";
   print "<tt>\n";
   foreach $key (sort keys(%ENV)) {
      print "$key = $ENV{$key}<p>";
   }


My local Internet Service Provider is on a DSL service.



CB_CODE = 1

CB_SLOT = 0

DOCUMENT_ROOT = /web

GATEWAY_INTERFACE = CGI/1.1

HOME = /

HTTP_ACCEPT = text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1

HTTP_ACCEPT_CHARSET = ISO-8859-1,utf-8;q=0.7,*;q=0.7

HTTP_ACCEPT_ENCODING = gzip,deflate

HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5

HTTP_CONNECTION = Keep-Alive, keep-alive

HTTP_HOST = www.tunefind.net

HTTP_KEEP_ALIVE = 300

HTTP_PROXY = 207.155.248.9

HTTP_PROXY_IP = 216.66.233.30

HTTP_REFERER = http://www.tunefind.net/HTM/call51.html

HTTP_USER_AGENT = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.1) Gecko/20031030

LOCALDOMAIN = tunefind.net

LOGNAME = cgiuser

MANPATH = /usr/man

PATH = /bin:/usr/vde/bin:/usr/local/bin:/usr/gnu/bin:/usr/ucb:/usr/ccs/bin:/usr/local/cvbin

PWD = /cgi/TEST

QUERY_STRING =

REMOTE_ADDR = 216.66.233.30

REMOTE_HOST = host-216-66-233-30.lcinet.net

REQUEST_METHOD = GET

SCRIPT_NAME = /cgi/TEST/cgi51.pl

SERVER_ADMIN = www@www.tunefind.net

SERVER_NAME = www.tunefind.net

SERVER_PORT = 80

SERVER_PROTOCOL = HTTP/1.0

SERVER_SOFTWARE = ConcentricHost-Ashurbanipal/1.8 (XO(TM) Web Site Hosting)

SHELL = /usr/local/bin/perl

TZ = EST5EDT

USER = cgiuser

VDE_CB_GRENT = chost

VDE_CB_PROD_NAME = XO(TM) Web Site Hosting

VDE_CB_SIGNUP_PATH = CNC

VDE_CB_TLD = cnchost.com

VDE_DOMAIN = tunefind.net

VDE_INBOUND = vrun:httpd

VDE_LOGNAME = cgiuser@tunefind.net

VDE_PGRPID = 19298

VDE_VERSION = 1.9d (clock)

VDE_ZONE = /cgi/

0
 

Author Comment

by:mkido
ID: 16442057
When I am on Comcast's Cable service,

CB_CODE = 1

CB_SLOT = 0

DOCUMENT_ROOT = /web

GATEWAY_INTERFACE = CGI/1.1

HOME = /

HTTP_ACCEPT = application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1

HTTP_ACCEPT_CHARSET = ISO-8859-1,utf-8;q=0.7,*;q=0.7

HTTP_ACCEPT_ENCODING = gzip,deflate

HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5

HTTP_CONNECTION = Keep-Alive, keep-alive

HTTP_HOST = www.tunefind.net

HTTP_KEEP_ALIVE = 300

HTTP_PROXY = 207.155.248.18

HTTP_PROXY_IP = 24.30.101.79

HTTP_REFERER = http://www.tunefind.net/HTM/call51.html

HTTP_USER_AGENT = Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax;nscd1)

LOCALDOMAIN = tunefind.net

LOGNAME = cgiuser

MANPATH = /usr/man

PATH = /bin:/usr/vde/bin:/usr/local/bin:/usr/gnu/bin:/usr/ucb:/usr/ccs/bin:/usr/local/cvbin

PWD = /cgi/TEST

QUERY_STRING =

REMOTE_ADDR = 24.30.101.79

REMOTE_HOST = c-24-30-101-79.hsd1.ca.comcast.net

REQUEST_METHOD = GET

SCRIPT_NAME = /cgi/TEST/cgi51.pl

SERVER_ADMIN = www@www.tunefind.net

SERVER_NAME = www.tunefind.net

SERVER_PORT = 80

SERVER_PROTOCOL = HTTP/1.0

SERVER_SOFTWARE = ConcentricHost-Ashurbanipal/1.8 (XO(TM) Web Site Hosting)

SHELL = /usr/local/bin/perl

TZ = EST5EDT

USER = cgiuser

VDE_CB_GRENT = chost

VDE_CB_PROD_NAME = XO(TM) Web Site Hosting

VDE_CB_SIGNUP_PATH = CNC

VDE_CB_TLD = cnchost.com

VDE_DOMAIN = tunefind.net

VDE_INBOUND = vrun:httpd

VDE_LOGNAME = cgiuser@tunefind.net

VDE_PGRPID = 3861

VDE_VERSION = 1.9d (clock)

VDE_ZONE = /cgi/

0
 

Author Comment

by:mkido
ID: 16442075
Thus, REMOTE_ADDRs are,
  DSL connection :  216.66.233.30
  Cable connection:  24.30.101.79

They are correct and accurate.   However, I haven't success on "index.php" approach.   The "www.tunefind.net" 's Server didn't respond.    I am not sure my side of error or the Server's setting.   Let me do one more trial.
0
 

Author Comment

by:mkido
ID: 16442125
By the way, I saw two dots in PHP codes around in the following.   Are they grammatical things?

 . $_SERVER{'REMOTE_ADDR'} .

I failed anyway by this PHP approach.   Thank you very much, I will close this post soon.
0
 
LVL 11

Expert Comment

by:siliconbrit
ID: 16444177

The dot means contatenate the strings, so:

  $str1="hello"
  $str2="world"


  $helloworld= $str1 . " " . $str2;

This means add "hello" to " " and then to "world", to form the string "hello world"

0
 

Author Comment

by:mkido
ID: 16451332
Thank you, hkey_current-user and siliconbrit.  

Finally I am getting into PHP and MySQL.   And it takes time.   I can feed back more quick, once I will have the basics.   Bye now.
0

Featured Post

Industry Leaders: 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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

839 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