CakeFest 2024: The Official CakePHP Conference

gethostname

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

gethostnameホスト名を取得する

説明

gethostname(): string|false

gethostname() 関数は、ローカルのマシンに設定されている標準的なホスト名を取得します。

パラメータ

この関数にはパラメータはありません。

戻り値

成功時にはホスト名を示す文字列。失敗時には false を返します

例1 簡単な gethostname() 関数の使用例

<?php
echo gethostname(); // 出力の例: sandie
?>

参考

  • gethostbyname() - インターネットホスト名に対応するIPv4アドレスを取得する
  • gethostbyaddr() - 指定した IP アドレスに対応するインターネットホスト名を取得する
  • php_uname() - PHP が稼動しているオペレーティングシステムに関する情報を返す

add a note

User Contributed Notes 4 notes

up
-1
markosjal AT Gmail
1 year ago
Since I built a PHP app that runs on Linux Windows and MacOS I just discovered that using gethostname() behaves differently on different OSes. Linux will return "hostname" while MacOS 10.15 will return "hostname.local" . Have yet to determine how Windows behaves but the difference is worth noting. Only a few days ago I was wanting to get the LAN extension in Linux and was never able to. I was oly hable to get "hostname". In MacOS it is just there. with "hostname.local"
up
-1
php at pk1048 dot com
10 months ago
Regarding Linux vs. macOS, that is not a difference in OS or PHP. macOS sets the hostname to <shortname>.local. Open a terminal window and run `hostname` to check. The local hostname can be set on macOS under the Sharing Preferences (or Settings if Ventura 13 or newer).
up
-33
kurdtpage at gmail dot com
5 years ago
On Linux, use the command `hostname` to view your host name, or `sudo hostname servername` to set it
up
-34
solaris
4 years ago
To the prev. note: **NEVER USE***
#`sudo hostname servername`

Some *nix systems will then force to set the 'servername' as new hostname!
To Top