What is the best way to extract the origin of a user from a URL in PHP?

To extract the origin of a user from a URL in PHP, you can use the parse_url() function to parse the URL and then extract the scheme, host, and port components to get the origin. This can be useful for determining where a user is coming from on your website.

$url = "https://www.example.com/path/to/page";
$parts = parse_url($url);
$origin = $parts['scheme'] . '://' . $parts['host'];
echo $origin;