?
Solved

External PHP IP Logger

Posted on 2008-10-17
20
Medium Priority
?
593 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 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