Jump to content

IP MYSQL


iWareWolf_

Recommended Posts

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

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

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

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

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.