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

前端 未结 9 1309
耶瑟儿~
耶瑟儿~ 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'm sorry but all these answers seem terrible to me. I would suggest rephrasing the question because in a sense all machines are "localhost".

    The question should be; How do I run different code paths depending on which machine it is executed on.

    In my opinion, the easiest way is to create a file called DEVMACHINE or whatever you want really and then simply check

    file_exists('DEVMACHINE')

    Remember to exclude this file when uploading to the live hosting environment!

    This solution is not depending on network configuration, it can not be spoofed and makes it easy to switch between running "live-code" and "dev-code".

提交回复
热议问题