JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / http.php
1 <?php
2
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/
7
8
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')) {
12                 $url = 'https';
13                 $expected_port = 443;
14         } else {
15                 $url = 'http';
16                 $expected_port = 80;
17         }
18
19         $url .= '://';
20         if($_SERVER['HTTP_HOST']) {
21                 $url .= $_SERVER['HTTP_HOST'];
22         } else {
23                 $url .= $_SERVER['SERVER_NAME'];
24                 if($_SERVER['SERVER_PORT'] != $expected_port) {
25                         $url .= ':' . $_SERVER['SERVER_PORT'];
26                 }
27         }
28
29         return $url;
30 }
31
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, ':');
37                 if($p) {
38                         $host = substr($host, 0, $p);
39                 }
40                 return $host;
41         } else {
42                 return $_SERVER['SERVER_NAME'];
43         }
44 }
45
46
47
48 # return our best guess at the url used to access this page
49 function this_url() {
50         $url = this_url_sans_path();
51
52         $url .= $_SERVER['REQUEST_URI'];
53
54         return $url;
55 }
56
57 # sends an HTTP redirect
58 #
59 # $url can be:
60 #   1) a full URL
61 #   2) an absolute path
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);
67                 }
68                 if(substr($url, 0, 1) == '/') {
69                         $url = this_url_sans_path() . $url;
70                 } else {
71                         $url = preg_replace('|/[^/]*$|', "/$url", this_url());
72                 }
73         }
74                         
75         if(function_exists('session_save_messages')) {
76                 if(function_exists('atexit_now')) {
77                         atexit_now();
78                 }
79                 session_save_messages();
80         }
81
82         header("HTTP/1.0 $status");
83         header("Location: $url");
84         echo($message);
85         exit();
86 }
87
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
93 }