How can I detect if the user is on localhost in PHP?

前端 未结 9 1260
耶瑟儿~
耶瑟儿~ 2020-12-12 12:45

In other words, how can I tell if the person using my web application is on the server it resides on? If I remember correctly, PHPMyAdmin does something like this for securi

相关标签:
9条回答
  • 2020-12-12 13:37

    I found a easy answer.

    Because all local drives have C: or D: or F: ... etc.

    Just detect if the second character is a :

    if ( substr_compare(getcwd(),":",1,1) == 0)
    {
    echo '<script type="text/javascript">alert(" The working dir is at the local computer ")</script>';
        $client_or_server = 'client';
    }
    else
    {
    echo '<script type="text/javascript">alert(" The working dir is at the server ")</script>';
        $client_or_server = 'server';
    }
    
    0 讨论(0)
  • 2020-12-12 13:38

    How about to compare $_SERVER['SERVER_ADDR'] === $_SERVER['REMOTE_ADDR'] to determine if client is on the same machine as server?

    0 讨论(0)
  • 2020-12-12 13:41

    $_SERVER["REMOTE_ADDR"] should tell you the user's IP. It's spoofable, though.

    Check this bounty question for a very detailed discussion.

    I think what you remember with PHPMyAdmin is something different: Many MySQL Servers are configured so that they can only be accessed from localhost for security reasons.

    0 讨论(0)
提交回复
热议问题