3 # Copyright (C) 2005 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # return our best guess at the url used to access this page, without the path or query string
20 function this_url_sans_path() {
21 if(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == 'yes') {
30 if($_SERVER['HTTP_HOST']) {
31 $url .= $_SERVER['HTTP_HOST'];
33 $url .= $_SERVER['SERVER_NAME'];
34 if($_SERVER['SERVER_PORT'] != $expected_port) {
35 $url .= ':' . $_SERVER['SERVER_PORT'];
42 # just the hostname, no port number
43 function this_host() {
44 if($_SERVER['HTTP_HOST']) {
45 $host = $_SERVER['HTTP_HOST'];
46 $p = strpos($host, ':');
48 $host = substr($host, 0, $p);
52 return $_SERVER['SERVER_NAME'];
58 # return our best guess at the url used to access this page
60 $url = this_url_sans_path();
62 $url .= $_SERVER['REQUEST_URI'];
67 # sends an HTTP redirect
72 # 3) a filename (you can pass a directory/file.html and such, but "../" prefix is not supported yet)
73 function redirect($url, $status = '302 Moved Temporarily', $message = '') {
74 if(!strpos($url, ':')) {
75 while(substr($url, 0, 2) == './') {
76 $url = substr($url, 2);
78 if(substr($url, 0, 1) == '/') {
79 $url = this_url_sans_path() . $url;
81 $url = ereg_replace('/[^/]*$', "/$url", this_url());
85 if(function_exists('session_save_messages')) {
86 session_save_messages();
89 header("HTTP/1.0 $status");
90 header("Location: $url");
95 # output http headers to allow caching of this page forever
96 function http_cache_forever($cache_control = 'public') {
97 header('Last-Modified: '.gmdate('D, d M Y H:i:s', 5025) . ' GMT'); # long time ago
98 header("Cache-Control: $cache_control");
99 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