Jump to content

log ip


british_government

Recommended Posts

sure this is a complete newbie question, at work on our intranet we would like to implement a system for logging the ip of each site visitor say for the index page, for the purposes of monitoring which area uses it most etc.

just wonderd if anyone knew how? closest i know is
<?php echo $_SERVER["REMOTE_ADDR"]; ?>
to display the IP

any help appreciated

Sam
Link to comment
https://forums.phpfreaks.com/topic/36396-log-ip/
Share on other sites

That will echo the IP. You'll want to do something like this:

[code]
$ip = getenv( "REMOTE_ADDR" );
$referer = getenv( "HTTP_REFERER" );

$dbh=mysql_connect ("dblocation", "dbusername", "dbpassword")
or die ("Couldn't connect to the database.");
mysql_select_db ("dbdatabase")
or die("Couldn't select the database.");
$query = mysql_query("INSERT INTO mytable ip = '$ip', referer = '$referer'");
echo ("The information has been added to the database");
[/code]

That will upload the IP address and Page they were viewing into a MySQL database called dbdatabase into the table mytable.

Hope this helps you
Link to comment
https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173170
Share on other sites

We use this sentence and it seems to work quite fine even if the visitor is behind a proxy

[code]
if ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) && (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ((isset($_SERVER['HTTP_CLIENT_IP'])) && (!empty($_SERVER['HTTP_CLIENT_IP']))) {
$ip = explode(".",$_SERVER['HTTP_CLIENT_IP']);
$ip = $ip[3].".".$ip[2].".".$ip[1].".".$ip[0];
} elseif ((!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) || (empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
if ((!isset($_SERVER['HTTP_CLIENT_IP'])) && (empty($_SERVER['HTTP_CLIENT_IP'])))  {
$ip = $_SERVER['REMOTE_ADDR'];
}
} else {
$ip = "0.0.0.0";
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173177
Share on other sites

[quote author=smc link=topic=124772.msg517495#msg517495 date=1170196461]
That will echo the IP. You'll want to do something like this:

[code]
$ip = getenv( "REMOTE_ADDR" );
$referer = getenv( "HTTP_REFERER" );

$dbh=mysql_connect ("dblocation", "dbusername", "dbpassword")
or die ("Couldn't connect to the database.");
mysql_select_db ("dbdatabase")
or die("Couldn't select the database.");
$query = mysql_query("INSERT INTO mytable ip = '$ip', referer = '$referer'");
echo ("The information has been added to the database");
[/code]

That will upload the IP address and Page they were viewing into a MySQL database called dbdatabase into the table mytable.

Hope this helps you
[/quote]

i have used this and setup the database and a table within it called mytable and fields called ip and referer, it says data added to dataase but its not going in?

im probably making a stupid mistake

thanks
Link to comment
https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173181
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.