You are here

11 October 2011

Check if URL is external or internal in PHP. We are going to use parse_url PHP native function. Function parse_url parses a URL and returns an associative array containing any of the various components of the URL that are present.

Source code viewer
  1. $url_host = parse_url($external_url, PHP_URL_HOST);
  2. $base_url_host = parse_url($internal_url, PHP_URL_HOST);
  3.  
  4. if($url_host == $base_url_host || empty($url_host))
  5. {
  6. // internal link ...
  7. }
  8. else
  9. {
  10. // external link ...
  11. }
Programming Language: PHP