Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Created July 2, 2025 08:26
Show Gist options
  • Save tranchausky/4c287f22727828f8ea89817e0b0cdc46 to your computer and use it in GitHub Desktop.
Save tranchausky/4c287f22727828f8ea89817e0b0cdc46 to your computer and use it in GitHub Desktop.
php get domain at
<?php
function get_absolute_root_url_my($with_scheme = true)
{
$url = '';
if ($with_scheme)
{
$host = '';
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
} else if (isset($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
}
if (strpos($host, 'localhost') !== false) {
$url .= 'http://';
} else {
$url .= 'https://';
}
$url .= $host;
// Thêm cổng nếu cần
if ($host && strpos($host, 'localhost') !== false) {
$is_https = (strpos($url, 'https://') === 0);
if ((!$is_https && $_SERVER['SERVER_PORT'] != 80) ||
($is_https && $_SERVER['SERVER_PORT'] != 443))
{
$url_port = ':' . $_SERVER['SERVER_PORT'];
if (strrchr($url, ':') != $url_port) {
$url .= $url_port;
}
}
}
}
$url .= cookie_path();
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment