因为么有防火墙,过滤ip又不想在在本机修改.
详细的看了一阵cdn说明
发现可以在
cdn的 访问控制 -- 防盗链 下设置黑白名单
根据自己远古代码在二次修改吧
cdn传入的 HTTP_X_FORWARDED_FOR ,第一个值是客户ip
懒得折腾循环, 百度cdn , cdn-user-ip ,${remote_addr}
直接调用 cdn-user-ip
- <?php
- $user_ip = $_SERVER['REMOTE_ADDR'];
- echo "REMOTE_ADDR: " . $user_ip;
- echo "<p>";
- echo "--------<p>";
- $xff_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- echo "HTTP_X_FORWARDED_FOR: " . $xff_ip;
- echo "<p>";
- echo "--------<p>";
- echo "--------<p>";
- $Cdn_User_ip = $_SERVER['Cdn-User-Ip'];
- echo "Cdn-User-Ip: " . $Cdn_User_ip;
- //请根据你的具体环境和需求来选择合适的方法获取用户IP地址。在考虑安全性时,使用第三方服务或API来验证和解析IP地址可能是更可靠的选择。
- // 获取 REMOTE_ADDR
- $remote_addr = $_SERVER['REMOTE_ADDR'];
- // 获取 HTTP_X_FORWARDED_FOR,注意它可能包含多个IP地址,取第一个作为用户IP
- $x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
- if ($x_forwarded_for) {
- $user_ip = explode(',', $x_forwarded_for)[0];
- } else {
- $user_ip = $remote_addr;
- }
- echo "User IP: " . $user_ip;
- echo "--------<p>";
- echo "--------<p>";
- if (function_exists('getallheaders')) {
- $http_headers = getallheaders();
- echo "HTTP Headers:\n";
- foreach ($http_headers as $name => $value) {
- echo "$name: $value\n";
- }
- } else {
- echo "getallheaders() function is not available on this server.";
- }
- // 输出 REMOTE_ADDR 值
- $remote_addr = $_SERVER['REMOTE_ADDR'];
- echo "\nREMOTE_ADDR Value: " . $remote_addr;
- ?>
复制代码
|