I want PHP to retrieve the domain when the current URL is a subdomain.

Asked 1 years ago, Updated 1 years ago, 321 views

The current URL is
https://a.example.com/
Also
https://b.example.com/
If so, how do I get example.com?

Can't I get it from the predefined variable $_SERVER?

php

2023-02-06 23:38

1 Answers

There seems to be a package called PHP Domain Parser.

how-to examples:
Separate domain names from subdomains in PHP - Qiita

$pslManager=new Pdp\PublicSuffixListManager();
$parser = new Pdp\Parser($pslManager->getList());

$url='www.domainname.net';
$subdomain=$parser->getSubdomain($url);
$domainName=$parser->getRegisterableDomain($url);

/** $subdomain='www'*/
/** $domainName='domainname.net'*/


2023-02-08 01:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.