Check ip address of host or website with PHP function and HTML form

Posted by Shek on May 26, 2008 under PHP | Be the First to Comment | Total View: 9,706 views

Editor’s note: There is a function in PHP to check the IP address of a host. It is called “gethostbyname()“. We could use it to check the IP address of a website. Steps are very simple: Put the website address into the function, for example: gethostbyname(www.phpsuperblog.com); and print the result out and then the IP address of the website would appear.

I have create a HTML file called “checkipform.html” with a HTML form in it:

For the HTML form, it has a text field and a submit button, the text field is for the user to type in the URL of the website for checking the IP address. After the submit button being pressed, it will direct us to a php file named “showip.php”:

1
2
3
4
5
6
7
<html>
<form name ="aform" action="showip.php" method="post">
Enter the website you like to check the IP address:
<input name="address" type="text" />
<input name="Submit" type="submit" value="check IP" />
</form>
</html>

For “showip.php”:

First we create a PHP variable to store the website address which the user input into the HTML form:

$website = $_POST['address'];

Then we run the PHP function gethostbyname() to get the IP address. We also create another PHP variable to store the result:

$ip = gethostbyname($website);

Now we print out the result to the screen:

echo "The IP of $website is $ip";

All together we have for “showip.php”:

1
2
3
4
5
<?php
$website = $_POST['address'];
$ip = gethostbyname($website);
echo "The IP of $website is $ip";
?>

Not what you want? Try a Search

Add A Comment

*