4

Let's say I have a URL of the document linking to another document (which can be both absolute or relative) and I need to have this link in absolute.

I made the simple function providing this functionality for several common cases:

function absolute_url($url,$parent_url){
  $parent_url=parse_url($parent_url);
  if(strcmp(substr($url,0,7),'http://')==0){
    return $url;
  }
  elseif(strcmp(substr($url,0,1),'/')==0){
    return $parent_url['scheme']."://".$parent_url['host'].$url;
  }
  else{
    $path=$parent_url['path'];
    $path=substr($path,0,strrpos($path,'/'));
    return $parent_url['scheme']."://".$parent_url['host']."$path/".$url;
  }
}

$parent_url='http://example.com/path/to/file/name.php?abc=abc';
echo absolute_url('name2.php',$parent_url)."\n";
// output http://example.com/path/to/file/name2.php
echo absolute_url('/name2.php',$parent_url)."\n";
// output http://example.com/name2.php
echo absolute_url('http://name2.php',$parent_url)."\n";
// output http://name2.php

The code works fine, but there could be more cases such as ../../path/to/file.php which will not work.

So is there any standard classes or function doing this thing better (more universal) that my function?

I tried to Google it and check the similar questions (one and two) but it looks like server-path related solution which is not the thing I'm looking for.

Community
  • 1
  • 1
Vlada Katlinskaya
  • 991
  • 1
  • 10
  • 26
  • `..\..\path\to\file.php` is a file path, not a url, so would warrant a separate function. – Steve Oct 17 '14 at 11:22
  • @Steve so `file` will not work in HTML document? – Vlada Katlinskaya Oct 17 '14 at 11:34
  • It might, depending on the browser, but its not guaranteed. A uri (what you put in a link) should use forward slashes. Im not trying to be awkward, i genuinely thought you where talking about file paths, eg for use with phps include etc. – Steve Oct 17 '14 at 11:49
  • @Steve oh! You're right! I'm talking about URI. So I meant `../../path/to/file.php` – Vlada Katlinskaya Oct 17 '14 at 12:08
  • this is dublicate of http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php – qdinar Jan 03 '16 at 15:54

3 Answers3

2

This function will resolve relative URLs to a given current page url in $pgurl without regex. It successfully resolves:

/home.php?example types,

same-dir nextpage.php types,

../...../.../parentdir types,

full http://example.net urls,

and shorthand //example.net urls

//Current base URL (you can dynamically retrieve from $_SERVER)
$pgurl = 'http://example.com/scripts/php/absurl.php';

function absurl($url) {
 global $pgurl;
 if(strpos($url,'://')) return $url; //already absolute
 if(substr($url,0,2)=='//') return 'http:'.$url; //shorthand scheme
 if($url[0]=='/') return parse_url($pgurl,PHP_URL_SCHEME).'://'.parse_url($pgurl,PHP_URL_HOST).$url; //just add domain
 if(strpos($pgurl,'/',9)===false) $pgurl .= '/'; //add slash to domain if needed
 return substr($pgurl,0,strrpos($pgurl,'/')+1).$url; //for relative links, gets current directory and appends new filename
}

function nodots($path) { //Resolve dot dot slashes, no regex!
 $arr1 = explode('/',$path);
 $arr2 = array();
 foreach($arr1 as $seg) {
  switch($seg) {
   case '.':
    break;
   case '..':
    array_pop($arr2);
    break;
   case '...':
    array_pop($arr2); array_pop($arr2);
    break;
   case '....':
    array_pop($arr2); array_pop($arr2); array_pop($arr2);
    break;
   case '.....':
    array_pop($arr2); array_pop($arr2); array_pop($arr2); array_pop($arr2);
    break;
   default:
    $arr2[] = $seg;
  }
 }
 return implode('/',$arr2);
}

Usage Example:

echo nodots(absurl('../index.html'));

nodots() must be called after the URL is converted to absolute.

The dots function is kind of redundant, but is readable, fast, doesn't use regex's, and will resolve 99% of typical urls (if you want to be 100% sure, just extend the switch block to support 6+ dots, although I've never seen that many dots in a URL).

Hope this helps,

Aaron Gillion
  • 2,227
  • 3
  • 19
  • 31
0
$uri = "..";
$path = realpath($uri);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);

if($path){
    $path = str_replace($root, "", $path);
    $path = $_SERVER["SERVER_NAME"] . $path;
    $protocol = "http";
    if(isset($_SERVER["HTTPS"])){
        $protocol .= "s";        
    }
    $path = "{$protocol}://$path";
    $path = str_replace("\\", "/", $path);
}

var_dump($path);

There is probably a better/quicker way, but I just knocked this up...

0

A web browser uses the page URL or the base tag to resolve relative URLs.

This script can resolve a URL or URI relative to a base URL. The URL or URI is resolved in the common way used by browsers.

/** Build a URL
 *
 * @param array $parts An array that follows the parse_url scheme
 * @return string
 */
function build_url($parts)
{
    if (empty($parts['user'])) {
        $url = $parts['scheme'] . '://' . $parts['host'];
    } elseif(empty($parts['pass'])) {
        $url = $parts['scheme'] . '://' . $parts['user'] . '@' . $parts['host'];
    } else {
        $url = $parts['scheme'] . '://' . $parts['user'] . ':' . $parts['pass'] . '@' . $parts['host'];
    }

    if (!empty($parts['port'])) {
        $url .= ':' . $parts['port'];
    }

    if (!empty($parts['path'])) {
        $url .= $parts['path'];
    }

    if (!empty($parts['query'])) {
        $url .= '?' . $parts['query'];
    }

    if (!empty($parts['fragment'])) {
        return $url . '#' . $parts['fragment'];
    }

    return $url;
}

/** Convert a relative path into an absolute path
 *
 * @param string $path
 * @return string
 */
function abs_path($path)
{
    $path_array = explode('/', $path);

    // Solve current and parent folder navigation
    $translated_path_array = array();
    $i = 0;
    foreach ($path_array as $name) {
        if ($name === '..') {
            unset($translated_path_array[--$i]);
        } elseif (!empty($name) && $name !== '.') {
            $translated_path_array[$i++] = $name;
        }
    }

    return '/' . implode('/', $translated_path_array);
}

/** Convert a relative URL in to an absolute URL
 *
 * @param string $url URL or URI
 * @param string $base Absolute URL
 * @return string
 */
function abs_url($url, $base)
{
    $url_parts = parse_url($url);
    $base_parts = parse_url($base);

    // Handle the path if it is specified
    if (!empty($url_parts['path'])) {
        // Is the path relative
        if (substr($url_parts['path'], 0, 1) !== '/') {
            // Does the base path end with slash?
            if (substr($base_parts['path'], -1) === '/') {
                $url_parts['path'] = $base_parts['path'] . $url_parts['path'];
            } else {
                $url_parts['path'] = dirname($base_parts['path']) . '/' . $url_parts['path'];
            }
        }

        // Make path absolute
        $url_parts['path'] = abs_path($url_parts['path']);
    }

    // Use the base URL to populate the unfilled components until a component is filled
    foreach (['scheme', 'host', 'path', 'query', 'fragment'] as $comp) {
        if (!empty($url_parts[$comp])) {
            break;
        }
        $url_parts[$comp] = $base_parts[$comp];
    }

    return build_url($url_parts);
}

Test

// Base URL
$base_url = 'https://example.com/path1/path2/path3/path4/file.ext?field1=value1&field2=value2#fragment';

// URL and URIs (_ is used to see what is coming from relative URL)
$test_urls = array(
    "http://_example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment", // URL
    "//_example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment",      // URI without scheme
    "//_example.com",                                                                        // URI with host only
    "/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment",                    // URI without scheme and host
    "_path1/_path2/_file.ext",                                                               // URI with path only
    "./../../_path1/../_path2/file.ext#_fragment",                                           // URI with path and fragment
    "?_field1=_value1&_field2=_value2#_fragment",                                            // URI with query and fragment
    "#_fragment"                                                                             // URI with fragment only
);

// Expected result
$expected_urls = array(
    "http://_example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment",
    "https://_example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment",
    "https://_example.com",
    "https://example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment",
    "https://example.com/path1/path2/path3/path4/_path1/_path2/_file.ext",
    "https://example.com/path1/path2/_path2/file.ext#_fragment",
    "https://example.com/path1/path2/path3/path4/file.ext?_field1=_value1&_field2=_value2#_fragment",
    "https://example.com/path1/path2/path3/path4/file.ext?field1=value1&field2=value2#_fragment"
);

foreach ($test_urls as $i => $url) {
    $abs_url = abs_url($url, $base_url);
    if ( $abs_url == $expected_urls[$i] ) {
        echo  "[OK] " . $abs_url . PHP_EOL;
    } else {
        echo  "[WRONG] " . $abs_url . PHP_EOL;
    }
}

Result

[OK] http://_example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment
[OK] https://_example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment
[OK] https://_example.com
[OK] https://example.com/_path1/_path2/_file.ext?_field1=_value1&_field2=_value2#_fragment
[OK] https://example.com/path1/path2/path3/path4/_path1/_path2/_file.ext
[OK] https://example.com/path1/path2/_path2/file.ext#_fragment
[OK] https://example.com/path1/path2/path3/path4/file.ext?_field1=_value1&_field2=_value2#_fragment
[OK] https://example.com/path1/path2/path3/path4/file.ext?field1=value1&field2=value2#_fragment
Diblo Dk
  • 585
  • 10
  • 26