一·C#Cloudflare 取客户ip
using Microsoft.AspNetCore.Http;
using System.Linq;
namespace myproject.Extensions
{
public static class HttpContextAccessorExtensions
{
public static string GetIpAddress(this IHttpContextAccessor accessor)
{
if (!string.IsNullOrEmpty(accessor.HttpContext.Request.Headers["CF-CONNECTING-IP"]))
return accessor.HttpContext.Request.Headers["CF-CONNECTING-IP"];
var ipAddress = accessor.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
if (!string.IsNullOrEmpty(ipAddress))
{
var addresses = ipAddress.Split(',');
if (addresses.Length != 0)
return addresses.Last();
}
return accessor.HttpContext.Connection.RemoteIpAddress.ToString();
}
}
}
二·Cloudflare 取客户ip
<?php
if (empty($_SERVER['HTTP_CF_CONNECTING_IP']))
{
$ip =$_SERVER['REMOTE_ADDR'];
}else
{
$ip =$_SERVER['HTTP_CF_CONNECTING_IP'];
}
if (empty($ip)) {
$ip = $_SERVER['REMOTE_ADDR'];
}
echo $ip;
?>
© 版权声明
文章版权归作者所有,转载请预留文章地址。s.wbsun.net
THE END
暂无评论内容