Home >Backend Development >PHP Problem >How to get ip address using php

How to get ip address using php

autoload
autoloadOriginal
2021-03-24 17:09:566552browse

How to get ip address using php

This article mainly introduces the use of dos command to obtain the IP address, and also provides the use of php to obtain ipAddress method.

As we all know, when a computer queries the local IP address, it usually uses the dos command

win r call outdosCommand

How to get ip address using php

Use ipconfig/all to get your ip address (I will not expose my IP)

How to get ip address using php

But how to get your IP address in php?

Method one:

<?php  
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
?>

Method two:

<?php 
$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"];
echo $ip;
?>

Method three:

<?php
if(getenv(&#39;HTTP_CLIENT_IP&#39;)) {
  $onlineip = getenv(&#39;HTTP_CLIENT_IP&#39;);} elseif(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;)) {
  $onlineip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);} elseif(getenv(&#39;REMOTE_ADDR&#39;)) {
  $onlineip = getenv(&#39;REMOTE_ADDR&#39;);} else {
  $onlineip = $HTTP_SERVER_VARS[&#39;REMOTE_ADDR&#39;];}echo $onlineip;
?>

Recommended: "php video tutorial" "php tutorial"

The above is the detailed content of How to get ip address using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more