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. Edited by requinix
Link to comment
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
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
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
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.