Icebergness Posted January 27, 2012 Share Posted January 27, 2012 Hi, I have a page on my Intranet which lists a bunch of install files (for example, Microsoft Office), which are stored in the same directory structure as the site itself. I also have a duplicate installs folder structure in another office, on their local server. I want to make it so that the link changes, depending on which office the user is in. To simplify: If (user is in London) display London link else display Blackpool link I've been trying to make it work via IP Address. The IP range in London is 10.44.6.* However, I can't make the script work with a wildcard (it works fine if I put a specific address in). The code I have so far is: <?php $ip=$_SERVER['REMOTE_ADDR']; if ($ip == '10.44.6.*') { ?><a href="link1.php">Click Here</a><?php } else { ?><a href="link2.php">Click Here</a><?php }; ?> I think it may be an obvious solution, but if some kind person could perhaps point out my mistakes? Thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/ Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2012 Share Posted January 27, 2012 There's no such thing as a wildcard in IP an address. You have to write code to figure out what you want to do with the address. To do this properly, you'd need to write code that would allow you to enter an IP address a network address and a subnet mask, then perform a binary AND operation to see if the given IP address falls within the range of network addresses defined by the mask. Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1311697 Share on other sites More sharing options...
bspace Posted January 28, 2012 Share Posted January 28, 2012 in this case you could remove everything after the last . in $ip giving 10.44.6. then this would work if ($ip == '10.44.6.') Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1311845 Share on other sites More sharing options...
.josh Posted January 28, 2012 Share Posted January 28, 2012 in this case you could remove everything after the last . in $ip giving 10.44.6. then this would work if ($ip == '10.44.6.') this won't work, because for instance, '10.44.6.' does not equal '10.44.6.1'. You use something like strpos instead. Alternatively you can use regex to match it, if you want to match ranges. Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1311850 Share on other sites More sharing options...
kicken Posted January 28, 2012 Share Posted January 28, 2012 Use the netmask to and ip2long to compare. $offices = array( array('mask' => 0xFFFFFF00, 'base' => ip2long('10.44.6.0') //... ); $ip = ip2long('10.44.6.56'); //whatever client's IP is foreach ($offices as $off){ if (($ip&$off['mask']) == $off['base']){ //it's this one } } Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1311864 Share on other sites More sharing options...
bspace Posted January 28, 2012 Share Posted January 28, 2012 in this case you could remove everything after the last . in $ip giving 10.44.6. then this would work if ($ip == '10.44.6.') this won't work, because for instance, '10.44.6.' does not equal '10.44.6.1'. You use something like strpos() instead. Alternatively you can use regex to match it, if you want to match ranges. but i wasn't sugesting comparing '10.44.6.' with '10.44.6.1' I was suggesting that if you take $ip, remove anything after the last '.' you will then effectively be comparing '10.44.6.' with '10.44.6' no matter what the last part of the address is '10.44.6.' does equal '10.44.6.' there are obviously othe similar approaches, the point was that the check could be made without involving wildcards or netmasks etc Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1312052 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2012 Share Posted January 28, 2012 But how do you know what network 10.44.6.1 refers to without a subnet mask? You don't; it's ambiguous. Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1312065 Share on other sites More sharing options...
Icebergness Posted January 30, 2012 Author Share Posted January 30, 2012 in this case you could remove everything after the last . in $ip giving 10.44.6. then this would work if ($ip == '10.44.6.') this won't work, because for instance, '10.44.6.' does not equal '10.44.6.1'. You use something like strpos instead. Alternatively you can use regex to match it, if you want to match ranges. Hi guys, I went with the strpos idea which worked perfectly. I put this code in the header: <?php $ip = $_SERVER['REMOTE_ADDR']; $london = '10.44.'; $office = strpos($ip, $london); $blackpool_path = 'path'; $london_path = 'path' ?> And put this where the link would go: <a href="<?php if ($office === false) { echo $blackpool_path; } else { echo $london_path; }; ?>installer.exe">Link name</a> Many thanks for your help Dave Quote Link to comment https://forums.phpfreaks.com/topic/255874-problem-with-if-ip-equals-wildcard/#findComment-1312621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.