Here is some of my php scripts.
Log ip and dns of visitor
Example: https://6kw.fi/php/ip.php
<?php
// Get the visitor's IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Try to get the DNS name
$dns = @gethostbyaddr($ip);
// Set the filename to store the data
$file = "ip_log.txt";
// Open the file for writing
$fp = fopen($file, "a");
// Write the data to the file
fwrite($fp, "IP: $ip, DNS: $dns, Time: " . date("Y-m-d H:i:s") . "\n");
// Close the file
fclose($fp);
// Display the information on the page
echo "<h2>Visitor Information</h2>";
echo "<p>IP Address: $ip</p>";
echo "<p>DNS Name: $dns</p>";
// Read the file contents and display
echo "<h2>Log</h2>";
echo "<pre>";
readfile($file);
echo "</pre>";
?>
—
Ping ip address.
Example: https://6kw.fi/php/ping.php
<?php
// Function to ping an IP address and return the results
function ping($ip) {
// Execute the ping command using exec function
exec("ping -c 4 $ip", $output, $return_var);
// Check if the ping command was successful
if ($return_var === 0) {
// Remove extra lines from the output
$output = array_slice($output, 1);
echo "Pinged IP address: $ip<br>";
// Loop through each line of the ping command output
foreach ($output as $line) {
// Find the ping time in the line
if (preg_match('/time=(\d+)/', $line, $matches)) {
echo "Ping time: " . $matches[1] . "ms<br>";
}
}
} else {
echo "Ping command failed.<br>";
}
}
// Check if the IP address was submitted via the form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$ip = $_POST["ip"];
// Prompt the user to enter an IP address if the field is empty
if (empty($ip)) {
echo "Please enter an IP address.";
} else {
ping($ip);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>IP Address Ping</title>
</head>
<body>
<form method="post">
<label for="ip">IP Address:</label>
<input type="text" id="ip" name="ip">
<input type="submit" value="Ping">
</form>
</body>
</html>
Traceroute
example: https://6kw.fi/php/traceroute.php
<?php
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the IP address from the form
$ip = $_POST['ip'];
// Sanitize the input to prevent command injection
$ip = escapeshellarg($ip);
// Execute the traceroute command and store the output
$command = "traceroute $ip";
$output = shell_exec($command);
// Display the traceroute output
echo "<pre>$output</pre>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Traceroute</title>
</head>
<body>
<form method="post">
<label for="ip">Enter IP address:</label>
<input type="text" id="ip" name="ip">
<input type="submit" value="Run traceroute">
</form>
</body>
</html>
Log ip and dns of visitor and count total number of visitors.
Example: https://6kw.fi/php/ip2.php
<?php
// Get the visitor's IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Try to get the DNS name
$dns = @gethostbyaddr($ip);
// Set the filename to store the data
$file = "ip_log.txt";
// Open the file for writing
$fp = fopen($file, "a");
// Write the data to the file
fwrite($fp, "IP: $ip, DNS: $dns, Time: " . date("Y-m-d H:i:s") . "\n");
// Close the file
fclose($fp);
// Display the information on the page
echo "<h2>Visitor Information</h2>";
echo "<p>IP Address: $ip</p>";
echo "<p>DNS Name: $dns</p>";
// Count the lines in the file
$lineCount = 0;
$handle = fopen($file, "r");
if ($handle !== false) {
while (($line = fgets($handle)) !== false) {
$lineCount++;
}
fclose($handle);
}
// Display the log and line count
echo "<h2>Log (Visitors count: $lineCount)</h2>";
echo "<pre>";
readfile($file);
echo "</pre>";
?>
Form and Data Display. (save data to data.txt file)
Example: https://6kw.fi/php/form.php Database: https://6kw.fi/php/data.txt
<!DOCTYPE html>
<html>
<head>
<title>Form and Data Display</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
}
form {
margin-bottom: 20px;
padding: 15px;
background-color: #f2f2f2;
border-radius: 5px;
width: 300px;
}
label, input {
display: block;
margin-bottom: 10px;
}
input[type="text"], input[type="tel"] {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
input[type="submit"] {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.message {
margin-bottom: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<?php
// File name
$filename = "data.txt";
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form data and sanitize it
$name = htmlspecialchars(trim($_POST["name"]));
$streetAddress = htmlspecialchars(trim($_POST["streetAddress"]));
$postalCode = htmlspecialchars(trim($_POST["postalCode"]));
$city = htmlspecialchars(trim($_POST["city"]));
$phoneNumber = htmlspecialchars(trim($_POST["phoneNumber"]));
// Get the user's IP address
$ipAddress = $_SERVER['REMOTE_ADDR'];
// Get the DNS information for the IP address
$dnsInfo = gethostbyaddr($ipAddress);
// Get the current date and time
$dateTime = date("Y-m-d H:i:s");
// Validate input
if (empty($name) || empty($streetAddress) || empty($postalCode) || empty($city) || empty($phoneNumber)) {
echo "<div class='message' style='color: red;'>All fields are required.</div>";
} else {
// Open the file for writing (append mode adds data to the end)
if ($file = fopen($filename, "a")) {
// Lock the file before writing
if (flock($file, LOCK_EX)) {
// Write the data to the file
fwrite($file, $dateTime . " - " . $name . " - " . $streetAddress . " - " . $postalCode . " - " . $city . " - " . $phoneNumber . " - IP: " . $ipAddress . " - DNS: " . $dnsInfo . "\n");
// Unlock and close the file
flock($file, LOCK_UN);
fclose($file);
// Display a success message
echo "<div class='message' style='color: green;'>Data saved successfully!</div>";
} else {
echo "<div class='message' style='color: red;'>Could not lock the file for writing.</div>";
}
} else {
echo "<div class='message' style='color: red;'>Error opening file for writing.</div>";
}
}
}
// Read the file contents and display them
if (file_exists($filename) && is_readable($filename)) {
if ($file = fopen($filename, "r")) {
echo "<h2>Saved Data:</h2>";
while (($line = fgets($file)) !== false) {
echo htmlspecialchars($line) . "<br>";
}
fclose($file);
} else {
echo "<div class='message' style='color: red;'>Error reading file.</div>";
}
} else {
echo "<div class='message' style='color: red;'>No data available.</div>";
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="streetAddress">Street Address:</label>
<input type="text" id="streetAddress" name="streetAddress" required><br>
<label for="postalCode">Postal Code:</label>
<input type="text" id="postalCode" name="postalCode" required><br>
<label for="city">City:</label>
<input type="text" id="city" name="city" required><br>
<label for="phoneNumber">Phone Number:</label>
<input type="tel" id="phoneNumber" name="phoneNumber" required><br>
<input type="submit" value="Save">
</form>
</body>
</html>
Feedback form (save data to feedback.txt) Also add timestamp, IP and DNS.
Example: https://6kw.fi/php/feedback.php Database: https://6kw.fi/php/feedback.txt
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Add timestamp
$timestamp = date("Y-m-d H:i:s");
// Get IP and DNS addresses
$ip_address = $_SERVER['REMOTE_ADDR'];
$dns_address = gethostbyaddr($ip_address);
// Get browser information
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Form the data to be saved
$data = "Time: $timestamp\nName: $name\nEmail: $email\nMessage: $message\nIP Address: $ip_address\nDNS Address: $dns_address\nUser Agent: $user_agent\n---\n";
// Save the data to a text file
file_put_contents('feedback.txt', $data, FILE_APPEND | LOCK_EX);
// Display confirmation message
echo "<p>Thank you for your feedback, $name!</p>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Feedback Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 50px auto;
background: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
input[type="text"],
input[type="email"],
textarea {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
display: inline-block;
background-color: #007BFF;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
p {
text-align: center;
font-size: 18px;
color: green;
}
</style>
</head>
<body>
<div class="container">
<h1>Feedback Form</h1>
<form action="feedback.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" required></textarea>
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>
Yet another version of ip logging php script. Arranged results to tables and shortened information of browser and operating systen. Results in inverted order.
example: https://6kw.fi/php/ip5.php database https://6kw.fi/php/ip_log5.txt
<?php
// Get the visitor's IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Try to get the DNS name
$dns = @gethostbyaddr($ip);
// Get the visitor's browser information
$userAgent = $_SERVER['HTTP_USER_AGENT'];
// Function to shorten the user agent string
function getShortUserAgent($userAgent) {
$browser = "Unknown Browser";
$platform = "Unknown OS";
// Detect platform
if (preg_match('/linux/i', $userAgent)) {
$platform = 'Linux';
} elseif (preg_match('/macintosh|mac os x/i', $userAgent)) {
$platform = 'Mac';
} elseif (preg_match('/windows|win32/i', $userAgent)) {
$platform = 'Windows';
}
// Detect browser
if (preg_match('/MSIE/i', $userAgent) && !preg_match('/Opera/i', $userAgent)) {
$browser = 'Internet Explorer';
} elseif (preg_match('/Firefox/i', $userAgent)) {
$browser = 'Firefox';
} elseif (preg_match('/Chrome/i', $userAgent)) {
$browser = 'Chrome';
} elseif (preg_match('/Safari/i', $userAgent)) {
$browser = 'Safari';
} elseif (preg_match('/Opera/i', $userAgent)) {
$browser = 'Opera';
} elseif (preg_match('/Netscape/i', $userAgent)) {
$browser = 'Netscape';
}
return "$browser on $platform";
}
// Shorten the user agent string
$shortUserAgent = getShortUserAgent($userAgent);
// Set the filename to store the data
$file = "ip_log5.txt";
// Open the file for writing
if ($fp = fopen($file, "a")) {
fwrite($fp, "IP: $ip, DNS: $dns, Browser: $shortUserAgent, Time: " . date("Y-m-d H:i:s") . "\n");
fclose($fp);
} else {
die("Error opening log file.");
}
// Display the information on the page as a table
echo "<h2>Visitor Information</h2>";
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 50%;'>";
echo "<tr><th>Information</th><th>Details</th></tr>";
echo "<tr><td>IP Address</td><td>$ip</td></tr>";
echo "<tr><td>DNS Name</td><td>$dns</td></tr>";
echo "<tr><td>Browser</td><td>$shortUserAgent</td></tr>";
echo "<tr><td>Time</td><td>" . date("Y-m-d H:i:s") . "</td></tr>";
echo "</table>";
// Read the file and reverse the lines for display
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
die("Error reading log file.");
}
$lineCount = count($lines);
// Display the log in a table with reversed order
echo "<h2>Log (Visitors count: $lineCount)</h2>";
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
echo "<tr><th>IP Address</th><th>DNS Name</th><th>Browser</th><th>Time</th></tr>";
// Loop through the lines in reverse order
foreach (array_reverse($lines) as $line) {
// Split the log line into components
$parts = explode(", ", $line);
if (count($parts) < 4) continue; // Skip malformed lines
list($logIp, $logDns, $logBrowser, $logTime) = $parts;
// Extract individual details
$logIp = substr($logIp, 4); // Remove "IP: "
$logDns = substr($logDns, 5); // Remove "DNS: "
$logBrowser = substr($logBrowser, 9); // Remove "Browser: "
$logTime = substr($logTime, 6); // Remove "Time: "
// Output each row in the table
echo "<tr><td>$logIp</td><td>$logDns</td><td>$logBrowser</td><td>$logTime</td></tr>";
}
echo "</table>";
?>