How To Check Whether A URL Is External URL or Internal URL With PHP?

check if url is external javascript
php get all url parameters
php parse url parameters
extract data from url php
php check if url is valid
check if url exists online
php get url from string
php check if url exists

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[href!="#"]') as $ahref) {
    if(isexternal($ahref)) {
        $external++;
    }
    $ahrefs++;
}

Where isexternal is a function

function isexternal($url) {
    // FOO...

    // Test if link is internal/external
    if(/*condition is true*/) {
        return true;
    }
    else {
        return false;
    }
}

Help!


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

function isexternal($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:

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

How To Check Whether A URL Is External URL or Internal URL With , This below code takes a URL as a string, then two different class names as strings and compares the URL to the host (I also commented out a WordPress  The website's internal and external links analyzer visits the given URL and analyzes which anchor links does the webpage contain, and then it breakdown the links into external and internal links. The external links are those links which are of a separate website or domain, and the internal links are website's own links which the entered URL contains in its HTML source.


function isexternal($url) {
    // FOO...

    // Test if link is internal/external
    if(strpos($url,'domainname.com') !== false || strpos($url,"/") === '0') 
    {
         return true;
    }
    else 
    {
         return false;
    }
}

PHP - Determine if a URL is an internal or external URL, A simple HTTP PHP class to crawl a URL for internal and external URLs check for error when loading url; text starting with "file_get_contents"  How to check the existence of URL in PHP? Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn’t exist.


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

function IsResourceLocal($url){
    if( empty( $url ) ){ return false; }
    $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? */
        return true;        
        }
     }
    /* 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 ){
        return true;
        }
    return false;
}

A simple HTTP PHP class to crawl a URL for internal and external , This function is intended specifically for the purpose of parsing URLs and not URIs. However Test if URL is already absolute (contains host, or begins with '/') 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.


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"
    )
);

parse_url - Manual, Just enter a url and check which links a webpage contains. This tool will analyze an entered domain/url for internal and external anchor links and checks if  Use the Shell to find the internal and external URLs for the ECP virtual directory. This example returns the ECP virtual directory name, internal URL, and external URL in a formatted list. Get-ECPVirtualDirectory | Format-List Name,InternalURL,ExternalURL


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:

function isexternal($url) {

  // Test if link is internal/external
  if(stristr($url, "myDomain.com") || strpos($url,"/") == '0')
    return true;
  else
    return false;
}

Analyze Internal & External Links of a Webpage, On the other hand, the number of external links is important for your SEO ranking. You can check both the external and internal links in your pages through a  Name Modifiers Type Description Overrides; DependencySerializationTrait:: $_serviceIds protected : property : An array of service IDs keyed by property name used for serialization.


How to check the number of internal/external links on my website?, $path: (optional) The internal path or external URL being linked to, such as to the site, $options['language'] is used to look up the alias for the URL. Defaults to an empty string, as most modern web servers automatically find 'index.php'. If  Step 3: Update all internal and external links to HTTPS: Ensure all the internal and external links point to the new HTTPS URLs. Internal links include navigation/menu links, images, CSS references etc. If internally the website still references HTTP files, it will break.


url | common.inc | Drupal 7.x, If a path cannot be assessed by Drupal's menu handler, then we must treat it as potentially insecure. Parameters. string $path: The internal path or external URL​  In this tutorial, you will learn how to integrate the WordPress authentication functions in an external PHP application. PHP Files Location Since WordPress uses domain-based cookies to keep users logged in, it is important that your PHP files are hosted on the same domain.


UrlHelper::isExternal | UrlHelper.php | Drupal 8.2.x, This is what fixed it: if (!drupal_valid_path($path)) { // Not a system URL. if (!​drupal_lookup_path('source', $path)) { // Not a path alias. // $path does not exist. } }. PHP program to get complete URL of currently running pages. There are few steps to get the complete URL of the currently running page which are given below: Create a PHP variable which will store the URL in string format. Check whether the HTTPS is enabled by the server .If it is, append “https” to the URL string.