This question already has answers here:
PHP how to get local IP of system
(21 answers)
How do I get the external IP of my server using PHP?
(12 answers)
Closed 3 years ago.
I need to display the address of the server by which it can be identified in LAN.
I tried using
echo $_SERVER["SERVER_ADDR"];
but this just returns 127.0.0.1 instead of 192.168.xx.xx.
How do i get the second one without the access to WWW?
Hope this will help,
function getLanIP(){
exec("ipconfig /all", $output);
foreach($output as $line){
if (preg_match("/(.*)IPv4 Address(.*)/", $line)){
$ip = $line;
$ip = str_replace("IPv4 Address. . . . . . . . . . . :","",$ip);
$ip = str_replace("(Preferred)","",$ip);
}
}
return $ip;
}
OR something like,
$ip = getLanIP();
echo $ip;
<?php
function getLocalIp(){
return gethostbyname(trim(`hostname`));
}
echo getLocalIp();
?>
Solution was simpler than I thought it would be.
Just use:
exec ("hostname -I", $ip);
echo $ip[0];
I am trying to working with geoip to display Ips and their Countries . I am trying to display this code but nothing comes up?
$country = geoip_country_name_by_name($ip);
You must have the GeoIP functions installed first. If they are installed, then it could be possible, that the IP you're providing to the function does not exist in the database.
Try this code:
<?php
$country = geoip_country_name_by_name($ip);
if ($country) {
echo 'This host is located in: ' . $country;
} else {
echo 'Cannot find the IP in the database.'
}
-- Please scroll down to where I marked the PHP --
To explain in better detail.
I made a Leaflet map and in that map I want to load my own location.
Here's my code for that in Javascript, but this is out of question like #Pocketsand and I already discussed. So then scroll down to the PHP code and see if you can get the IP address through the browser.
$part_content = "<div id=\"mapid\"></div>";
//.setView([".$longitude.", ".$latitude."], ".$zoom_factor.");
$part_content .= "<script>
var map = L.map('mapid').fitWorld();
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © OpenStreetMap contributors, ' +
'CC-BY-SA, ' +
'Imagery © Mapbox',
id: 'mapbox.streets'
}).addTo(map);
function onLocationFound(e) {
var radius = e.accuracy / 2;
L.marker(e.latlng).addTo(map)
.bindPopup(\"You are within \" + radius + \" meters from this point\").openPopup();
L.circle(e.latlng, radius).addTo(map);
}
function onLocationError(e) {
alert(e.message);
}
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({setView: true, maxZoom: 16});
</script>";
It's the same code as in this maps source code.
When I go to Firefox, it partly works and on someone else's computer it works fine, when on my computer I get the error from the image I showed you.
So I can't locate on my computer to my own location, as in google maps it works perfectly and my extensions also don't seem to block that part.
PHP:
Basically this:
$ip = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
Gives me a random IP, because of HTTP_X_FORWARDED_FOR and REMOTE_ADDR gives me the correct IP, but not when I use this from a different IP address then the local one... thats why I check if the proxy is not empty.
This is the full php code for the tracker:
$ip = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$url = "http://freegeoip.net/json/$ip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
$location = json_decode($data);
$longitude = $location->longitude;
$latitude = $location->latitude;
$longitude = str_replace(",", ".", $longitude);
$latitude = str_replace(",", ".", $latitude);
}
My current problem is:
$ip = $_SERVER['HTTP_CLIENT_IP'] ? $_SERVER['HTTP_CLIENT_IP'] : ($_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
Which gets the correct IP address, but it almost seems like it gets the IP hosts location. Which is not my intention, I intend to get the clients location through the IP address, which currently isn't working.
I tried and tried, but couldn't seem to figure this out and hope you guys know more about this.
Thanks in advance!
I am guessing you didn't visit the URL in the error message? It's a Chrome security thing, try running off a local webserver or getting a https certificate on your remote server.
The Chrome Security team and I propose that, for new and particularly
powerful web platform features, browser vendors tend to prefer to make
the the feature available only to secure origins by default.
[...]
Definitions:
“Secure origins” are origins that match at least one of the following
(scheme, host, port) patterns:
(https, *, *)
(wss, *, *)
(*, localhost, ) (, 127/8, *)
(*, ::1/128, *)
(file, *, —)
(chrome-extension, *, —)
This list may be incomplete, and may need to be changed.
Source: https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features
To get a client public ip address, in PHP 5.3 or greater use:
<?php
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
?>
And if that is not working, use an external provider like https://geolocation-db.com
A JSON-P callback example:
<?php
$jsonp = file_get_contents('https://geolocation-db.com/jsonp');
$data = jsonp_decode($jsonp);
print $data->IPv4 . '<br>';
print $data->country_code . '<br>';
print $data->country_name . '<br>';
print $data->state . '<br>';
print $data->city . '<br>';
print $data->postal . '<br>';
print $data->latitude . '<br>';
print $data->longitude . '<br>';
// Strip callback function name and parenthesis
function jsonp_decode($jsonp) {
if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
return json_decode(trim($jsonp,'();'));
}
?>
And a JSON example:
<?php
$json = file_get_contents('https://geolocation-db.com/json');
$data = json_decode($json);
print $data->country_code . '<br>';
print $data->country_name . '<br>';
print $data->state . '<br>';
print $data->city . '<br>';
print $data->postal . '<br>';
print $data->latitude . '<br>';
print $data->longitude . '<br>';
print $data->IPv4 . '<br>';
?>
Instead of going with a PHP function I did a leaflet function instead, which tracks the user through the browser.
Using the library: leaflet.locate
Where then I could use the:
// create control and add to map
var lc = L.control.locate().addTo(map);
// request location update and set location
lc.start();
But this isn't the answer to the PHP part of my code, even though this leaflet function also works for now.
Sadly the PHP part can't find the exact address through the IP if the server is at another place, but the leaflet way of doing it through the browser does work. Hope you guys find use in these answers.
I'm writing a simple form to check if a .com domain is available. I'm evaluating 3 methods (#dns_get_record, gethostbyname() and checkdnsrr()) for a bunch of test domain names with the below code, but I am getting all not available entries for gethostbyname() and checkdnsrr(), which you can see # http://suggestmyname.com/nonwp/arraypush.php if you refresh a few times. I don't get this issue when running on my local testing server, anyone know why the inconsistency? Also not all of the domains are being checked on my remote server, but are on my local testing server.
$sdomains = array();
array_push($sdomains, 'etc.com');
function domainAvailable1($domain){
$results = #dns_get_record($domain, DNS_ANY);
return empty($results);
}
function domainAvailable2($domain){
return gethostbyname($domain) == $domain;
}
function domainAvailable3($domain){
return !checkdnsrr($domain, 'ANY');
}
echo '<table><tr><td>#dns_get_record</td><td>gethostbyname</td><td>checkdnsrr</td></tr>';
foreach($sdomains as $sdomain){
if (domainAvailable1($sdomain) == true){
echo '<tr><td bgcolor=green>' . $sdomain . ' is available!</td>';
} else {
echo '<tr><td bgcolor=red>' . $sdomain . ' is NOT available.</td>';
}
if (domainAvailable2($sdomain) == true){
echo '<td bgcolor=green>' . $sdomain . ' is available!</td>';
} else {
echo '<td bgcolor=red>' . $sdomain . ' is NOT available.</td>';
}
if (domainAvailable3($sdomain) == true){
echo '<td bgcolor=green>' . $sdomain . ' is available!</td></tr>';
} else {
echo '<td bgcolor=red>' . $sdomain . ' is NOT available.</td></tr>';
}
}
I'm not sure that using any of those functions (basically using DNS in general) is a reliable method to determine if a domain is available.
I have plenty of domains registered that have absolutely no DNS records so they can't be resolved by any means, but the domains are of course taken.
While most domains will have some DNS records, there will be cases where there aren't any. For example the domain mymoney.com in your script says it is available through all 3 checks, but the domain is registered.
Your best bet is to use WHOIS to query the appropriate registrar for a given TLD and use WHOIS to determine availability. I think if you try to use DNS, there will be false positives or other problems you will run into.
I am using the code below (simplified version) to determine if my IPs are on a blacklist. I need to modify it to be able to determine if an IP is on a Whitelist. The function will require me to see a specific code returned.
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
How can this be adjusted to return the (code) output value when the script runs?
$host = '222.22.222.222';
$rbl = 'hostkarma.junkemailfilter.com';
$rev = array_reverse(explode('.', $host));
$lookup = implode('.', $rev) . '.' . $rbl;
if ($lookup != gethostbyname($lookup)) {
echo "ip: $host is listed in $rbl\n";
} else {
echo "ip: $host NOT listed in $rbl\n";
}
EDIT: Sorry guys, The function of the script above will return confirmation if the IP address is on the blacklist entered in $rlb. However, Hostkarma returns a code, one of the 127.0 codes shown above as each code indicates a different block status. I need to get the code. "echo $lookup;" just returns the reverse lookup, like this: 222.222.22.222.hostkarma.junkemailfilter.com
$lookup = implode('.', $rev) . '.' . $rbl;
$value = gethostbyname($lookup);
if ($lookup != $value){
echo "ip: $host is listed in $rbl\n";
echo "return value: $value\n";
}
else{
echo "ip: $host NOT listed in $rbl\n";
}
The 127.x.x.x code should be given to you as the value returned by gethostbyname.
Do you mean this?
echo $lookup;