示例#1
0
文件: functions.php 项目: phpf/http
 /**
  * Returns HTTP-relevant environment (server) information.
  * 
  * @param string $var One of 'ssl', 'host', 'domain', or 'protocol'.
  * @return mixed 'ssl' returns boolean; the rest return a string.
  */
 function http_env($var)
 {
     switch (strtolower($var)) {
         case 'ssl':
             static $ssl;
             if (!isset($ssl)) {
                 if ($https = getenv('https')) {
                     $ssl = 'on' === strtolower($https) || 1 == $https;
                 } else {
                     if ('https' === getenv('http_x_forwarded_proto') || 443 == getenv('server_port')) {
                         $ssl = true;
                     } else {
                         $ssl = false;
                     }
                 }
             }
             return $ssl;
         case 'host':
             static $host;
             if (!isset($host)) {
                 $host = rtrim(getenv('http_host'), '/\\') . rtrim(dirname(getenv('script_name')), '/\\');
             }
             return $host;
         case 'domain':
             static $domain;
             if (!isset($domain)) {
                 $domain = 'http' . (http_env('ssl') ? 's' : '') . '://' . ltrim(http_env('host'), '/');
             }
             return $domain;
         case 'protocol':
             return http_env('ssl') ? 'https' : 'http';
         default:
             trigger_error("Invalid HTTP env variable '{$var}'.");
             return null;
     }
 }
示例#2
0
文件: Native.php 项目: phpf/micro
 /**
  * If given array of cookie parameters, sets cookie parameters.
  */
 public function __construct(array $cookieparams = array())
 {
     ini_set('session.use_cookies', 1);
     $this->cookie_params = array_replace(array('lifetime' => 86400 * 7, 'path' => '/', 'domain' => '.' . http_env('host'), 'secure' => false, 'httponly' => false), $cookieparams);
 }
示例#3
0
/**
 * Takes an associative array in the layout of parse_url, and constructs a URL
 * from it.
 *
 * @author FuelPHP
 * @see http://www.php.net/manual/en/function.http-build-url.php
 *
 * @param mixed (Part(s) of) an URL in form of a string or associative array like
 * parse_url() returns
 * @param mixed Same as the first argument
 * @param int A bitmask of binary or'ed HTTP_URL constants (Optional)
 * HTTP_URL_REPLACE is the default
 * @param array If set, it will be filled with the parts of the composed url like
 * parse_url() would return
 * @return string constructed URL
 */
function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false)
{
    $keys = array('user', 'pass', 'port', 'path', 'query', 'fragment');
    if ($flags & HTTP_URL_STRIP_ALL) {
        // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
        $flags |= HTTP_URL_STRIP_USER;
        $flags |= HTTP_URL_STRIP_PASS;
        $flags |= HTTP_URL_STRIP_PORT;
        $flags |= HTTP_URL_STRIP_PATH;
        $flags |= HTTP_URL_STRIP_QUERY;
        $flags |= HTTP_URL_STRIP_FRAGMENT;
    } else {
        if ($flags & HTTP_URL_STRIP_AUTH) {
            // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
            $flags |= HTTP_URL_STRIP_USER;
            $flags |= HTTP_URL_STRIP_PASS;
        }
    }
    // parse the original URL
    $parsed = is_array($url) ? $url : parse_url($url);
    // make sure we always have a scheme, host and path
    if (empty($parsed['scheme'])) {
        static $scheme;
        isset($scheme) or $scheme = 'http' . (http_env('ssl') ? 's' : '');
        $parsed['scheme'] = $scheme;
    }
    if (empty($parsed['host'])) {
        static $host;
        isset($host) or $host = http_env('host');
        $parsed['host'] = $host;
    }
    if (!isset($parsed['path'])) {
        $parsed['path'] = '';
    }
    // make the path absolute if needed
    if (!empty($parsed['path']) && '/' !== substr($parsed['path'], 0, 1)) {
        $parsed['path'] = '/' . $parsed['path'];
    }
    // scheme and host are always replaced
    if (isset($parts['scheme'])) {
        $parsed['scheme'] = $parts['scheme'];
    }
    if (isset($parts['host'])) {
        $parsed['host'] = $parts['host'];
    }
    // replace the original URL with it's new parts (if applicable)
    if ($flags & HTTP_URL_REPLACE) {
        foreach ($keys as $key) {
            if (isset($parts[$key])) {
                $parsed[$key] = $parts[$key];
            }
        }
    } else {
        // join the original URL path with the new path
        if (isset($parts['path']) && $flags & HTTP_URL_JOIN_PATH) {
            if (isset($parsed['path'])) {
                $parsed['path'] = rtrim(str_replace(basename($parsed['path']), '', $parsed['path']), '/') . '/' . ltrim($parts['path'], '/');
            } else {
                $parsed['path'] = $parts['path'];
            }
        }
        // join the original query string with the new query string
        if (isset($parts['query']) && $flags & HTTP_URL_JOIN_QUERY) {
            if (isset($parsed['query'])) {
                $parsed['query'] .= '&' . $parts['query'];
            } else {
                $parsed['query'] = $parts['query'];
            }
        }
    }
    // Skip this part if default flag
    if (HTTP_URL_REPLACE !== $flags) {
        // strips all the applicable sections of the URL
        // note: scheme and host are never stripped
        foreach ($keys as $key) {
            if ($flags & (int) constant('HTTP_URL_STRIP_' . strtoupper($key))) {
                unset($parsed[$key]);
            }
        }
    }
    $new_url = $parsed;
    $url = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
    if (isset($parsed['user'])) {
        $pass = isset($parsed['pass']) ? ':' . $parsed['pass'] : '';
        $url .= $parsed['user'] . $pass . '@';
    }
    $url .= isset($parsed['host']) ? $parsed['host'] : '';
    $url .= isset($parsed['port']) ? ':' . $parsed['port'] : '';
    $url .= isset($parsed['path']) ? $parsed['path'] : '';
    $url .= isset($parsed['query']) ? '?' . $parsed['query'] : '';
    $url .= isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';
    return $url;
}