Skip to content Skip to sidebar Skip to footer

How To Check Whether A Url Is External Url Or Internal Url With Php?

I'm getting all ahrefs of a page with this loop: foreach($html->find('a[href!='#']') as $ahref) { $ahrefs++; } I want to do something like this: foreach($html->find('a[h

Solution 1:

Use parse_url and compare host to your local host (often but not always it's the same as $_SERVER['HTTP_HOST'])

functionisexternal($url) {
  $components = parse_url($url);    
  return !empty($components['host']) && strcasecmp($components['host'], 'example.com'); // empty host will indicate url like '/relative.php'
}

Hovewer this will treat www.example.com and example.com as different hosts. If you want all your subdomains to be treated as local links then the function will be somewhat larger:

functionisexternal($url) {
  $components = parse_url($url);
  if ( empty($components['host']) ) returnfalse;  // we will treat url like '/relative.php' as relativeif ( strcasecmp($components['host'], 'example.com') === 0 ) returnfalse; // url host looks exactly like the local hostreturn strrpos(strtolower($components['host']), '.example.com') !== strlen($components['host']) - strlen('.example.com'); // check if the url host is a subdomain
}

Solution 2:

This is how you can simply detect external URLs:

$url    = 'https://my-domain.com/demo/';
$domain = 'my-domain.com';

$internal = (
    false !== stripos( $url, '//' . $domain ) || // include "//my-domain.com"and"http://my-domain.com"
    stripos( $url, '.' . $domain ) ||            // include subdomains, like "www.my-domain.com". DANGEROUS (see below)!
    (
        0 !== strpos( $url, '//' ) &&            // exclude protocol relative URLs, like "//example.com"0 === strpos( $url, '/' )                // include root-relative URLs, like "/demo"
    )
);

The above check will treat www.my-domain.com and my-domain.com as being "internal".

Why this rule is dangerous:

The subdomain logic introduces a weakness that could be exploited: When an external URL contains your domain inside the path, for example, https://external.com/www.my-domain.com is treated as internal!

More secure code:

This problem can be eliminated by removing subdomain support (which I suggest to do):

$url    = 'https://my-domain.com/demo/';
$domain = 'my-domain.com';

$internal = (
    false !== stripos( $url, '//' . $domain ) || // include "//my-domain.com"and"http://my-domain.com"
    (
        0 !== strpos( $url, '//' ) &&            // exclude protocol relative URLs, like "//example.com"0 === strpos( $url, '/' )                // include root-relative URLs, like "/demo"
    )
);

Solution 3:

functionisexternal($url) {
    // FOO...// Test if link is internal/externalif(strpos($url,'domainname.com') !== false || strpos($url,"/") === '0') 
    {
         returntrue;
    }
    else 
    {
         returnfalse;
    }
}

Solution 4:

I know this post is old but here my function i coded right now. Maybe some other need it too.

functionIsResourceLocal($url){
    if( empty( $url ) ){ returnfalse; }
    $urlParsed = parse_url( $url );
    $host = $urlParsed['host'];
    if( empty( $host ) ){ 
    /* maybe we have a relative link like: /wp-content/uploads/image.jpg *//* add absolute path to begin and check if file exists */$doc_root = $_SERVER['DOCUMENT_ROOT'];
    $maybefile = $doc_root.$url;
    /* Check if file exists */$fileexists = file_exists ( $maybefile );
    if( $fileexists ){
        /* maybe you want to convert to full url? */returntrue;        
        }
     }
    /* strip www. if exists */$host = str_replace('www.','',$host);
    $thishost = $_SERVER['HTTP_HOST'];
    /* strip www. if exists */$thishost = str_replace('www.','',$thishost);
    if( $host == $thishost ){
        returntrue;
        }
    returnfalse;
}

Solution 5:

You probably want to check if the link is in the same domain. That will only work though if all your href attributes are absolute and contain the domain. Relative ones like /test/file.html are tricky because one can have folders that have the same name as domains.. So, if you have full url's in each link:

functionisexternal($url) {

  // Test if link is internal/externalif(stristr($url, "myDomain.com") || strpos($url,"/") == '0')
    returntrue;
  elsereturnfalse;
}

Post a Comment for "How To Check Whether A Url Is External Url Or Internal Url With Php?"