phpparsingparse-url

parse_url() returns an error when example.com is passed


According to following code if $host_name is something like example.com PHP returns a notice: Message: Undefined index: host but on full URLs like http://example.com PHP returns example.com. I tried if statements with FALSE and NULL but didn't work.

$host_name = $this->input->post('host_name');
$parse = parse_url($host_name);
$parse_url = $parse['host'];

How can I modify the script to accept example.com and return it?


Solution

    1. Upgrade your php. 5.4.7 Fixed host recognition when scheme is ommitted and a leading component separator is present.

    2. Add scheme manually: if(mb_substr($host_name, 0, 4) !== 'http') $host_name = 'http://' . $host_name;