• 文章介绍
  • 评价建议
  • 访问随机显示图片,支持jpg,jpeg,png,gif,webp格式图片,加入检测不会在短时间内显示相同的图片,同时加入访问显示短时间同IP大量访问直接404

    写了个简单的访问记录功能,所有访问均会被记录包括调用域名和访问者IP,有个简易的统计页面,域名/admin   输入密码即可访问到,如需修改密码请到 /admin/index.php文件23行改成你的密码

    <?php
    // 图片文件夹路径
    $imageFolder = 'images/';// 这里改成你的图片实际路径
    
    // 检查图片文件夹是否存在图片文件
    $imageFiles = glob($imageFolder . '*.{jpg,jpeg,png,gif,webp}', GLOB_BRACE);
    if (empty($imageFiles)) {
        echo '请上传图片到指定文件夹。';
        exit;
    }
    
    // 随机选择一张图片
    $randomImage = $imageFiles[array_rand($imageFiles)];
    
    // 获取客户端IP地址
    $clientIP = $_SERVER['REMOTE_ADDR'];
    
    // 存储最后一次访问的图片索引
    $lastImageIndex = isset($_COOKIE['last_image_index']) ? intval($_COOKIE['last_image_index']) : -1;
    
    // 遍历图片,确保不会在短时间内同一个IP地址显示相同的图片
    $filteredImages = array_filter($imageFiles, function ($index) use ($lastImageIndex) {
        return $index !== $lastImageIndex;
    }, ARRAY_FILTER_USE_KEY);
    
    // 随机选择一张过滤后的图片
    $randomFilteredImage = $filteredImages[array_rand($filteredImages)];
    
    // 更新最后一次访问的图片索引
    setcookie('last_image_index', array_search($randomFilteredImage, $imageFiles), time() + 3600);
    
    // 根据图片文件的扩展名设置响应头
    $imageExtension = strtolower(pathinfo($randomFilteredImage, PATHINFO_EXTENSION));
    if ($imageExtension === 'jpg' || $imageExtension === 'jpeg') {
        header('Content-Type: image/jpeg');
    } elseif ($imageExtension === 'png') {
        header('Content-Type: image/png');
    } elseif ($imageExtension === 'gif') {
        header('Content-Type: image/gif');
    } elseif ($imageExtension === 'webp') {
        header('Content-Type: image/webp');
    }
    
    // 存储访问日志和限制日志的文件
    $logFile = 'admin/log/img-log.txt';
    
    // 检测调用域名和限制同一IP地址的访问频率
    $limit = 50; // 限制的访问次数
    $interval = 5; // 限制的时间间隔(秒)
    
    // 检测调用域名
    $callingDomain = $_SERVER['HTTP_REFERER'] ?? '';
    if (!empty($callingDomain)) {
        $callingDomain = parse_url($callingDomain, PHP_URL_HOST);
        $timestamp = time();
        file_put_contents($logFile, $clientIP . ' - ' . $callingDomain . ' - ' . $timestamp . "\n", FILE_APPEND);
    }
    
    // 检查访问频率
    $accessCount = 0;
    $accessTime = time() - $interval;
    if (file_exists($logFile)) {
        $accessData = file($logFile, FILE_IGNORE_NEW_LINES);
        foreach ($accessData as $accessLine) {
            if ($accessLine >= $accessTime) {
                $accessCount++;
            }
        }
    }
    
    // 记录当前访问时间
    file_put_contents($logFile, time() . "\n", FILE_APPEND);
    
    // 如果访问次数超过限制,返回 404 Not Found
    if ($accessCount > $limit) {
        http_response_code(404);
        exit;
    }
    
    // 获取文件名的 MD5 值
    $hashedFileName = md5($randomFilteredImage);
    // 提取 MD5 值中的数字部分
    $numericFileName = preg_replace('/[^0-9]/', '', $hashedFileName);
    
    // 读取图片文件并输出
    if ($imageExtension === 'webp') {
        // 使用 file_get_contents 函数读取 WebP 文件内容
        $imageContent = file_get_contents($randomFilteredImage);
        // 输出图片内容
        echo $imageContent;
    } else {
        // 读取其他图片格式文件并输出
        header('Content-Disposition: inline; filename="' . $numericFileName . '.' . $imageExtension . '"');
        readfile($randomFilteredImage);
    }
    ?>
    

    免责声明:
    1:如非特殊说明,本站对提供的素材及源码不拥有任何权利,其版权归原著者拥有。
    2:本站内容均由互联网收集整理、网友上传,并且以技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
    3:本站部分收费内容仅作本站日常维护费用,若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
    4:请勿将源码、软件进行商业交易、转载等行为,源码、软件只为研究、学习所提供,该软件使用后发生的一切问题与本站无关。
    5:站内资源如有侵犯你版权的,请来信1058364519@qq.com指出,本站将立即改正。
    6:素材源码均为网络收集,不承担任何版权问题,不提供任何技术支持!
    7:严重警告本站木屋模型、木屋CAD图纸、木屋效果图未经允许不得转载:凡木屋模型及CAD文章由设计师QINYUHUI设计制作,享有内容所有权,文章仅在QINYUHUI名下网站、信息平台或嘿很有内涵微博发布,未经授权违者必究!!!

    MASUC » 随便简单写的一个随机图片程序分享给大家