无论如何在PHP中使用唯一标识用户的路由器?

Is there anyway in PHP to use uniquely identify a user's router? I'd like t know what users are on the same router.

You can retrieve the users IP address using $_SERVER['REMOTE_ADDR'] and then use a traceroute utility to get the hops and check for the routers that you wish to find, if you know the IPs of both of them.

Assuming that the router is not address-translating for them, otherwise this will not work, as it relies on being able to get back through the router to the user. If either router can return to the user, then it will not work at all. Good for dial-in users though.

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$router1IP = "123.123.123.123";

$output = array();
exec('traceroute -n ' . $ip, $output, $result);

if ($result == 0) {
    $outputText = implode("
", $output);
    if (preg_match("/" . preg_quote($router1IP) . "/", $output)) {
        echo "Router 1";
    } else {
        echo "Router 2";
    }
}
?>

The general answer is going to basically be no - detecting network topology is a non-trivial operation (and certainly detecting the path packets took to you can be well-nigh impossible from an endpoint). However, it sounds like you have some kind of specific topology which perhaps if you tell us more about we might be able to help you with.

Presuming the following things:

  • You have two ingress routers
  • Your web server only has one ethernet interface
  • You're trying to determine which router the traffic came through

You could VLAN tag all incoming traffic at each ingress interface (and put all your hosts on untagged switch interfaces), and give yourself an interface alias on your web server for each VLAN, and thus by knowing which virtual interface you came in on, you'll know what ingress router the traffic came from.

The best you can do is identify a users IP address using the $_SERVER['REMOTE_ADDR']. If ip address corresponds to router, you're in luck.