4

I'm wondering how I can detect if the user is on a localhost.

My reason for doing so is so that I can automatically run production code on a live server, but uncompressed code on my machine, without having to change the link all the time.

I know I can do it in JS like this... if(document.URL.indexOf("localhost:8888") <= 0){

But I need to do it in Php for my WordPress installation. I've come across a similar question here - How can I detect if the user is on localhost in PHP?

So I tried this (below), but it fails to load anything

<?php if(IPAddress::In(array("127.0.0.1","::1"))) { ?>
        <script src="<?php bloginfo('template_url'); ?>/js/scripts.js"></script>
<?php } ?>

I've also tried the suggested solution here, but again, doesn't work for me How can I detect if the user is on localhost in PHP?

In case these details help, I'm using MAMP on Mac, with a WordPress installation.

Community
  • 1
  • 1
SparrwHawk
  • 13,581
  • 22
  • 61
  • 91

3 Answers3

17

Use the $_SERVER global vars: http://www.php.net/manual/en/reserved.variables.server.php

if (in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) {
    // code for localhost here
}

EDIT: Regarding TerryE's comment, you may want to do something like this (or see his regex answer, although it may not be needed):

if (substr($_SERVER['REMOTE_ADDR'], 0, 4) == '127.'
        || $_SERVER['REMOTE_ADDR'] == '::1') {
    // code for localhost here
}

Because the localhost can be anything in 127.0.0.0/8, although 127.0.0.1 is the most common. http://en.wikipedia.org/wiki/Localhost

Although my original answer will probably be fine (it is what Symfony2 uses by default to "protect" the app_dev.php from accidental production use)

Donny Kurnia
  • 5,260
  • 5
  • 35
  • 52
Matt
  • 5,478
  • 9
  • 56
  • 95
  • Great, that works thanks Matt. Finally, do you know if there's any performance hit for checking the address before loading say, loading a script? – SparrwHawk Feb 12 '12 at 23:49
  • I'd say it's pretty insignificant. If anything, `in_array` may be slow for large arrays, but 2 elements shouldn't be slow. Essentially you are making two string comparisons so this shouldn't slow you down. If this is performance critical you should be using a different bootstrap/front controller for your development machine anyway. – Matt Feb 13 '12 at 03:35
  • Actually, 127.0.0.1/8 means that the first 8 bits are fixed. So it's not only 127.0.0.X but even 127.X.Y.Z, although 127.0.0.1 is by far the most common address. – Arjan Feb 13 '12 at 06:17
  • Oh I see, I had that backwards – Matt Feb 13 '12 at 06:23
1

Similar to Matt's answer you could use a server var

if(!(strpos($_SERVER['SERVER_NAME'], 'localhost') === false)){
   //on localhost
}
donutdan4114
  • 1,262
  • 1
  • 8
  • 16
0

Use a preg_match('!127\.0\.\d+\.\d+!', $_SERVER["REMOTE_ADDR"]). The class B match will be OK because sometimes it comes in on 127.0.1.1, etc..

TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Matt's also correct: you should check for the IPv6 loopback as well: `'!(127\.0\.\d+\.\d+|::1)!'` – TerryE Feb 12 '12 at 23:39
  • Do you really need regular expressions for this? And why would localhost ever be 127.0.1.1? I guess I was just pretty sure that localhost was always 127.0.0.1. – Matt Feb 12 '12 at 23:42
  • See [WP:localhost](http://en.wikipedia.org/wiki/Localhost) and you obviously run WinXXXX on your PC/Laptop. A lot of the rest of the world run Linux, IOS, ... :-) – TerryE Feb 13 '12 at 00:46
  • Ah, didn't know about that. But isn't that saying that 127.0.0.X is loopback then, because the `/8` part would affect the last 8 bits only? So basically matching any string starting with `127.0.0.` should work? And yes, I do run Windows primarily, but with a decent amount of Linux experience and a VM. – Matt Feb 13 '12 at 03:39