Hello, Im trying To create A code In php That Does the Following:

A. detects visiting users real ip address
B. detects city and country
C. detects browser type and version
D. detects referrer url
E. detects landing page
F. appends this information to a txt file

Im using the following php file hosted in same folder as the txt file, and using

<script src='http://h1.ripway.com/Inny/spy/spy.php'></script>

in my html.

txt file

<html>
<head>
<title>Spy</title>
<body background="http://i14.photobucket.com/albums/a345/Instar/escher_background.gif">
 
</head>
<body>
 
<SCRIPT src='http://h1.ripway.com/Inny/spy/pass.js'></SCRIPT>
 
 
 
<center><font color='red'>
<form method="POST" action="http://www.geobytes.com/IpLocator.htm?GetLocation">
<input type="hidden" name="cid" value="0">
<input type="hidden" name="c" value="0">
<input type="hidden" name="Template" value="iplocator.htm">
<h3>IP Address to locate:<input type="text" name="ipaddress" size="15" value=""> <input type="submit" value="Submit">
</h3>
</form>
</center>

php

<html>
<head>
<title>Spy</title>
</head>
<body background="http://i14.photobucket.com/albums/a345/Instar/escher_background.gif">
<center><h2>
<center> 
<table border="5" bg color="white">
<tr>
<td><center>
<body>
<?php
$spy = fopen("spy.html" , "a");
$site = $HTTP_REFERER;
$ip = $REMOTE_ADDR;
$browser = $HTTP_USER_AGENT;
$time = date("H:i dS F");
 
echo "<font color='red'>The site User came from: " . $_SERVER["HTTP_REFERER"] . "<br />";
echo "<font color='blue'>ALL IP ADRESSES, TIME, SITE AND USERS BROWSERS ARE BEING CAPTURED!";
 
fwrite($spy,"<font color='blue'><<<<<------------BEGIN------------>>>>></font><br />");
fwrite($spy,"<font color='black'>Site user came from: $site <br />");
fwrite($spy,"IP Address: $ip <br />");
fwrite($spy,"Browser: $browser <br />");
fwrite($spy,"Time: $time <br />");
fwrite($spy,"<font color='red'><<<<<------------END------------>>>>></font><p/><p>");
 
fclose($spy);
?>
 
</table>
</center></td>
</tr>
</table border>
</body>
</html>

Im getting odd results as all entrys show same ip . please help?

<<<<<------------BEGIN------------>>>>>
Site user came from: http://herproom.5.forumer.com/index.php?
IP Address: 192.168.0.5
Browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
Time: 00:11 26th November
<<<<<------------END------------>>>>>


<<<<<------------BEGIN------------>>>>>
Site user came from: http://herproom.5.forumer.com/index.php?
IP Address: 192.168.0.5
Browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
Time: 00:15 26th November
<<<<<------------END------------>>>>>


<<<<<------------BEGIN------------>>>>>
Site user came from: http://herproom.5.forumer.com/index.php?
IP Address: 192.168.0.5
Browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
Time: 00:17 26th November
<<<<<------------END------------>>>>>

This cant all be same user as Ive been watching. Ip showing for all entries

Full-screenYour IP address is 192.168.0.5
City: Marina Del Rey California
Country: United States
Continent: North America
Time Zone: PST

Please help me do What Im trying to.

Recommended Answers

All 12 Replies

If you use a <script> tag then you will get the URL/page where the <script> tag is in as the referer.

example: if index.php has:

<script src="spy.php">

Then you'll always get index.php as the referer. This is because the user was on index.php when the HTTP request for spy.php was made.

If you want to get the site the user came from, then you have to save their referer in index.php. You can either append this data to your script src, or save it directly.

eg:

in index.php use:

<script src="spy.php?referer=<?php echo $_SERVER["HTTP_REFERER"]; ?>"></script>

Then in referer.php retrieve the site the user came from via:

$referer = $_GET['referer'];

Or you can just include spy.php in your PHP file:

include('spy.php');

This reduces the HTTP requests/bandwidth to your server.

One thing I noticed is the your spy.php gives non-js output. This will cause any JavaScript debuggers on the HTTP Client (users browsers) to display error messages.
What you want to do is give out a header('Content-Type: text/js'); and also reduce output to a format such as JSON or just JS. If viewed directly, you can still read the output and thus do your own debugging...

Thanks Digital But It dosent seem to work now, did I mess it up? Please show me how to write it. I have

php

<?php 
$spy2 = fopen("spy2.html" , "a");
$time = date("H:i dS F");

fwrite($spy2,"<font color='blue'><<<<<------------BEGIN------------>>>>></font><br />");
fwrite($spy2,"<font color='black'>Site user came from:<b><font color='GREEN'> ".$_SERVER['HTTP_REFERER']."</font></b><br />");
fwrite ($spy2,"$referer = $_GET['referer'];$referer = $_GET['referer']."<br />;
fwrite($spy2,"IP Address: ".$_SERVER['REMOTE_ADDR']."<br />");
fwrite($spy2,"Browser: ".$_SERVER['HTTP_USER_AGENT']."<br />");
fwrite($spy2,$_SERVER['HTTP_USER_AGENT'].": ".$_SERVER['HTTP_USER_AGENT']."<br />");
fwrite($spy2,"Language: ".$_SERVER['HTTP_ACCEPT_LANGUAGE']."<br />");
fwrite($spy2,$_SERVER['HTTP_ACCEPT_CHARSET'].": ".$_SERVER['HTTP_ACCEPT_CHARSET']."<br />");
fwrite($spy2,$_SERVER['HTTP_ACCEPT_ENCODING'].": ".$_SERVER['HTTP_ACCEPT_ENCODING']."<br />");
fwrite($spy2,"Time: ".$time."<br />");
fwrite($spy2,"<font color='red'><<<<<------------END------------>>>>></font><p/><p>");

fclose($spy2);

?>

html

<html>
<title>Spy2</title>
<head>
</head><meta http-equiv="Content-Type" content="text/js; charset=UTF-8" />
<body background="http://i14.photobucket.com/albums/a345/Instar/escher_background.gif">

<center><font color='red'>
<form method="POST" action="http://www.geobytes.com/IpLocator.htm?GetLocation">
<input type="hidden" name="cid" value="0">
<input type="hidden" name="c" value="0">
<input type="hidden" name="Template" value="iplocator.htm">
<h3>IP Address to locate:<input type="text" name="ipaddress" size="15" value=""> <input type="submit" value="Submit">
</h3>
</form>
</center>

in my index.php?

<script src="http://h1.ripway.com/Inny/spy2/spy2.phpreferer=<?php echo $_SERVER["HTTP_REFERER"]; ?>"></script>


the output

http://h1.ripway.com/Inny/spy2/spy2.html

the index.php?

http://herproom.5.forumer.com/index.php?

results still only showing index.php? pages !!
cant seem to get real ip addys?

<<<<<------------BEGIN------------>>>>>
Site user came from: http://herproom.5.forumer.com/index.php?showtopic=4606
IP Address: 192.168.0.5
Browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705): Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
Language: en-us
:
gzip, deflate: gzip, deflate
Time: 04:38 28th November
<<<<<------------END------------>>>>>


<<<<<------------BEGIN------------>>>>>
Site user came from: http://herproom.5.forumer.com/index.php?
IP Address: 192.168.0.5
Browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; Hotbar 10.0.362.0)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; Hotbar 10.0.362.0): Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; Hotbar 10.0.362.0)
Language: en-au
:
gzip, deflate: gzip, deflate
Time: 04:38 28th November
<<<<<------------END------------>>>>>

Try a simple example first:

index.php

<script src="http://h1.ripway.com/Inny/spy2/spy2.php?referer=<?php echo $_SERVER["HTTP_REFERER"]; ?>"></script>

spy2.php

<?php
file_put_contents('log.txt', 'Referer: '.$_GET['referer']."\n");
?>

See if you get the correct referer.

note: You have to click on a link from a different page that points to index.php to get the referer as that page. Or you can simulate a HTTP GET for your index.php page by using telnet:

This way you can be sure of what is being received...

Open your command prompt/terminal/shell:
type in: telnet h1.ripway.com 80

type in: GET /index.php HTTP/1.1
press enter
type in: HTTP-Referer: http://example.com/referer.php
press enter
type in: HOST: domain.com
press enter
type in: Connection: close
press enter
press enter

Then go to your log.txt file to see if you got the correct referer.

nothing at all returned! it seems I cant put

referer=<?php echo urlencode($_SERVER["HTTP_REFERER"]); ?>

in script tags?

Would it be possible to get the info I want and write to a log.txt using javascript instead?
seems my site has a problem parsing php?
or maybe its the host im using for spy.php?
I dont know of another free host that allows php, not sure what version of php my server supports.

Ok if I use an Iframe instead of script tags, it will still write to the log but still not getting correct referrer

index.php

<iframe src="http://h1.ripway.com/Inny/spy2/spy2.php?referer=<?php echo urlencode($_SERVER["HTTP_REFERER"]); ?>"></iframe>

php

<?php 
header("content-type: application/x-javascript"); 
$spy2 = fopen("spy2.html" , "a");
$time = date("H:i dS F");
$referer = $_GET['referer'];$referer = $_GET['referer'];
fwrite($spy2,"<font color='blue'><<<<<------------BEGIN------------>>>>></font><br />");
fwrite($spy2,"<font color='black'>Site user came from:<b><font color='GREEN'> ".$_SERVER['HTTP_REFERER']."</font></b><br />");
fwrite($spy2,"IP Address: ".$_SERVER['REMOTE_ADDR']."<br />");
fwrite($spy2,"Browser: ".$_SERVER['HTTP_USER_AGENT']."<br />");
fwrite($spy2,$_SERVER['HTTP_USER_AGENT'].": ".$_SERVER['HTTP_USER_AGENT']."<br />");
fwrite($spy2,"Language: ".$_SERVER['HTTP_ACCEPT_LANGUAGE']."<br />");
fwrite($spy2,$_SERVER['HTTP_ACCEPT_CHARSET'].": ".$_SERVER['HTTP_ACCEPT_CHARSET']."<br />");
fwrite($spy2,$_SERVER['HTTP_ACCEPT_ENCODING'].": ".$_SERVER['HTTP_ACCEPT_ENCODING']."<br />");
fwrite($spy2,"Time: ".$time."<br />");

fwrite($spy2,"<font color='red'><<<<<------------END------------>>>>></font><p/><p>");

fclose($spy2);

?>

Try using and include() instead of <script> in your index.php PHP and see what you get.

like this?

include()http://h1.ripway.com/Inny/spy2/spy2.php?referer=<?php echo urlencode($_SERVER["HTTP_REFERER"]); ?>

I tried this but it showed up as text with a link to my page

include('http://h1.ripway.com/Inny/spy2/spy2.php');

You don't need the $_SERVER["HTTP_REFERER"] appended to the URL since you are not making a HTTP Request to http://h1.ripway.com/Inny/spy2/spy2.php as you do when you embed a <script> src.
In spy2.php use $_SERVER["HTTP_REFERER"] to get the referer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.