简体   繁体   中英

How to detect local ip address

this is my code to detect my system ip Address

<?php

if ($_POST['login']) {

    $uname = $_POST['uname'];
    $pass = $_POST['pass'];

    $email = $_POST['email'];
    $phnoe = $_POST['phnoe'];
    $ipd = $_SERVER['REMOTE_ADDR'];

    if (mysql_query("Insert Into nennetwork (name, email, password, phone, useripp ) Values ('$uname', '$email', '$pass', '$phnoe', '$ipd' )"))
        $pdmsg = "New Member Added!!";
}

and my IP is 192.168.7.16 but it is giving server address like 103.18.164.206.. i want my System's Ip Is it possible???

use $_SERVER['SERVER_ADDR'];

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.

Extra Knowledge

  1. $_SERVER['HTTP_HOST'] - gives http://192.168.0.1/index.php
  2. $_SERVER['SERVER_NAME'] - gives www.example.com
  3. $_SERVER['SERVER_ADDR'] - gives 127.0.0.1
  4. $_SERVER['REMOTE_ADDR'] - also gives 127.0.0.1

EDIT 01

$ipAddress = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
    $ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}

As it seems your page is hosted on an internet network (for example ip of the server is 2.178.xx ). In this case what I can say is that you cant .

Web Servers can not access inside of the user's network. for example, you have a wifi modem and 3 people are connected. the webserver can only sees your outer (internet) ip address not the inner (in wifi network, sth like 192.168.xx )

So the answer is: you cant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM