Extra
This page is a collection of some of the scripts I write on my free time, which I don't seem to get much these days =( Most of them are php scripts, so you'll need a web server that has php support in order for you to try them.
These scripts are provided as-is without express or implied warranty and this means use them at your own risk. All questions are welcome but I cannot guarantee a reply for each questions asked. Please be noted that I am giving this free support knowingly that I can turn away easily, so please don't be rude in the email.
My ip
Ever wonder how those ip websites knows your ip address? Or how people log your ip when you visit their site? Well, it is actually pretty simple and it can be done by using just php script.
Below contain a sample code that can display the time your visitor visits your page, their ip address, real ip address (If they are running behind proxy), their host address, browser information (including os information) and the referrer (which link they click to get here). I also displayed their ip information by querying a whois server and it saves the information in a log file for further reference. I used print instead of echo to make the code more readable for some people.
This sample code will log all visit to the page (including
web spiders) by the month. However
it does not know how to differentiate unique visits and repetitive visits, it just writes
the log every time someone visits. So when your log file becomes 10 mb don't
assume you have a lot of visitors. The log file will be named YYYY-MM.log
where YYYY is the year & MM is the month.
If you want to see this script live in action go to the demo page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>IP Log</title>
</head>
<body>
<?php
$log = "log";
$ip = $_SERVER['REMOTE_ADDR'];
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER['HTTP_USER_AGENT'];
$referred = $_SERVER['HTTP_REFERER'];
$date_time = gmdate('m-d-y@H:i:sT');
$real_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
print "<p><strong>Your IP address:</strong><br />\n";
if ($real_ip == "") {
print "$ip</p>\n";
} else {
print "$ip</p>\n";
print "<p>You seem to be using a proxy, your real IP is <strong>$real_ip</strong></p>\n";
}
print "<p><strong>Your host address:</strong><br />\n";
print "$hostaddress</p>\n";
print "<p><strong>Your browser info:</strong><br />\n";
print "$browser</p>\n";
print "<p><strong>Time requested:</strong><br />\n";
print "$date_time</p>\n";
print "<p><strong>Where you came from:</strong><br />\n";
if ($referred == "") {
print "Page was directly requested</p>\n";
} else {
print "$referred</p>\n";
}
print "<pre>";
system ("whois -h whois.apnic.net $ip");
print "</pre>";
$logfile = ("".gmdate('Y-m').".log");
$fp = fopen($logfile,"a");
fputs($fp, $date_time." - ".$ip."(".$real_ip.")".$hostaddress." - ".$browser." - ".$referred."\n");
fclose($fp);
?>
</body>
</html>
Show
Show is a little php script to display the source of php, html or JavaScript . It interprets the embedded elements in the source and display them in html format.
Below is the code of the php script but be aware people abusing this script to see all the files in your web server. This script can only display files in the current folder and below, so please unless there is no choice create a special folder to put the files you like to show and you wont have any problem.
To show the content of the file you just have to add this argument after the
php file’s name ?file=file_name.php. For example,
http://www.yourdomain.com/script.php?file=file_name.php
If you want to see the script live, just visit the demo page. Since I have no file to show you, I'll just show the php script itself.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Show</title>
</head>
<body>
<?php
$file = $_GET['file'];
// Check a file name was given
if ( empty($file) || $file == "" ) {
echo "Missing filename.<br \>\n";
exit;
}
// Check file path is allowed
if ( strncmp($file, "/", 1) == 0 || strstr($file, "../") ) {
echo "File name is not allowed: $file.<br \>\n";
exit;
}
// Sanitise file name
$file = EscapeShellCmd(substr($file, 0, 40));
// Check file exists
if ( ! file_exists($file) || ! is_file($file) ) {
echo "File not found or not printable: $file.<br \>\n";
exit;
}
// Attempt to open file
$fp = fopen($file, "r");
if ( ! $fp ) {
echo "Couldn't open file: $file.<br \>\n";
exit;
}
// print lines of the file
echo "<pre>\n";
while ( ! feof($fp) ) {
echo htmlspecialchars(fgets($fp,4096));
}
fclose($fp);
echo "</pre>\n";
?>
</body>
</html>