以编程方式获取cookie的域名

时间:2011-12-27 03:55:38

标签: php regex cookies

我想为cookie和会话cookie设置域,以便它适用于所有子域。我知道我可以使用以下方法做到这一点:

session_set_cookie_params(0, '/', '.example.com');
set_cookie('name', value, expires, '/', '.example.com');

如何从PHP中的服务器变量中获取.example.com?问题是,我需要让它适用于所有类型的域模式,例如

  • www.example.com
  • example.com
  • example.co.uk
  • subdomain.example.co.uk
  • sub1.sub2.example.co.uk

3 个答案:

答案 0 :(得分:1)

我找到了一个answer并提出了以下解决方案,该解决方案大部分时间都可以使用,但域名中包含2个字符TLD(如www.ex.co)的3个或更少字符除外:

$domain = false;
$host = parse_url('http://'.$_SERVER['SERVER_NAME'], PHP_URL_HOST);
if(preg_match('/([a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $host, $m)) {
    $domain = $m[1];
}

答案 1 :(得分:0)

您可以使用$ _SERVER ['SERVER_NAME']变量。它将返回正在执行脚本的主机。

如果你想要一个特定的部分,你可以使用爆炸。

$temp = explode(".", $_SERVER['SERVER_NAME']);
$host = implode(".", ($temp[count($temp) - 1], $temp[count($temp) - 2]));

我不推荐它,例如.co.uk域名会被错误地捕获。你明白了。

答案 2 :(得分:0)

如果没有包含值的数组,这似乎是不可能的。 可以避免迭代到数组以检查+ - 所有域,但不是所有域。有了这样的东西(也许可以改进),你将获得正确的cookie域名。它只迭代一次数组,并检查一个值: https://www.axew3.com/w3/2017/05/extract-domain-name-for-cookie-php-function/

已编辑:代码已得到改进。

改进版本

$w3domains = array(".aero",".biz",".cat",".com",".coop",".edu",".gov",".info",".int",".jobs",".mil",".mobi",".museum",
    ".name",".net",".org",".travel",".ac",".ad",".ae",".af",".ag",".ai",".al",".am",".an",".ao",".aq",".ar",".as",".at",".au",".aw",
    ".az",".ba",".bb",".bd",".be",".bf",".bg",".bh",".bi",".bj",".bm",".bn",".bo",".br",".bs",".bt",".bv",".bw",".by",".bz",".ca",
    ".cc",".cd",".cf",".cg",".ch",".ci",".ck",".cl",".cm",".cn",".co",".cr",".cs",".cu",".cv",".cx",".cy",".cz",".de",".dj",".dk",".dm",
    ".do",".dz",".ec",".ee",".eg",".eh",".er",".es",".et",".eu",".fi",".fj",".fk",".fm",".fo",".fr",".ga",".gb",".gd",".ge",".gf",".gg",".gh",
    ".gi",".gl",".gm",".gn",".gp",".gq",".gr",".gs",".gt",".gu",".gw",".gy",".hk",".hm",".hn",".hr",".ht",".hu",".id",".ie",".il",".im",
    ".in",".io",".iq",".ir",".is",".it",".je",".jm",".jo",".jp",".ke",".kg",".kh",".ki",".km",".kn",".kp",".kr",".kw",".ky",".kz",".la",".lb",
    ".lc",".li",".lk",".lr",".ls",".lt",".lu",".lv",".ly",".ma",".mc",".md",".mg",".mh",".mk",".ml",".mm",".mn",".mo",".mp",".mq",
    ".mr",".ms",".mt",".mu",".mv",".mw",".mx",".my",".mz",".na",".nc",".ne",".nf",".ng",".ni",".nl",".no",".np",".nr",".nu",
    ".nz",".om",".pa",".pe",".pf",".pg",".ph",".pk",".pl",".pm",".pn",".pr",".ps",".pt",".pw",".py",".qa",".re",".ro",".ru",".rw",
    ".sa",".sb",".sc",".sd",".se",".sg",".sh",".si",".sj",".sk",".sl",".sm",".sn",".so",".sr",".st",".su",".sv",".sy",".sz",".tc",".td",".tf",
    ".tg",".th",".tj",".tk",".tm",".tn",".to",".tp",".tr",".tt",".tv",".tw",".tz",".ua",".ug",".uk",".um",".us",".uy",".uz", ".va",".vc",
    ".ve",".vg",".vi",".vn",".vu",".wf",".ws",".ye",".yt",".yu",".za",".zm",".zr",".zw");

$w3cookie_domain = 'axew3.com';
//$w3cookie_domain = 'sub.mydomain.eu';
//$w3cookie_domain = 'tes.originalgangster.co.uk';
//$w3cookie_domain = 'sub3.sub2.sub1.example.co.uk';
//$w3cookie_domain = 'subdomain.example.co.uk';
//$w3cookie_domain = 'sub1.sub2.example.it';
//$w3cookie_domain = 'example.co.uk';

$count_dot = substr_count($w3cookie_domain, ".");

     if($count_dot >= 3){
      preg_match('/.*(\.)([-a-z0-9]+)(\.[-a-z0-9]+)(\.[a-z]+)/', $w3cookie_domain, $w3m0, PREG_OFFSET_CAPTURE);
      $w3cookie_domain = $w3m0[2][0].$w3m0[3][0].$w3m0[4][0];
   }

   $ckcd = explode('.',$w3cookie_domain);

  if(!in_array('.'.$ckcd[1], $w3domains)){
   $w3cookie_domain = preg_replace('/^[^\.]*\.([^\.]*)\.(.*)$/', '\1.\2', $w3cookie_domain);
  }

    $w3cookie_domain = '.' . $w3cookie_domain;

    echo $w3cookie_domain;