british_government Posted January 30, 2007 Share Posted January 30, 2007 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 IPany help appreciatedSam Link to comment https://forums.phpfreaks.com/topic/36396-log-ip/ Share on other sites More sharing options...
smc Posted January 30, 2007 Share Posted January 30, 2007 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 More sharing options...
rantsh Posted January 30, 2007 Share Posted January 30, 2007 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 More sharing options...
british_government Posted January 30, 2007 Author Share Posted January 30, 2007 [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 mistakethanks Link to comment https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173181 Share on other sites More sharing options...
ShogunWarrior Posted January 30, 2007 Share Posted January 30, 2007 I believe: [code]INSERT INTO mytable ip = '$ip', referer = '$referer'[/code]Should be:[code]INSERT INTO mytable (ip,referer) VALUES('$ip','$referer')[/code] Link to comment https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173183 Share on other sites More sharing options...
Ninjakreborn Posted January 30, 2007 Share Posted January 30, 2007 Yes, he is right.The format you have, is what you use when using something like SELECT. Insert has a different way of doing it, as he showed you.You would do something like what you did if you were using UPDATE or something else. Link to comment https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173185 Share on other sites More sharing options...
british_government Posted January 30, 2007 Author Share Posted January 30, 2007 yeah i used smc's solution with ShogunWarrior's editthanks alot, works perfectly, anything to keep the boss happy! Link to comment https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173190 Share on other sites More sharing options...
smc Posted January 30, 2007 Share Posted January 30, 2007 Oh haha I my apologies, I just got done writing about 10 Select scripts :PGlad you got it working :) Link to comment https://forums.phpfreaks.com/topic/36396-log-ip/#findComment-173207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.