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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.