Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we check if we are running the PHP script on localhost?

Is there a way to check if we are running a PHP script on localhost - development server or on live server - production server? Is there any PHP constant, variable, function,etc that can give me this information.

I need this to put different settings for production and development server. Now I parse the URL to see which one it is but I was wondering s there any better way to do that. My concern is that we may change the URL of the script and that may ruin my check.

I am looking few a solution with one config file and IF condition in it depending on which I will define different settings. The only problem is that I do not want to change the IF statement when there are changes on the server settings like hostname, document_root or something else that I am using to identify local/remote host.

And want to SVN update from one source without changing anything to my production server.

And I would like ideally to be able to run and CRON jobs with these settings.

like image 270
Yasen Zhelev Avatar asked Jan 24 '11 15:01

Yasen Zhelev


People also ask

How do I know if PHP is running on localhost?

Also there is another easy way to check is localhost or not: this code check if ip is in private or reserved range if it is thene we are in localhost.

How do I know if a PHP script is running?

Check if a PHP script is already running If you have long running batch processes with PHP that are run by cron and you want to ensure there's only ever one running copy of the script, you can use the functions getmypid() and posix_kill() to check to see if you already have a copy of the process running.

How do I run a PHP site locally?

Type the command php -S localhost:8000 to run your site on port 8000. Note: If you get an error that 'php' is not recognized, you likely will need to add it to your path manually. To do that, locate php.exe (for me it is in the directory C:\xampp\php\ ).


6 Answers

I use the SetEnv in my host definition to locate on which environment i am running (Dev, Stage, Production) :

<VirtualHost *:80>
(all the host info)
SetEnv SERVER_CONTEXT "dev"
</VirtualHost>

And each config file as an extra word in it : config.dev.ini, config.stage.ini, config.prod.ini, etc, ... It works like a charm.

like image 172
Mikushi Avatar answered Sep 23 '22 07:09

Mikushi


if($_SERVER["REMOTE_ADDR"]=="127.0.0.1"){
$local = True;
}else{

    $local = False;
}

EDIT

You could also check the first part of the address and see if the server is in the local network, the again assuming your server won't be in the local network when in production

like image 26
armonge Avatar answered Sep 22 '22 07:09

armonge


I set an environment variable in the Apache configuration and check that. This has the advantage over using a PHP configuration file that all your application code remains exactly the same on PROD, TEST, DEV etc; no need to go and make changes after a check out, as the code just pulls the config from Apache.

To demonstrate, the following can be set in your VirtualHost configuration

SetEnv ENVIRONMENT PROD

In your PHP code, you can then check the environment with

$env = getenv('ENVIRONMENT');

If you feel the need to make this available everywhere, you can then use define, but I don't find it necessary (I use the specified environment to load the appropriate configuration files and create a read-only Singleton configuration, which is then used to check any configuration options; better than if ($env == 'PROD') {} type code, as all that logic is in the config, not in your code).

like image 45
El Yobo Avatar answered Sep 25 '22 07:09

El Yobo


Use a config file you include with a define in it

define("DEBUG",true); //set to false for live

and use that in your code, e.g.:

if(DEBUG){}
like image 38
Nanne Avatar answered Sep 25 '22 07:09

Nanne


You can try and use the global $_SERVER['SERVER_NAME'] This should tell you the host name of the system that's running the script. You could use this to determine what settings to use (depending on the system).

I don't know that this will output 'localhost' however and you may need to know your actual host name of your development machine.

like image 32
pseudoramble Avatar answered Sep 23 '22 07:09

pseudoramble


The server has no idea what environment it is unless you tell it to. What I do is use DEFINE to set the environment. My application code is the same on every instance but the my configuration files change. That way you can use .htaccess file include the configuration files on every script and check to see what they settings are.

like image 22
Amir Raminfar Avatar answered Sep 21 '22 07:09

Amir Raminfar