The IP Address Lookup Tool in PHP. This tool allows users to input an IP address and retrieve basic information about the IP, including the country, region, and city.
1. PHP Code for IP Address Lookup Tool
This example uses an external API (e.g., ipinfo.io
) to fetch IP details.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="IP Address Lookup Tool to get IP location information."> <meta name="keywords" content="IP Address Lookup, PHP, IP Geolocation, IP Tool"> <title>IP Address Lookup Tool</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; } .container { max-width: 600px; margin: 0 auto; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { text-align: center; } form { margin-top: 20px; } label { display: block; margin-bottom: 5px; font-weight: bold; } input[type="text"], input[type="submit"] { width: 100%; padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 5px; } input[type="submit"] { background-color: #007bff; color: #fff; cursor: pointer; } .result { font-size: 18px; text-align: center; color: #333; } .error { color: red; text-align: center; } </style> </head> <body> <div class="container"> <h1>IP Address Lookup Tool</h1> <p>Enter an IP address to get basic information such as country, region, and city.</p> <form method="post"> <label for="ip_address">Enter an IP Address:</label> <input type="text" id="ip_address" name="ip_address" placeholder="E.g., 8.8.8.8" required> <input type="submit" name="lookup_ip" value="Lookup IP"> </form> <?php if (isset($_POST['lookup_ip'])) { $ip_address = $_POST['ip_address']; // Use ipinfo.io API to get details about the IP address $url = "http://ipinfo.io/{$ip_address}/json"; // Get the content of the API request $ip_info = @file_get_contents($url); if ($ip_info) { $ip_info_data = json_decode($ip_info, true); // Display IP address details echo "<div class='result'>"; echo "<p><strong>IP Address:</strong> " . $ip_address . "</p>"; echo "<p><strong>Country:</strong> " . $ip_info_data['country'] . "</p>"; echo "<p><strong>Region:</strong> " . $ip_info_data['region'] . "</p>"; echo "<p><strong>City:</strong> " . $ip_info_data['city'] . "</p>"; echo "<p><strong>Location:</strong> " . $ip_info_data['loc'] . "</p>"; echo "<p><strong>Organization:</strong> " . $ip_info_data['org'] . "</p>"; echo "</div>"; } else { echo "<div class='error'>Invalid IP address or unable to fetch information.</div>"; } } ?> </div> </body> </html>
2. How the Tool Works
- Input:
- User inputs an IP address.
- Processing:
- The PHP script calls the
ipinfo.io
API to retrieve IP location data. - Output:
- Displays the country, region, city, location (latitude and longitude), and organization associated with the IP address.
3. Integrating the IP Address Lookup Tool into WordPress
Method 1: Creating a Custom Plugin
Steps:
- Create a Plugin Folder:
- Go to
/wp-content/plugins/
and create a folder calledip-lookup-tool
.
- Create the Plugin File:
- Inside the
ip-lookup-tool
folder, create a file namedip-lookup.php
. - Add the following PHP code:
<?php /* Plugin Name: IP Address Lookup Tool Description: A tool to lookup the location and details of an IP address. Version: 1.0 Author: Your Name */ // Function to display the IP Address Lookup Tool function ip_lookup_tool_shortcode() { ob_start(); // Start output buffering ?> <div class="container"> <h1>IP Address Lookup Tool</h1> <p>Enter an IP address to get basic information such as country, region, and city.</p> <form method="post"> <label for="ip_address">Enter an IP Address:</label> <input type="text" id="ip_address" name="ip_address" placeholder="E.g., 8.8.8.8" required> <input type="submit" name="lookup_ip" value="Lookup IP"> </form> <?php if (isset($_POST['lookup_ip'])) { $ip_address = $_POST['ip_address']; // Use ipinfo.io API to get details about the IP address $url = "http://ipinfo.io/{$ip_address}/json"; // Get the content of the API request $ip_info = @file_get_contents($url); if ($ip_info) { $ip_info_data = json_decode($ip_info, true); // Display IP address details echo "<div class='result'>"; echo "<p><strong>IP Address:</strong> " . $ip_address . "</p>"; echo "<p><strong>Country:</strong> " . $ip_info_data['country'] . "</p>"; echo "<p><strong>Region:</strong> " . $ip_info_data['region'] . "</p>"; echo "<p><strong>City:</strong> " . $ip_info_data['city'] . "</p>"; echo "<p><strong>Location:</strong> " . $ip_info_data['loc'] . "</p>"; echo "<p><strong>Organization:</strong> " . $ip_info_data['org'] . "</p>"; echo "</div>"; } else { echo "<div class='error'>Invalid IP address or unable to fetch information.</div>"; } } ?> </div> <?php return ob_get_clean(); // Return the output } // Register the shortcode [ip_lookup_tool] add_shortcode('ip_lookup_tool', 'ip_lookup_tool_shortcode'); ?>
- Activate the Plugin:
- Go to the WordPress Admin Dashboard > Plugins and activate the IP Address Lookup Tool plugin.
- Add the Shortcode:
- To display the tool on any post or page, use the shortcode:
[ip_lookup_tool]
Method 2: Using a PHP Code Execution Plugin
- Install a PHP Code Plugin:
- Install and activate the “Insert PHP Code Snippet” plugin (or similar).
- Add the IP Lookup Code:
- Add the PHP code as a snippet.
- Use the Shortcode:
- The snippet will generate a shortcode, such as
[insert_php_snippet id="1"]
. - Insert this shortcode in any post or page to display the tool.