Link to home
Start Free TrialLog in
Avatar of Bhump23
Bhump23

asked on

External PHP IP Logger

I have many PHP snippets on how to log IP's, but all they do is just log the external IP. Is there anyway to log an external IP, using PHP code?

This is the current PHP IP logger I have now, but it just logs the internal IP address, rather than the external IP.


<?php
 
$to = 'me@memail.com;
$subject = 'Visitors IP';
 
 
$visitorSpecs = 
"<hr size=2 width=300 align=left>".
"<b>Visitor IP address:</b> ".$_SERVER['REMOTE_ADDR'].
"<br>".
"<b>Visitor system specs:</b> ".$_SERVER['HTTP_USER_AGENT'].
"<br>";
 
$headers = "Content-type: text/html \nFrom: IP sniffer script";
 
$body = "<body>
<br>
<table cellspacing=1 cellpadding=2 align=center>
<tr>
<td>
<b><font face=arial size=2>Website visitors IP address and system specs:</font></b>
</td></tr>
<tr>
<td>
<font face=arial size=2> ".$visitorSpecs." </font>
</td></tr></table>
</body>";
 
mail($to,$subject,$body,$headers);
?>

Open in new window

Avatar of RightNL
RightNL

try
 

if ($_SERVER['HTTP_X_FORWARD_FOR']) {
$ip = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
} 

Open in new window

Avatar of Bhump23

ASKER

Im kind of new at php, can you insert that snippet of code, into my code above. Thx!
something like that..
<?php
 
$to = 'me@memail.com;
$subject = 'Visitors IP';
 
if ($_SERVER['HTTP_X_FORWARD_FOR']) {
$ip = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
} 
 
 
$visitorSpecs = 
"<hr size=2 width=300 align=left>".
"<b>Visitor IP address:</b> ".$ip.
"<br>".
"<b>Visitor system specs:</b> ".$_SERVER['HTTP_USER_AGENT'].
"<br>";
 
$headers = "Content-type: text/html \nFrom: IP sniffer script";
 
$body = "<body>
<br>
<table cellspacing=1 cellpadding=2 align=center>
<tr>
<td>
<b><font face=arial size=2>Website visitors IP address and system specs:</font></b>
</td></tr>
<tr>
<td>
<font face=arial size=2> ".$visitorSpecs." </font>
</td></tr></table>
</body>";
 
mail($to,$subject,$body,$headers);
?>

Open in new window

Avatar of Bhump23

ASKER

It's werid I added the snippet you gave me, but now it gives me a parse error:

Parse error: parse error in /home/l/login.siteburg.com/WWW/homepage.php on line 4

But it does not describe what the error is?
Avatar of Bhump23

ASKER

found was missing a "  '  " after my email address.

All I am getting in my email is, the inertnal IP not external. Why is that?

Website visitors IP address and system specs:  

--------------------------------------------------------------------------------
Visitor IP address: 198.173.4.9
Visitor system specs: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; InfoPath.2)
 
Avatar of Bhump23

ASKER

I found the following code for external ips.

Can you insert it into my code for me.


/**
* Call as: $userp = GetUserIP();
*/
function GetUserIP() {
 
    if (isset($_SERVER)) {
 
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
            return $_SERVER["HTTP_X_FORWARDED_FOR"];
        
        if (isset($_SERVER["HTTP_CLIENT_IP"]))
            return $_SERVER["HTTP_CLIENT_IP"];
 
        return $_SERVER["REMOTE_ADDR"];
    }
 
    if (getenv('HTTP_X_FORWARDED_FOR'))
        return getenv('HTTP_X_FORWARDED_FOR');
 
    if (getenv('HTTP_CLIENT_IP'))
        return getenv('HTTP_CLIENT_IP');
 
    return getenv('REMOTE_ADDR');
}

Open in new window

that's the ip that's used to directly connect to the server if both your client and the server are on the same internal subnet that might happen..
but as far as I can tell the 198 is not a privat ip range.
it might help..
try this.


 
 
<?php
 
 
 
function GetUserIP() {
 
    if (isset($_SERVER)) {
 
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
            return $_SERVER["HTTP_X_FORWARDED_FOR"];
        
        if (isset($_SERVER["HTTP_CLIENT_IP"]))
            return $_SERVER["HTTP_CLIENT_IP"];
 
        return $_SERVER["REMOTE_ADDR"];
    }
 
    if (getenv('HTTP_X_FORWARDED_FOR'))
        return getenv('HTTP_X_FORWARDED_FOR');
 
    if (getenv('HTTP_CLIENT_IP'))
        return getenv('HTTP_CLIENT_IP');
 
    return getenv('REMOTE_ADDR');
}
 
$to = 'me@memail.com;
$subject = 'Visitors IP';
 
$ip = GetUserIP();
 
 
$visitorSpecs = 
"<hr size=2 width=300 align=left>".
"<b>Visitor IP address:</b> ".$ip.
"<br>".
"<b>Visitor system specs:</b> ".$_SERVER['HTTP_USER_AGENT'].
"<br>";
 
$headers = "Content-type: text/html \nFrom: IP sniffer script";
 
$body = "<body>
<br>
<table cellspacing=1 cellpadding=2 align=center>
<tr>
<td>
<b><font face=arial size=2>Website visitors IP address and system specs:</font></b>
</td></tr>
<tr>
<td>
<font face=arial size=2> ".$visitorSpecs." </font>
</td></tr></table>
</body>";
 
mail($to,$subject,$body,$headers);
?> 

Open in new window

Avatar of Bhump23

ASKER

thx for the code but one more question. Still getting my internal, but I want the external ip I get at www.whatismyip.com

Any ideas, Right?
where's your server?
if you are testing on a server that is in your network the server would only see your internal ip..
 
Avatar of Bhump23

ASKER

No just on a single LAN pc
again where is the site hosted? is it on the same computer?
Avatar of Bhump23

ASKER

site is hosted at:

www.siteburg.com

Its my buddies webhoster.
try to do a phpinfo();
to see what variable holds the correct IP..
just upload a file with below code
don't forget to delete after you are done..

<?php
 
phpinfo();
 
?>

Open in new window

Avatar of Bhump23

ASKER

Would'nt you have to state the file location/name, of the phpinfo() in my base ip logging script?

Or just insert code in a new file and that should be good?
just create a new file ..
dump that line in . and call it from your browser..
it will give you a lot of info on the server and also what server vars are stored.. then when you find your ip you can use that info to change the script.
Avatar of Bhump23

ASKER

yep game me:

SERVER_ADDR  198.173.4.9
REMOTE_ADDR  198.173.4.9  

so thats not my ip its the servers. Well thx for your help, do you know of any code to change that or just find a new hoster to run the script
ASKER CERTIFIED SOLUTION
Avatar of RightNL
RightNL

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bhump23

ASKER

k thx man
Avatar of Bhump23

ASKER

Helped me on my problem, thx Right