JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed this_url() for https
[wfpl.git] / http.php
index 437ec74..1fad97b 100644 (file)
--- a/http.php
+++ b/http.php
 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 
-# return our best guess at the url used to access this page
-function this_url() {
-       list($protocol, $version) = explode('/', $_SERVER['SERVER_PROTOCOL']);
-       $url = strtolower($protocol);
-
-       if($url == 'http') {
-               $expected_port = 80;
-       } elseif ($url == 'https') {
+# return our best guess at the url used to access this page, without the path or query string
+function this_url_sans_path() {
+       if(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == 'yes') {
+               $url = 'https';
                $expected_port = 443;
        } else {
-               $expected_port = -1;
+               $url = 'http';
+               $expected_port = 80;
        }
 
        $url .= '://';
@@ -42,13 +39,57 @@ function this_url() {
                }
        }
 
+       return $url;
+}
+
+# just the hostname, no port number
+function this_host() {
+       if($_SERVER['HTTP_HOST']) {
+               $host = $_SERVER['HTTP_HOST'];
+               $p = strpos($host, ':');
+               if($p) {
+                       $host = substr($host, 0, $p);
+               }
+               return $host;
+       } else {
+               return $_SERVER['SERVER_NAME'];
+       }
+}
+
+
+
+# return our best guess at the url used to access this page
+function this_url() {
+       $url = this_url_sans_path();
+
        $url .= $_SERVER['REQUEST_URI'];
 
        return $url;
 }
 
+# sends an HTTP redirect
+#
+# $url can be:
+#   1) a full URL
+#   2) an absolute path
+#   3) a filename (you can pass a directory/file.html and such, but "../" prefix is not supported yet)
 function redirect($url, $status = '302 Moved Temporarily', $message = '') {
-       header("HTTP/1.1 $status");
+       if(!strpos($url, ':')) {
+               while(substr($url, 0, 2) == './') {
+                       $url = substr($url, 2);
+               }
+               if(substr($url, 0, 1) == '/') {
+                       $url = this_url_sans_path() . $url;
+               } else {
+                       $url = ereg_replace('/[^/]*$', "/$url", this_url());
+               }
+       }
+                       
+       if(function_exists('session_save_messages')) {
+               session_save_messages();
+       }
+
+       header("HTTP/1.0 $status");
        header("Location: $url");
        echo($message);
        exit();