This function extract and return, the correct domain name for any passed value.
Get ever the correct cookie domain name, iterating into array only one time:
function w3all_extract_cookie_domain( $w3cookie_domain ) {
$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");
$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;
return $w3cookie_domain;
}
switch if on localhost:
$w3cookie_domain = 'axew3.com';
//$w3cookie_domain = 'sub.axew3.eu';
//$w3cookie_domain = 'sub1.originalgangster.co.uk';
//$w3cookie_domain = 'sub3.sub2.sub1.example.co.uk';
//$w3cookie_domain = 'sub2.sub1.example.it';
//$w3cookie_domain = 'subdomain.example.co.uk';
//$w3cookie_domain = 'example.co.uk';
$w3cookie_domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
// if( $w3cookie_domain !== false ){
$w3cookie_domain = w3_extract_cookie_domain( $w3cookie_domain );
// }
(on WP_w3all plugin phpBB/WP)
Get complete Domains Names list updated at runtime (array)
The more convenient code that i’ve find out in mind, to get in any case, the correct cookie domain name, where many other different ways fails.
I do not think: but asking myself if there is possibility to have the same result, in less code and more fast than this.
Comments
One response to “Extract (ever) correct domain name for cookie – php function”
All Domains Names updated at runtime (array):
https://www.axew3.com/www/data-hints/w3-all-top-level-domains-names-array.php