Solved

External PHP IP Logger

Posted on 2008-10-17
20
570 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:Bhump23
  • 11
  • 9
20 Comments
 
LVL 5

Expert Comment

by:RightNL
ID: 22747496
try
 

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

Open in new window

0
 

Author Comment

by:Bhump23
ID: 22747862
Im kind of new at php, can you insert that snippet of code, into my code above. Thx!
0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747868
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bhump23
ID: 22747891
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?
0
 

Author Comment

by:Bhump23
ID: 22747912
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)
 
0
 

Author Comment

by:Bhump23
ID: 22747914
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

0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747917
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.
0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747927
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

0
 

Author Comment

by:Bhump23
ID: 22747934
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?
0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747939
where's your server?
if you are testing on a server that is in your network the server would only see your internal ip..
 
0
 

Author Comment

by:Bhump23
ID: 22747941
No just on a single LAN pc
0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747947
again where is the site hosted? is it on the same computer?
0
 

Author Comment

by:Bhump23
ID: 22747950
site is hosted at:

www.siteburg.com

Its my buddies webhoster.
0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747961
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

0
 

Author Comment

by:Bhump23
ID: 22747981
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?
0
 
LVL 5

Expert Comment

by:RightNL
ID: 22747996
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.
0
 

Author Comment

by:Bhump23
ID: 22748025
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
0
 
LVL 5

Accepted Solution

by:
RightNL earned 500 total points
ID: 22748037
it's your hosting provider that's screwing up..
remote address and server address shouldn't be the same.. unless your working on their server ;)
ask your buddy to fix it...
0
 

Author Comment

by:Bhump23
ID: 22748042
k thx man
0
 

Author Closing Comment

by:Bhump23
ID: 31507384
Helped me on my problem, thx Right
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question