Solved

External PHP IP Logger

Posted on 2008-10-17
20
559 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now