Best way to get hostname with php
What about gethostname()?
Edit: This might not be an option I suppose, depending on your environment. It's new in PHP 5.3. php_uname('n') might work as an alternative.
How to get real host or server name in PHP
Edit: as pointed by Gumbo, the original poster probably means HTTP_HOST rather than HOST_NAME. Otherwise, my answer is plain wrong.
The HTTP_HOST variable reflects the domain name that the visitor used to access the site. If doesn't have anything to do with file paths! Its value is conveniently stored in $_SERVER['HTTP_HOST']. Is there any other way to get it? Of course, there're normally several ways to do things. For instance, this works when PHP runs as Apache module.
<?php
$request_headers = apache_request_headers();
echo $request_headers['Host'];
?>
The question is: why would anyone want to do such a thing? Why replace a reliable standard method with a quirky workaround that eventually fetches the same piece of data from the same place?You have the concern that $_SERVER['HTTP_HOST'] is altered by the HTTP request. Of course it is: that's where it comes from. The browser has to specify what site it wants to visit (that's the base of name based virtual hosts) and if it sends a rogue value, well, it just won't reach the site.
How to get host name from php on shared hosting?
Are you looking lot the $_SERVER['HTTP_HOST']?
PHP $_SERVER['HTTP_HOST'] vs. $_SERVER['SERVER_NAME'], am I understanding the man pages correctly?
That’s probably everyone’s first thought. But it’s a little bit more difficult. See Chris Shiflett’s article SERVER_NAME
Versus HTTP_HOST
.
It seems that there is no silver bullet. Only when you force Apache to use the canonical name you will always get the right server name with SERVER_NAME
.
So you either go with that or you check the host name against a white list:
$allowed_hosts = array('foo.example.com', 'bar.example.com');
if (!isset($_SERVER['HTTP_HOST']) || !in_array($_SERVER['HTTP_HOST'], $allowed_hosts)) {
header($_SERVER['SERVER_PROTOCOL'].' 400 Bad Request');
exit;
}
Get Hostname from IP
Here is the answer,the only way which i could find to get the host from the ip was :
$data = (shell_exec('nslookup ' . $ip_adress));
Here is the working code that will return your host ip:$iper = new IpEr();
$ip_adress = $iper->Get_Ip();
$data = (shell_exec('nslookup ' . $ip_adress));
$data = (explode("\n",$data));
$data = explode(":",$data[0]);
echo trim($data[1]);
And here is the iper class if someone needs it:<?php
class IpEr{
public function Get_Ip(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
?>
Get Host name from given URL
you can try this
<?php
function getHost($Address) {
$parseUrl = parse_url(trim($Address));
return trim(isset($parseUrl['host']) ? $parseUrl['host'] : array_shift(explode('/', $parseUrl['path'], 2)));
}
echo getHost('http://stackoverflow.com/users/login');
Related Topics
Converting Array and Objects in Array to Pure Array
Laravel League/Flysystem Getting File Url with Aws S3
How to Determine the Length (In Pixels) of a String Being Rendered on a Web Page
What Are My Options to Check for Viruses on a PHP Upload
How to Pass an Array into a PHP Soapclient Call
How to Run PHP File Using Cron Jobs
How Does This Pcre Pattern Detect Palindromes
PHP MySQL Pagination with Random Ordering
Converting Between Timezones in PHP
Download the Contents of a Url in PHP Even If It Returns a 404
How to Put Double Quotes Inside a String Within an Ajax JSON Response from PHP
Z-Scores(Standard Deviation and Mean) in PHP