在线查询IP和WHois源码 PHP版本

在线查询IP和WHois源码 PHP版本

PHP
32
0

1734544687.png

整体页面是我一贯用的蓝色,IP查询的api是免费的,无需更改,Whois需要去https://www.whoisxmlapi.com/自己注册申请然后替换成你的API

主页的右上角默认显示本地ip和国家国旗,天气和天气图标。API免费无需修改。

预览地址

DEMO 地址

下载地址

本站下载 ip-whois.zip

网盘下载 https://cloud.xifeng.net/s/jRfM 密码:6666

代码分享

PHP 主页 index.php

     <?php
// 查询IP地址信息的函数
function getIpInfo($ip) {
    $apiUrl = "http://ip-api.com/json/{$ip}";
    $response = file_get_contents($apiUrl);
    return json_decode($response, true);
}

// 替换为使用WHOIS API的查询函数
function getWhoisInfo($query) {
    $apiKey = "替换为你的WhoisXMLAPI密钥"; // 替换为你的WhoisXMLAPI密钥
    $apiUrl = "https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey={$apiKey}&domainName={$query}&outputFormat=JSON";

    $response = @file_get_contents($apiUrl);

    if ($response === false) {
        return "无法获取WHOIS信息,请检查输入或稍后再试。";
    }

    $whoisData = json_decode($response, true);

    if (isset($whoisData['ErrorMessage'])) {
        return "错误:" . htmlspecialchars($whoisData['ErrorMessage']['msg']);
    }

    return htmlspecialchars($whoisData['WhoisRecord']['rawText']);
}

// 处理用户提交
$ip = $_POST['ip'] ?? '';
$whoisQuery = $_POST['whois_query'] ?? '';
$ipInfo = $whoisInfo = null;

if (filter_var($ip, FILTER_VALIDATE_IP)) {
    $ipInfo = getIpInfo($ip);
}

if (!empty($whoisQuery)) {
    $whoisInfo = getWhoisInfo($whoisQuery);
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IP 和 WHOIS 查询工具</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
        <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap" rel="stylesheet">
    <!-- Font-awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.5.0/css/flag-icon.min.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <header>
        <div class="header-container">
            <a href="/" class="logo-link">
        <img src="/images/logo.png" alt="Logo" class="logo">
    </a>
       <h1 class="header-title">IP 和 WHOIS 查询工具</h1>
        <!-- 顶部IP地址和天气显示 -->
        <div class="header-info">
            <p id="ip-info">IP: 获取中... <img id="country-flag" src="" alt="国旗"></p>
            <p id="weather-info">天气: 获取中...</p>
        </div>
    </div>
        <nav class="navbar">
            <a href="/">首页</a>
            <a href="/about">关于</a>
            <a href="/help">帮助</a>
        </nav>
    </header>
    <div class="container">
        <h2>查询工具</h2>
        <!-- IP查询表单 -->
        <form method="post" action="">
            <div class="form-group">
                <label for="ip">IP地址查询:</label>
                <input type="text" id="ip" name="ip" value="<?= htmlspecialchars($ip) ?>" placeholder="请输入IP地址">
            </div>
            <button type="submit">查询IP信息</button>
        </form>

        <?php if ($ipInfo): ?>
            <div class="result">
                <h3>IP信息:</h3>
                <ul>
                    <li>IP地址:<?= htmlspecialchars($ipInfo['query']) ?></li>
                    <li>国家:<?= htmlspecialchars($ipInfo['country']) ?></li>
                    <li>地区:<?= htmlspecialchars($ipInfo['regionName']) ?></li>
                    <li>城市:<?= htmlspecialchars($ipInfo['city']) ?></li>
                    <li>ISP:<?= htmlspecialchars($ipInfo['isp']) ?></li>
                </ul>
            </div>
        <?php elseif ($ip): ?>
            <div class="result">
                <p>无法获取IP信息,请检查输入。</p>
            </div>
        <?php endif; ?>

        <!-- WHOIS查询表单 -->
        <form method="post" action="">
            <div class="form-group">
                <label for="whois_query">WHOIS查询:</label>
                <input type="text" id="whois_query" name="whois_query" value="<?= htmlspecialchars($whoisQuery) ?>" placeholder="请输入IP或域名">
            </div>
            <button type="submit">查询WHOIS信息</button>
        </form>

        <?php if ($whoisInfo): ?>
            <div class="result">
                <h3>WHOIS信息:</h3>
                <pre><?= $whoisInfo ?></pre>
            </div>
        <?php elseif ($whoisQuery): ?>
            <div class="result">
                <p>无法获取WHOIS信息,请检查输入。</p>
            </div>
        <?php endif; ?>
    </div>
    <footer>
        &copy; 2024 IP 和 WHOIS 查询工具 | 版权所有
    </footer>
    <script src="/js/ip-weather.js"></script>
</body>
</html>

CSS style.css

/* 设置 body 为 flex 布局,确保页脚固定在底部 */
body {
    display: flex;
    flex-direction: column;
    min-height: 100vh; /* 确保页面高度至少为视口高度 */
    font-family: 'Open Sans', '微软雅黑', Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4faff;
    color: #333;
}

/* Header 样式 */
header {
    background-color: #ffffff;
    border-bottom: 2px solid #ddd;
}

.header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1000px;
    margin: 0 auto;
    padding: 15px 20px;
    position: relative;
}

/* 定义旋转动画 */
@keyframes rotateHover {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

/* LOGO 基础样式 */
.logo {
    height: 40px;
}

/* 鼠标悬停触发旋转动画 */
.logo:hover {
    animation: rotateHover 0.5s linear;
}

/* 标题样式 */
.header-title {
    font-size: 24px;
    font-weight: bold;
    color: #013DC4;
    text-transform: uppercase;
    letter-spacing: 1px;
    text-align: center;
    flex-grow: 1;
    position: relative;
    margin: 0;
    padding: 0;
}

/* 标题修饰特效 */
.header-title::after {
    content: '';
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
    width: 60%;
    height: 3px;
    background: linear-gradient(to right, #013DC4, #0056b3);
    border-radius: 2px;
}

/* Header 信息 (IP 和天气) */
.header-info {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 5px;
}

.header-info p {
    margin: 0;
    padding: 5px 10px;
    background-color: #f4faff;
    border: 1px solid #ddd;
    border-radius: 5px;
    font-size: 14px;
    color: #333;
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-info img {
    width: 20px;
    height: 15px;
    border: 1px solid #ddd;
    border-radius: 2px;
}

/* 导航栏 */
.navbar {
    background-color: #013DC4;
    padding: 10px 0;
    text-align: center;
    margin: 0; /* 紧贴 Header */
}

.navbar a {
    color: white;
    text-decoration: none;
    margin: 0 15px;
    font-size: 16px;
    padding: 5px 10px;
    position: relative;
}

/* 导航链接下划线特效 */
.navbar a::after {
    content: "";
    position: absolute;
    width: 0;
    height: 2px;
    background-color: white;
    bottom: 0;
    left: 50%;
    transition: width 0.3s ease, left 0.3s ease;
}

.navbar a:hover::after {
    width: 100%;
    left: 0;
}

.navbar a:hover {
    color: #b6e0ff;
}

/* 主体容器 */
.container {
    flex-grow: 1; /* 使内容部分占据剩余空间 */
    max-width: 1200px; /* 最大宽度限制 */
    width: 100%; /* 确保容器在小屏幕时占满宽度 */
    margin: 20px auto;
    padding: 20px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 使用媒体查询进行响应式调整 */
@media (max-width: 768px) {
    .container {
        max-width: 100%; /* 在小屏幕上占满整个屏幕 */
        padding: 15px;
    }
}

@media (max-width: 480px) {
    .container {
        max-width: 100%;
        padding: 10px;
    }
}


/* === 标题样式 === */
h2 {
    color: #013DC4;
}

/* === 表单样式 === */
form {
    margin-bottom: 30px;
}

.form-group {
    margin-bottom: 15px;
}

label {
    font-weight: bold;
    display: block;
    margin-bottom: 5px;
}

input[type="text"] {
    width: 100%;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 16px;
}

button {
    background-color: #013DC4;
    color: white;
    border: none;
    padding: 10px 15px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
}

button:hover {
    background-color: #002c91;
}

/* === 查询结果样式 === */
.result {
    margin-top: 20px;
    padding: 15px;
    background-color: #e9f7ff;
    border: 1px solid #b6e0ff;
    border-radius: 5px;
}

.result h3 {
    margin-top: 0;
    color: #013DC4;
}

.result pre {
    background-color: #f4faff;
    padding: 10px;
    border-radius: 5px;
    overflow-x: auto;
}

/* 页脚 */
footer {
    background-color: #013DC4;
    color: white;
    text-align: center;
    padding: 10px 20px;
    margin-top: 30px;
}

JS weather 天气

 async function fetchIpAndWeather() {
    try {
        // 使用 ipinfo.io 获取 IP 地址和位置
        const ipResponse = await fetch('https://ipinfo.io/json?token=58004f869449fc'); // 替换为你的 API 密钥
        if (!ipResponse.ok) throw new Error('IP 地址获取失败');
        
        const ipData = await ipResponse.json();
        const ip = ipData.ip;
        const city = ipData.city;
        const region = ipData.region;
        const country = ipData.country;
        const countryCode = ipData.country.toLowerCase();

        // 使用 Flag Icons 类来显示国旗
        const flagClass = `flag-icon flag-icon-${countryCode}`;
        document.getElementById('ip-info').innerHTML = `IP: ${ip} (${city}, ${region}, ${country}) <i class="${flagClass}"></i>`;

        const [latitude, longitude] = ipData.loc.split(',');

        // 获取天气
        const weatherApiKey = '72975f4e3fc84f9ba68160451240111'; // 替换为你从 WeatherAPI.com 获取的 API 密钥
        const weatherResponse = await fetch(`https://api.weatherapi.com/v1/current.json?key=${weatherApiKey}&q=${latitude},${longitude}`);
        if (!weatherResponse.ok) throw new Error('天气获取失败');
        
        const weatherData = await weatherResponse.json();
        const temperature = weatherData.current.temp_c; // 获取摄氏温度
        const weatherDescription = weatherData.current.condition.text;
        const weatherIcon = weatherData.current.condition.icon;

        // 更新天气信息
        document.getElementById('weather-info').innerHTML = `天气: ${city}, ${country} - ${temperature}°C (${weatherDescription}) <img src="https:${weatherIcon}" alt="${weatherDescription}" style="width: 16px; vertical-align:middle;">`;
    } catch (error) {
        // 捕获错误并显示
        console.error("Error:", error.message);
        document.getElementById('ip-info').textContent = 'IP: 获取失败';
        document.getElementById('weather-info').textContent = '天气: 获取失败';
    }
}

// 调用函数以获取数据
fetchIpAndWeather();

在线查询IP和WHois源码 PHP版本
本文作者
发布于
版权协议
转载或引用本文时请遵守许可协议,注明出处、不得用于商业用途!