Пример #1
0
function prepareBaseUrl()
{
    $filename = basename($_SERVER['SCRIPT_FILENAME']);
    if (basename($_SERVER['SCRIPT_NAME']) === $filename) {
        $baseUrl = $_SERVER['SCRIPT_NAME'];
    } elseif (basename($_SERVER['PHP_SELF']) === $filename) {
        $baseUrl = $_SERVER['PHP_SELF'];
    } elseif (basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
        $baseUrl = $_SERVER['ORIG_SCRIPT_NAME'];
        // 1and1 shared hosting compatibility
    } else {
        // Backtrack up the script_filename to find the portion matching
        // php_self
        $path = $_SERVER['PHP_SELF'];
        $file = $_SERVER['SCRIPT_FILENAME'];
        $segs = explode('/', trim($file, '/'));
        $segs = array_reverse($segs);
        $index = 0;
        $last = count($segs);
        $baseUrl = '';
        do {
            $seg = $segs[$index];
            $baseUrl = '/' . $seg . $baseUrl;
            ++$index;
        } while ($last > $index && false !== ($pos = strpos($path, $baseUrl)) && 0 != $pos);
    }
    // Does the baseUrl have anything in common with the request_uri?
    $requestUri = prepareRequestUri();
    if ($baseUrl && false !== ($prefix = getUrlencodedPrefix($requestUri, $baseUrl))) {
        // full $baseUrl matches
        return $prefix;
    }
    if ($baseUrl && false !== ($prefix = getUrlencodedPrefix($requestUri, dirname($baseUrl)))) {
        // directory portion of $baseUrl matches
        return rtrim($prefix, '/');
    }
    $truncatedRequestUri = $requestUri;
    if (($pos = strpos($requestUri, '?')) !== false) {
        $truncatedRequestUri = substr($requestUri, 0, $pos);
    }
    $basename = basename($baseUrl);
    if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
        // no match whatsoever; set it blank
        return '';
    }
    // If using mod_rewrite or ISAPI_Rewrite strip the script filename
    // out of baseUrl. $pos !== 0 makes sure it is not matching a value
    // from PATH_INFO or QUERY_STRING
    if (strlen($requestUri) >= strlen($baseUrl) && (false !== ($pos = strpos($requestUri, $baseUrl)) && $pos !== 0)) {
        $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
    }
    return rtrim($baseUrl, '/');
}
Пример #2
0
<?php

/**
 * Parse the URL and clean it.
 */
$uri_ = prepareRequestUri();
$url_ = prepareBaseUrl();
$uri_filtred = str_replace($url_, '', $uri_);
$uri_args = explode('/', $uri_filtred);
array_shift($uri_args);
/**
 * Initializer object URL to get argument of the URL 
 * if their here.
 */
try {
    $url = new Url();
    $args = explode('?', $uri_filtred);
    if (count($args) > 1) {
        $temp = explode('&', $args[1]);
        $url->setUrlArguments($temp);
    }
    foreach ($uri_args as $key => $value) {
        /**
         * Check if url contains some arguments 
         * Example : /path/to/web/model/?order=DESC
         */
        if (strpos($value, '?') !== false) {
            $value = strstr($value, '?', true);
        }
        /**
         * Use the object URL and set the different parts of the URL