How to check if the php script is running on a local server?

前端 未结 6 2074
伪装坚强ぢ
伪装坚强ぢ 2021-02-15 00:44

Is it possible to check if the website (php) is running locally or on a hosted server? I want to enable some logs if the website is running locally and I don\'t want these to ap

6条回答
  •  长情又很酷
    2021-02-15 01:23

    I believe the best approach is to 'fake' a testing mode, which can be done by creating a file in your local environment.
    When I used this approach I created an empty text file called testing.txt and then used the following code:

    if (file_exists('testing.txt')) {
        // then we are local or on a test environment
    } else {
        // we are in production!
    }
    

    This approach is 100% compatible with any Operating System and you can use several test files in case you want a more granular approach (e.g. development.txt, testing.txt, staging.txt, or production.txt) in order to customise your deployment process.

提交回复
热议问题