March 21, 201412 yr I'm looking to detect the location of a website visitor (presumably by IP), and then whatever their detected country is, I want this to then be the selected option within a <select> dropdown when specifying their Country of Residence. Save them having to choose it from the long list of country options! See visual below. Does anyone have any ideas/recommendations please on any plugins that would enable this?
March 21, 201412 yr Here is a small example <?php $countrys = array( "GB"=>"England", "US"=>"USA" ); $user = file_get_contents("http://api.hostip.info/country.php?ip=".$_SERVER['REMOTE_ADDR']); // Returns GB/US etc ... $html = '<select>'; foreach ($countrys as $key=>$val) { $selected = (($key == $user)?'selected':''); $html .= '<option value="'.$key.'" '.$selected.'>'.$val.'</option>'; } $html .= '</select>'; ?>
Create an account or sign in to comment