Php system ip address

gethostbyname

Returns the IPv4 address of the Internet host specified by hostname .

Parameters

Return Values

Returns the IPv4 address or a string containing the unmodified hostname on failure.

Examples

Example #1 A simple gethostbyname() example

$ip = gethostbyname ( ‘www.example.com’ );

See Also

  • gethostbyaddr() — Get the Internet host name corresponding to a given IP address
  • gethostbynamel() — Get a list of IPv4 addresses corresponding to a given Internet host name
  • inet_pton() — Converts a human readable IP address to its packed in_addr representation
  • inet_ntop() — Converts a packed internet address to a human readable representation

User Contributed Notes 30 notes

If you do a gethostbyname() and there is no trailing dot after a domainname that does not resolve, this domainname will ultimately be appended to the server-FQDN by nslookup.

So if you do a lookup for nonexistentdomainname.be your server may return the ip for nonexistentdomainname.be.yourhostname.com, which is the server-ip.

To avoid this behaviour, just add a trailing dot to the domainname; i.e. gethostbyname(‘nonexistentdomainname.be.’)

This function says «Returns the IPv4 address or a string containing the unmodified hostname on failure.

This isn’t entirely true, any hostname with a null byte in it will only return the characters BEFORE the null byte.

$hostname = «foo\0bar» ;
var_dump ( $hostname );
var_dump ( gethostbyname ( $hostname ));
?>

Results:
string ‘foo�bar’ (length=7)
string ‘foo’ (length=3)

Important note: You should avoid its use in production.

DNS Resolution may take from 0.5 to 4 seconds, and during this time your script is NOT being executed.

Your customers may think that the server is slow, but actually it is just waiting for the DNS resolution response.

You can use it, but if you want performance, you should avoid it, or schedule it to some CRON script.

Options for the underlying resolver functions can be supplied by using the RES_OPTIONS environmental variable. (at least under Linux, see man resolv.conf)

Set timeout and retries to 1 to have a maximum execution time of 1 second for the DNS lookup:
putenv ( ‘RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1’ );
gethostbyname ( $something );
?>

You should also use fully qualified domain names ending in a dot. This prevents the resolver from walking though all search domains and retrying the domain with the search domain appended.

For doing basic RBL (Real Time Blacklist) lookups with this function do:

$host = ‘64.53.200.156’ ;
$rbl = ‘sbl-xbl.spamhaus.org’ ;
// valid query format is: 156.200.53.64.sbl-xbl.spamhaus.org
$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» ;
>
?>

Tomas V.V.Cox

gethostbyname and gethostbynamel does not ask for AAAA records. I have written two functions to implement this. gethostbyname6 and gethostbynamel6. I don’t believe this issue has been addressed yet.

They are made to replace gethostbyname[l], in a way that if $try_a is true, if it fails to get AAAA records it will fall back on trying to get A records.

Feel free to correct any errors, I realise that it is asking for *both* A and AAAA records, so this means two DNS calls.. probably would be more efficient if it checked $try_a before making the query, but this works for me so I’ll leave that up to someone else to implement in their own work.. the tip is out there now anyway..

function gethostbyname6($host, $try_a = false) // get AAAA record for $host
// if $try_a is true, if AAAA fails, it tries for A
// the first match found is returned
// otherwise returns false

function gethostbynamel6($host, $try_a = false) // get AAAA records for $host,
// if $try_a is true, if AAAA fails, it tries for A
// results are returned in an array of ips found matching type
// otherwise returns false

$dns6 = dns_get_record($host, DNS_AAAA);
if ($try_a == true) $dns4 = dns_get_record($host, DNS_A);
$dns = array_merge($dns4, $dns6);
>
else < $dns = $dns6; >
$ip6 = array();
$ip4 = array();
foreach ($dns as $record) if ($record[«type»] == «A») $ip4[] = $record[«ip»];
>
if ($record[«type»] == «AAAA») $ip6[] = $record[«ipv6»];
>
>
if (count($ip6) < 1) if ($try_a == true) if (count($ip4) < 1) return false;
>
else return $ip4;
>
>
else return false;
>
>
else return $ip6;
>
>

When using gethostbynamel() and gethostbyname() together, you must do gethostbynamel() first, otherwise it will always give you one IP (or none) from the cache, and not return the full lookup.

On a side-note, PHP (5.0.4, but probably other versions too) can cache gethostbyname information.

In short, once PHP looks up an address, it may not actually perform another lookup as you may expect. In my particular case (I think) the problem was a change to resolv.conf didn’t take effect inside PHP (although nslookup/ping etc worked fine). Stop/Starting Apache fixed it (although a simple ‘restart’ (kill -HUP) didn’t).

In short, if you change resolv.conf, stop and restart Apache.

In PHP4 you can use gethostbyname() but I have found this unreliable when doing lookups on entries that return A records on the private network. PHP5 has a much better routine — dns_get_record(). If you are stuck with PHP4 or don’t want to upgrade you can use dig:

Источник

How to Get Local IP Address of System using PHP ?

IP (Internet Protocol) Address is an address of your network hardware. It helps in connecting your computer to other devices on your network and all over the world. An IP Address is made up of numbers or characters combined by ‘.’ symbol.

Example: It is an example of IP address.

All devices that are connected to an internet connection have a unique IP address which literally gives the idea that a large number of IP addresses had to be created for every system.

Local IP address: It is the IP address of your computer system which is meant to be kept private and thus is also known as private IP addresses. This article focuses on how to extract the IP address. A private IP address is the address of your device connected to the home or business network. If you have a few different devices connected to one ISP (Internet Service Provider), then all your devices will have a unique private IP address. This IP address cannot be accessed from devices outside your home or business network. Private IP addresses are not unique because there is a limited number of devices on your network. There are some other types of IP addresses that exist like, public IP addresses, static IP addresses and dynamic IP addresses that follow the same format for representation.

IPv4 address: The IPv4 version used to configure IP addresses in numerical value and it uses the hexadecimal number system to complete its job i.e. what makes it possible to get millions of IP, different for every system.

Example: In this example, we are trying to get the local IP address without using terminal and its commands. Instead, we are using a PHP program to get the same job done. We will be required two methods that are mentioned below:

  • getHostByName() Function: It gets the IPv4 address corresponding to a given Internet host name.
  • getHostName() Function: It gets the standard host name for the local machine.

Источник

How to Get Current System IP Address in php

To get the current system IP address in php, we can access the ‘REMOTE_ADDR’ key from the $_SERVER super global variable.

$ip_address = $_SERVER['REMOTE_ADDR'];

If the client is using a proxy or accessing your page with shared internet, you use the ‘HTTP_X_FORWARDED_FOR’ and ‘HTTP_CLIENT_IP’ keys respectively.

$ip_address_proxy = $_SERVER['HTTP_X_FORWARDED_FOR']; $ip_address_shared_internet = $_SERVER['HTTP_CLIENT_IP'];

If you are looking to get the IP address of a particular website, you can do that with the php gethostbyname() function.

$ip_address = gethostbyname("www.google.com");

When designing web pages, many times we need to get the IP address of a visitor for various reasons (logging, targeting, redirecting, etc.)

To get the IP address of a user, we can use the $_SERVER super global variable.

The $_SERVER super global variable has many different values which can be accessed in our program.

To access the IP address of the user, we read the ‘REMOTE_ADDR’ field from $_SERVER.

Below is a simple example of how to retrieve the IP address from a user.

$ip_address = $_SERVER['REMOTE_ADDR'];

How to Get the IP Address of a User Behind a Proxy in php

If the user is using a proxy and accessing your website, then you will need to use a different key to get the IP Address.

To get the IP address of a client using a proxy, you should access the ‘HTTP_X_FORWARDED_FOR’ key in the $_SERVER super global variable.

$ip_address_proxy = $_SERVER['HTTP_X_FORWARDED_FOR'];

If the user is using shared internet, then it’s possible that you will have to check the ‘HTTP_CLIENT_IP’ key in the $_SERVER variable.

$ip_address_shared_internet = $_SERVER['HTTP_CLIENT_IP'];

A full function which will check for all of these cases is below.

function getIPAddress() < if(!empty($_SERVER['HTTP_CLIENT_IP']))< $ip_address = $_SERVER['HTTP_CLIENT_IP']; >elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) < $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; >else < $ip_address = $_SERVER['REMOTE_ADDR']; >return $ip; >

How to Get the IP Address of a Website in php

Every website has an IP address. If we need to get the IP address of a particular website, then we can use the php gethostbyname() function.

Pass the URL of the website to gethostbyname() and you will get the IP address of that website.

Below is an example of getting the IP address of www.google.com in php.

$ip_address = gethostbyname("www.google.com");

Hopefully this article has been useful for you to learn how to get IP addresses using php.

  • 1. php array_pop() – Remove Last Element from Array
  • 2. php array_key_exists() Function – Check if Key Exists in Array
  • 3. php is_numeric – Check if Variable is a Number
  • 4. php Rename File – Renaming Files and Directories Easily
  • 5. Get Days in Month Using php
  • 6. php array_slice() – Get Slice of Elements from Array
  • 7. php array_filter() – Filter Array Elements with Function in php
  • 8. php sin – Find Sine of Number in Radians Using php sin() Function
  • 9. Check if String Starts with Given String in php with str_starts_with()
  • 10. php array_combine() – Create New Array Given Arrays of Keys and Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Источник

Php system ip address

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Читайте также:  Java if statements with integers
Оцените статью