I wish to extract the exact hostname from an ip address but what i am receiving is an entire string of the address.
I would just like to extract only the Host Name from the the string. Can any one point me in the right direction ?
Here is the snippet of the code being used:-
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$fullhost = gethostbyname(gethostbyaddr($ip));
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
?>
Host: <?=$host?>
The output received from it is :-
Host: *.36.64.182.airtelbroadband.in
I would just like to display Airtel Broadband and nothing else to the user
You can try this:
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']); // gets the full hostname
$hostNames = explode(".", $host); // explodes into parts divided by DOT
echo $hostNames[count($hostNames)-2]; // take what you need
// $hostNames[count($hostNames)-1]; -> in
// $hostNames[count($hostNames)-2]; -> airtelbroadband
You might also want to look into geoip.
This is a basic PHP extension that can be installed using PECL.
string geoip_isp_by_name ( string $hostname )
The geoip_isp_by_name() function will return the name of the Internet Service Provider (ISP) that an IP is assigned to.
This function is currently only available to users who have bought a commercial GeoIP ISP Edition. A warning will be issued if the proper database cannot be located.
Consider this simple example:
<?php
$subject = 'Host: *.36.64.182.airtelbroadband.in';
$pattern = '/^Host:\s+((\*|\d+)\.){4}(.+)\..+$/';
preg_match($pattern, $subject, $tokens);
var_dump($tokens[3]);
The output obviously is:
string(15) "airtelbroadband"
That is all the information you can rip from your input. If you really want to somehow show the "publicly known company names" (as opposed to the technical network names), then you need to use some form of catalog. It is very hard to somehow retrieve such information directly from the network. You could give the whois database a try. But parsing the output of that will be a really tough task...
Related
I am building my log system, for my software in php.
Data collection via: https://ipinfo.io/
As shown in the screenshot, just make a json_decode to read them.
Only problem for the privacy object that I can't show:
example working with for example the city parameter:
//Gets the IP Address from the visitor
$PublicIP = $_SERVER['REMOTE_ADDR'];
//Uses ipinfo.io to get the location of the IP Address, you can use another site but it will probably have a different implementation
$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
//Breaks down the JSON object into an array
$json = json_decode($json, true);
$city = $json['city'];
echo $city;
instead when I have to go into privacy, it doesn't give me anything back, what am I doing wrong?
$PublicIP = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
$json = json_decode($json, true);
$vpn = $json["privacy"]["vpn"];
echo $vpn
If vpn is false, then you won't see anything because false shows up as blank when echoed.
Try something like echo ($vpn == true ? "Yes" : "No"); instead. Or use var_dump($vpn);
See also PHP - Get bool to echo false when false.
There are two issues at least:
Privacy data is only available if you have a token (i.e. you signed up), and that too only if you purchased the privacy data - see their pricing page and the addons page. Then you'll have to include the token in your request.
You are requesting for /geo, which will only give back geographical data. Don't put that suffix in the request URL.
I suggest you carefully read https://ipinfo.io/developers and https://ipinfo.io/developers/data-types#privacy-data in particular for your use case.
I'm using geoplugin php to get the country and ip of visitors on my site, but its returning the ip and country of hosting server instead of user who visit the site, I'm using Hostinger as my hosting.
$ipdat = #json_decode(file_get_contents("http://www.geoplugin.net/json.gp"));
$country = $ipdat->geoplugin_countryName;
$ip = $ipdat->geoplugin_request;
its working fine on xampp localhost.
Your code behavior is an expected behavior since the GEO plugin site gets the IP address from where the script request came and gives details accordingly. If you want user/visitor's data, then you will need to make use of user's remote IP stored in $_SERVER global variable and pass this IP to GEO plugin to get the details as mentioned in their docs.
Snippet:
<?php
$data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$country = $data['geoplugin_countryName'];
$ip = $data['geoplugin_request'];
echo $country," ",$ip,"<br/>";
print_r($data);
I am using software called gns3 to build networks.
I have the need to find out devices external ip from within an gns3 internal network. This would be like a pc behind a router doing NAT.
Please could someone tell me if there is a way that you can echo out the clients external ip in php so I can add a web server to one of my virtual networks within gns3 so I can visit it on some devices and find their external ip’s for testing?
Thanks in advance
You need to make a request to an external website that could tell you the ip address you have. You may use checkip.dyndns.org - click the link to see your own ip addres.
if you want to do that from php here is an example code:
<?php
//get website content as a string
$ipCheckUrl = 'http://checkip.dyndns.org';
$subject = file_get_contents($ipCheckUrl);
//extract from string just ip address
$pattern = '/Current IP Address\:\s*(\d+\.\d+\.\d+\.\d+)/U';
$ip = preg_match($pattern,$subject,$result) ? $result[1] : 'Error';
//print ip address.
echo $ip;
Can I search ALL emails over IMAP without downloading the message(s)?
As specified in 6.4.4 of RFC 3501 IMAP version 4 revision 1 (IMAP4rev1):
The SEARCH command searches the mailbox for messages that match
the given searching criteria. Searching criteria consist of one
or more search keys. The untagged SEARCH response from the server
contains a listing of message sequence numbers corresponding to
those messages that match the searching criteria.
The defined search keys are as follows. Refer to the Formal
Syntax section for the precise syntactic definitions of the
arguments
BODY
Messages that contain the specified string in the body of the
message.
...so I wonder if I can search inside the body of the email without downloading it first?
The function imap_search() could help you but have in mind that it needs to be supported by the IMAP server to work.
UPDATE:
This is the small program:
error_reporting(E_ALL);
ini_set('display_errors', '1');
$user = ''; // put your Gmail email address here (including '#gmail.com');
$pass = ''; // put your Gmail password here
$host = 'imap.gmail.com:993'; // Put your IMAP server here with portGmail
$keyword = ''; // put the word you want to find
$mailbox = sprintf('{%s/imap/ssl/user=%s}INBOX', $host, $user);
$query = sprintf('BODY "%s"', $keyword);
$mbox = imap_open($mailbox, $user, $pass, OP_READONLY);
if ($mbox) {
$list = imap_search($mbox, $query, SE_UID);
var_dump($list);
imap_close($mbox);
}
It may or may not work with your setup. It worked for me with one account on our company mail server. It failed to connect on another server that works fine with my regular email client.
It worked and failed on the same time (!!) with Gmail. Don't ask!
I am not a php expert. I develop android apps. In my app i am getting the user's ip address from this url http://ip2country.sourceforge.net/ip2c.php?format=JSON. As you can see when some open this url it returns some info including the IP address. I only want to get the IP and (if possible) country. Most of the time this url is busy and doesn't returns ip and gives max active user connections error. Can you please give me any php file which i can put in my own webhost and call the url to get ip. The data returned should be in json so i can parse it easily.
Thanks
<?php
$json = file_get_contents("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
//this $json will have the response that the website sends.
echo json_encode($json);
?>
You can have this object wherever you call this php file and do the needful
Run this php file to check the output
Another way: EDIT
<?php
$visitor_ip = $_SERVER['REMOTE_ADDR'];
echo $visitor_ip;
$data1 = file_get_contents("http://api.hostip.info/?ip=$visitor_ip");
echo "<br> $data1";
?>
You can use PHP to get the users IP address via $_SERVER['REMOTE_ADDR']. You can then use an IP to Location lookup website to translate that into a country and merge the results:
$ip = $_SERVER['REMOTE_ADDR'];
$loc = json_decode(file_get_contents('http://ipinfo.io/'.$ip.'/json'), true);
$country = isset($loc['country']) ? $loc['country'] : 'Unknown';
$result = array('ip'=>$ip, 'country'=>$country);
header('Content-Type: application/json');
echo json_encode($result);
You get the IP address in PHP you have to use $_SERVER['REMOTE_ADDR'] then use http://ipinfo.io to pass that IP to this website. You can get the Location through JSON data, just follow the first answer on this question Getting the location from an IP address.
I have successfully implemented it by following the answer.