3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
9 # return our best guess at the url used to access this page, without the path or query string
10 function this_url_sans_path() {
11 if (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == 'yes')) {
20 if($_SERVER['HTTP_HOST']) {
21 $url .= $_SERVER['HTTP_HOST'];
23 $url .= $_SERVER['SERVER_NAME'];
24 if($_SERVER['SERVER_PORT'] != $expected_port) {
25 $url .= ':' . $_SERVER['SERVER_PORT'];
32 # just the hostname, no port number
33 function this_host() {
34 if($_SERVER['HTTP_HOST']) {
35 $host = $_SERVER['HTTP_HOST'];
36 $p = strpos($host, ':');
38 $host = substr($host, 0, $p);
42 return $_SERVER['SERVER_NAME'];
48 # return our best guess at the url used to access this page
50 $url = this_url_sans_path();
52 $url .= $_SERVER['REQUEST_URI'];
57 # sends an HTTP redirect
62 # 3) a filename (you can pass a directory/file.html and such, but "../" prefix is not supported yet)
63 function redirect($url, $status = '302 Moved Temporarily', $message = '') {
64 if(!strpos($url, ':')) {
65 while(substr($url, 0, 2) == './') {
66 $url = substr($url, 2);
68 if(substr($url, 0, 1) == '/') {
69 $url = this_url_sans_path() . $url;
71 $url = preg_replace('|/[^/]*$|', "/$url", this_url());
75 if(function_exists('session_save_messages')) {
76 if(function_exists('atexit_now')) {
79 session_save_messages();
82 header("HTTP/1.0 $status");
83 header("Location: $url");
88 # output http headers to allow caching of this page forever
89 function http_cache_forever($cache_control = 'public') {
90 header('Last-Modified: '.gmdate('D, d M Y H:i:s', 5025) . ' GMT'); # long time ago
91 header("Cache-Control: $cache_control");
92 header('Expires: ' . gmdate('D, d M Y H:i:s',time()+31536000) . ' GMT'); # rfc 2616 says not to go more than a year in the future