81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
/// <summary>
|
|
/// ip 的摘要描述
|
|
/// </summary>
|
|
///
|
|
namespace MyWeb
|
|
{
|
|
public class ip:function
|
|
{
|
|
|
|
public ip()
|
|
{
|
|
//
|
|
// TODO: 在這裡新增建構函式邏輯
|
|
//
|
|
}
|
|
|
|
public static string Get()
|
|
{
|
|
string myip = HttpContext.Current.Request.UserHostAddress;
|
|
if (myip == "::1")
|
|
myip = "127.0.0.1";
|
|
return myip;
|
|
}
|
|
|
|
//由ip取得國別
|
|
public string nation(string DefNation="")
|
|
{
|
|
language lang = new language();
|
|
string nation = DefNation != "" ? DefNation : lang.defaultNation();
|
|
|
|
string myip = Get();
|
|
string cname = "ip_" + myip.Replace(".", "_");
|
|
|
|
//清除之前暫存其它ip
|
|
List<string> clearItem = new List<string>();
|
|
foreach (string item in HttpContext.Current.Request.Cookies)
|
|
if (item.Split('_')[0] == "ip" && item != cname)
|
|
clearItem.Add(item);
|
|
foreach (string item in clearItem)
|
|
HttpContext.Current.Response.Cookies[item].Expires = DateTime.Now.AddDays(-1);
|
|
|
|
if (myip != "127.0.0.1")
|
|
{
|
|
if (HttpContext.Current.Request.Cookies[cname] != null)
|
|
{
|
|
nation = HttpContext.Current.Request.Cookies[cname].Value;
|
|
}
|
|
else
|
|
{
|
|
string[] data = WRequest("http://ip2c.org/" + myip, Method.GET , "").data.Split(';'); //1;TW;TWN;Taiwan; Republic of China (ROC)
|
|
if (data[0] == "1" && data.Length >= 2)
|
|
{
|
|
nation = data[1];
|
|
HttpContext.Current.Response.Cookies[cname].Value = nation;
|
|
HttpContext.Current.Response.Cookies[cname].Expires = DateTime.Now.AddDays(3);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isStrNull(lang.Text(nation)))
|
|
{
|
|
//網站有對應的語系
|
|
return nation;
|
|
}
|
|
else
|
|
{
|
|
//網站沒對應的語系,傳回指定預設的語系
|
|
return DefNation != "" ? DefNation : lang.defaultNation();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|