Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

External PHP IP Logger

Posted on 2008-10-17
20
Medium Priority
?
604 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

571 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