tags:

views:

54

answers:

4

Well, very simple question. So that's good news for you, I guess.

More thorough explanation:

I have a PHP script allowing me to add urls to a database. I want the script to resolve the url to an IP address as well, to store that, too. However, some of the URLs are like this:

http://111.111.111.111/~example/index.php

So it also needs to work with that.

I'm not SURE that this is possible, but it only makes sense it would be.

So: Is it possible, and if so, how?

+2  A: 

gethostbyname

echo gethostbyname('stackoverflow.com'); // 69.59.196.211

You can use parse_url to grab the host from the url:

$urlParts = parse_url('http://111.111.111.111/~example/index.php'); 

print_r($urlParts);

/*
Array
(
    [scheme] => http
    [host] => 111.111.111.111
    [path] => /~example/index.php
)
*/
webbiedave
Thanks, this helped a lot. Especially the parse_url()
Rob
You're welcome. Glad I could help.
webbiedave
A: 

Yes, gethostbyname

http://us.php.net/manual/en/function.gethostbyname.php

webdestroya
+1  A: 

gethostbyname()

$ip = gethostbyname('www.example.com');

It should be noted that if the website is on a shared hosting server, it is not guaranteed that your link will still be valid.

Tim Cooper
+1  A: 

gethostbynamel() - Get a list of IPv4 addresses corresponding to a given Internet host name.


Usage:

$ips = gethostbynamel('stackoverflow.com');

/*
Array
(
    [0] => 69.59.196.211
)
*/
echo '<pre>';
print_r($ips);
echo '</pre>';

$ips = gethostbynamel('google.com');

/*
Array
(
    [0] => 74.125.39.105
    [1] => 74.125.39.106
    [2] => 74.125.39.147
    [3] => 74.125.39.99
    [4] => 74.125.39.103
    [5] => 74.125.39.104
)
*/
echo '<pre>';
print_r($ips);
echo '</pre>';

To get the host out of an URL you can use this one liner:

// 111.111.111.111
echo parse_url('http://111.111.111.111/~example/index.php', PHP_URL_HOST);
Alix Axel
Why the downvote?
Alix Axel