This post in reply several years later, just in case another Web-weary traveler Googles by: using SSL changes things a bit, depending on the server and the configuration. It is not uncommon for the $SERVER_URI variable--which contrary to the prior post is indeed a valid PHP variable that gives the entire URL string minus the query string (which can be tacked on with the addition $QUERY_STRING as shown below)--to suddenly become unavailable.
if($_SERVER['QUERY_STRING']){
$query_string='?'.$_SERVER['QUERY_STRING'];
}
$url=$_SERVER['SCRIPT_URI'].$query_string;
Of course, the reason we would want to have that variable when using SSL is because we want to be able to tell whether or not the user has accessed our page via the non-secure HTTP protocol or whether the user has assessed our page correctly through the HTTPS protocol. If the user is using the non-secure HTTP, we need to be able to redirect to the secure HTTPS page.
If the $SERVER['SERVER_URI'] variable is not available, add
print_r($_SERVER)
to the code to get a run-down of all the $SEVER variables (which may or may not be available via the phpinfo() command). There should be at least one variable there you can use to detect the presence of HTTPS.
In my case, I was able to use HTTPS which returns 1 if true, nothing if false. Thus, you need something like this at the very top of your page, before any blank lines (which can also trip you up when other files are included or required) or other output is generated:
if($_SERVER['HTTPS']!=1){
Header('Location: https://www.mysite.com'.$_SERVER['PHP_SELF']);
}
===
Mr. Renaissance http://www.mrrena.com