4

Well. I read some topics in SO but I not found a very specific answer.

I need to check with PHP if a PHP code is running in local or remote host. Currently I check with $_SERVER['SERVER_NAME'] but it is inconsistent. In this case, if I run PHP with listed IPs like 127.0.0.1 or localhost it'll consider local, otherwise remote. If I share my IP with a friend, my code still local, but it consider remote because the shared IP isn't listed.

Well, I think that check IP for localhost is not a good idea (except if you know a good method). I tried methods like gethostbyaddr() and gethostbyname() but don't work correctly too.

I don't have a PHP code to show, but my code is basically that:

// true = localhost
return $_SERVER['SERVER_NAME'] === '127.0.0.1';

The fundamental question is: what can determine that PHP is running local? What is "local" for PHP? I think that it can solve the problem.

Obs.: I don't have access to CMD/Shell with PHP.

8
  • 1
    This question makes no sense. From PHP's perspective, it's always running on the local server. Commented Nov 21, 2012 at 21:30
  • 1
    What is "local" for you? Commented Nov 21, 2012 at 21:30
  • 1
    For perl I added a file local.txt to the filesystem on the local machine and checked it's existence. Commented Nov 21, 2012 at 21:31
  • NullUserException: just the problem.:( zerkms: exactly my problem! :D user4035: I think about that, maybe the unique possible solution. Commented Nov 21, 2012 at 21:53
  • @DavidRodrigues Well, what are you trying to accomplish? Why would you need this? Commented Nov 21, 2012 at 21:59

4 Answers 4

5

You could do what most PHP frameworks do and set a flag during your app's bootstrap phase that defines which environment the code is running in. In it's simplest form:

// the setting when run on a dev machine
define('ENV', 'local');

Then it's a simple case of:

if ( ENV == 'local' )
{
    // do stuff
}
Sign up to request clarification or add additional context in comments.

Comments

4

This is how I do it, which I find more reliable than trying to detect for 127.0.0.1:

if( strpos(gethostname(), '.local') !== false ) { }

Basically, the hostname's on my workstations all have .local appended to it. You can change this to match your workstation's hostname entirely.

Comments

1

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.

If someone is visiting your site via the web, the IP address you see will never be 127.0.0.1 (or ::1 for IPV6), regardless of the usage of a proxy. (Unless of course you're running the proxy yourself on the same server ;)

3 Comments

"This will only be true if running locally" --- you wanted to say "if requested from the same machine"
The request might be remote, but PHP always runs locally.
If you run under virtualbox or vmware , they assign you a different ip - this is an old post but still relevant for the Googling - 'php detect if running locally or on local server '/
1

As far as I know, only you will be able to know what addresses are local or not. Your network could be set up with IP addresses that don't look local at all. PHP cannot as far as I know determine this by itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.