Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

apache

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 appear on the site online.. I can set a variable $local=1; but I'll have to change that before uploading.. is there anyway to automate this task?

Local Server : WampServer 2.0 / Apache WebServer: Apache

like image 259
Nikhil Bhandari Avatar asked Jul 08 '11 14:07

Nikhil Bhandari


People also ask

How can I tell if PHP is running on my server?

Make sure the Web server is running, open a browser and type http://SERVER-IP/phptest.php. You should then see a screen showing detailed information about the PHP version you are using and installed modules.

How do I know if PHP is localhost?

Checking for Localhost Using the $_SERVER Superglobal Variable: We could simply use the REMOTE_ADDR index on the $_SERVER superglobal array to retrieve the IP address of the requesting client from the web server. For example: // are we on localhost?

How do I run a PHP file on a local server?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

Can I test PHP without a web server?

You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.


3 Answers

Check $_SERVER['REMOTE_ADDR']=='127.0.0.1'. This will only be true if running locally. Be aware that this means local to the server as well. So if you have any scripts running on the server which make requests to your PHP pages, they will satisfy this condition too.

like image 161
Michael Mior Avatar answered Oct 28 '22 16:10

Michael Mior


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.

like image 25
lisawebs Avatar answered Oct 28 '22 17:10

lisawebs


You should automate deployment

This is not directly the answer to your question, but in my opinion the better way. In an automated deployment process, setting a variable like $local = true, like other configuration values (for example your db-connection), would be no manual, error prone, task.

Checking for 'localness' is in my opinion the wrong way: you dont want to show your logs to every local visitor (a Proxy may be one), but only when deployed in a testing environment.

A popular tool for automated deployment is Capistrano, there should be PHP-Centric tools too.

like image 21
keppla Avatar answered Oct 28 '22 17:10

keppla