Menampilkan ip address menggunakan function $_SERVER

Halo master, sy ada proses untuk mendapatkan IP Address dengan mengunakan function $_SERVER nah ketika web pakai alamat DNS contoh lenovo.co.id maka yg terinput di database default contoh 10.24.222.22  walaupun beda pc dan beda jaringan ip yg terinput sama semua. tapi ketika sy akses web sy dengan menggunakan alamat berupa ip misal 10.43.2.24/blabla/home.php maka ip yg teriput didatabase ip yg berbeda. apakah ada solusinya?

avatar Mantofani
@Mantofani

60 Kontribusi 3 Poin

Diperbarui 1 tahun yang lalu

Tanggapan

Ada yang belum jelas. Anda ingin ip address milik server atau milik klien?

aku masi bingung dengan pertanyaannya XD,,, maaf

1 Jawaban:

Yes, there is a solution. You can use the following code to get the client IP address using the $_SERVER function:

$ip = $_SERVER['REMOTE_ADDR'];

The REMOTE_ADDR variable contains the client's IP address. However, if the client is behind a proxy server, this variable may not contain the client's real IP address. In this case, you can use the following code to get the client's real IP address:

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

The HTTP_X_FORWARDED_FOR variable contains a comma-separated list of IP addresses that the client has passed through before reaching your server. The first IP address in the list is the client's real IP address.

If both REMOTE_ADDR and HTTP_X_FORWARDED_FOR are set, then REMOTE_ADDR will contain the client's IP address as it appears to your server, and HTTP_X_FORWARDED_FOR will contain a comma-separated list of all IP addresses that the client has passed through before reaching your server.

Here is an example of how to use the REMOTE_ADDR and HTTP_X_FORWARDED_FOR variables to get the client's IP address:

$ip = $_SERVER['REMOTE_ADDR'];

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

echo $ip;

This code will print the client's IP address to the browser.

avatar adamajalah27
@adamajalah27

119 Kontribusi 40 Poin

Dipost 1 tahun yang lalu

Login untuk ikut Jawaban