/** * Gets an integer from the COOKIE data. * * \param $key The name of the integer. * \param $default The default value to return if no valid integer was * found. If you omit this parameter, null is used. * * \return A valid integer or the default value. */ public static function cookie_int($key, $default = null) { return array_get_int($_COOKIE, $key, $default); }
/** * Test array_get_int */ function test_array_get_int() { $data = array('first' => 1, 'second' => '2', 'third' => 'drei', 4 => 4); $this->assertEquals(1, array_get_int($data, "first")); $this->assertEquals(1, array_get_int($data, "first", 2)); $this->assertEquals(3, array_get_int($data, "notfound", 3)); $this->assertEquals(33, array_get_int($data, "third", 33)); $this->assertEquals(4, array_get_int($data, "4")); $this->assertEquals(4, array_get_int($data, 4)); }
/** * Returns the canonical base URL for the current request. This includes the * http part, the hostname and (optionally) port number. * * \return * The canonical base URL for the current request. * * \see Request::canonical_url */ static function canonical_base_url() { /* Protocol: http or https? */ $is_ssl = array_get_default($_SERVER, 'HTTPS', 'off') === 'on'; $protocol = $is_ssl ? 'https' : 'http'; /* The host */ $host = Request::host(); /* Get the port number */ $port = array_get_int($_SERVER, 'SERVER_PORT', 80); /* The SERVER_PORT is not always correct. Use the one from HTTP_HOST * instead, if any is set. Be very careful with this string since it is * user-provided. */ if (array_key_exists('HTTP_HOST', $_SERVER) && preg_match('/:([0-9]{1,5})$/', $_SERVER['HTTP_HOST'], $matches)) { $port = (int) $matches[1]; } /* Now build the url part for the port number. It's empty if not needed * because the default ports are used. */ $port_str = !$is_ssl && $port != 80 || $is_ssl && $port != 430 ? sprintf(':%d', $port) : ''; return sprintf('%s://%s%s', $protocol, $host, $port_str); }