iWareWolf_ Posted June 4, 2013 Share Posted June 4, 2013 I was wondering how I could get the ip and turn it into a value that MYSQL can log/read. Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/ Share on other sites More sharing options...
davidannis Posted June 4, 2013 Share Posted June 4, 2013 $myvar=$_SERVER['REMOTE_ADDR']; Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433893 Share on other sites More sharing options...
requinix Posted June 4, 2013 Share Posted June 4, 2013 If they're behind a proxy then REMOTE_ADDR will be just the proxy. Sometimes it's nice enough to pass along the original IP too though: if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else { $ip = $_SERVER["REMOTE_ADDR"]; }I think there's a third one to test for too but I've never seen it in the wild so I don't remember. Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433896 Share on other sites More sharing options...
iWareWolf_ Posted June 4, 2013 Author Share Posted June 4, 2013 Im wondering how could I change it into something MYSQL could read Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433903 Share on other sites More sharing options...
requinix Posted June 4, 2013 Share Posted June 4, 2013 The same way you get any PHP variable into MySQL. Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433906 Share on other sites More sharing options...
kicken Posted June 4, 2013 Share Posted June 4, 2013 As far as Mysql goes, an IP is just another piece of data, and it doesn't do anything special with it. You'd store it into a varchar or a binary column type. To insert it into mysql you do the same as you would any other piece of data. Create an execute an INSERT query with the IP as one of the column's values. Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433919 Share on other sites More sharing options...
Irate Posted June 4, 2013 Share Posted June 4, 2013 If they're behind a proxy then REMOTE_ADDR will be just the proxy. Sometimes it's nice enough to pass along the original IP too though: if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else { $ip = $_SERVER["REMOTE_ADDR"]; }I think there's a third one to test for too but I've never seen it in the wild so I don't remember. That'd be HTTP_CLIENT_IP. Anyway, you can realize what you're trying to do with a getIP() function, something similar to this. function getIP() $ip; { if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR != "unknown") // test forwarded_for and if not unknown { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif(isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; }You can then use the return value of getIP() as INSERT for your MySQL query. require_once('getIP.php'); $mysqli = new mysqli(...); // initialize new MySQLi connection $mysqli->query("INSERT INTO `yourtable` (ip) // assume you have a field named ip VALUES ( `".getIP()."`);"); // carry on with your code Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433951 Share on other sites More sharing options...
Irate Posted June 4, 2013 Share Posted June 4, 2013 That'd be HTTP_CLIENT_IP.Anyway, you can realize what you're trying to do with a getIP() function, something similar to this. <?php // getIP.php function getIP() $ip; { if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != "unknown") // test forwarded_for and if not unknown { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif(isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; ?>You can then use the return value of getIP() as INSERT for your MySQL query. <?php require_once('getIP.php'); $mysqli = new mysqli(...); // initialize new MySQLi connection $mysqli->query("INSERT INTO `yourtable` (ip) // assume you have a field named ip VALUES ( `".getIP()."`);"); // carry on with your code ?> I noticed that I made a slight syntax error, the quote now contains the fixed code. Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433961 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 You don't use back ticks for string values, it should be apostrophes. You also don't need the semicolon on the end. Link to comment https://forums.phpfreaks.com/topic/278735-ip-mysql/#findComment-1433965 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.