mr_mind Posted December 30, 2007 Share Posted December 30, 2007 To some this may be superfluous, to others that word may be to long to understand, and i know that $_SERVER['PHP_SELF'] may work, it just doesnt do it for me in the way of URLs. I think that URLs should be complete and $_SERVER['PHP_SELF'] does not do that. So i came up with a short function which will do all of that for you <?php // First we start the "whoami()" function function whoami() { // We want to find out the server protocol in the URL. This is // usually "http" but can be "https" so we wont make any // assumptions here. // We know how to get something close to that which is // getenv('SERVER_PROTOCOL') // though this will give us some excess information like so // HTTP/1.1 // As you can tell we just want what comes before the "/". // What we are going to do is use "strpos" (position in string) // to find out where the "/" is and then substr (returns part // of the string) to get everything before it. $slash_strpos = strpos(getenv('SERVER_PROTOCOL'), '/'); // This, if getenv('SERVER_PROTOCOL') returns HTTP/1.1, will // return 4. $slash_substr = substr(getenv('SERVER_PROTOCOL'), 0, $slash_strpos); // Now we have something like "HTTP" but we want "http://" so // we make it lower case and add "://" to it. $whoami = strtolower($slash_substr) . '://'; // Now we need the domain name. We will go about this by using // the following string. $whoami .= getenv('SERVER_NAME'); // If the URL typed in is "http://www.example.com/test.php?x=1" // we will get "www.example.com" from that string. // Now we need the path to the current script. For that we will // use SCRIPT_NAME/ $whoami .= getenv('SCRIPT_NAME'); // If we have the URL as before we will get "/test.php". // Next we need the query in the URL. To do this we will use // QUERY_STRING if anything is passed through the URL. if(!empty($_GET)) { // If anything is then we have to put the "?" before it // or we will get "http://www.example.com/test.phpx=1". $whoami .= '?' . getenv('QUERY_STRING'); } // When we are finished we will have something like // http://www.example.com/test.php?x=1 // And that is what we return return $whoami; } // Now i know that it may be a pain adding a query to the URL after this // not knowing what was already passed to the URL for certain so if you // wanted to do that you would type something like this $url = whoami(); if(strpos(whoami(), '?') !== false) { $url .= '&'; } else { $url .= '?'; } $url .= 'y=2'; print $url; // If the URL was originally "http://www.example.com/test.php?x=1" we // would print "http://www.example.com/test.php?x=1&y=2" but if there // was nothing passed through the URL we would have // "http://www.example.com/test.php?y=2" ?> Link to comment https://forums.phpfreaks.com/topic/83762-whoami/ Share on other sites More sharing options...
Daniel0 Posted January 1, 2008 Share Posted January 1, 2008 You missed the port. Link to comment https://forums.phpfreaks.com/topic/83762-whoami/#findComment-427376 Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Share Posted January 4, 2008 I would still use PHP_SELF. Link to comment https://forums.phpfreaks.com/topic/83762-whoami/#findComment-429813 Share on other sites More sharing options...
mr_mind Posted January 8, 2008 Author Share Posted January 8, 2008 ahhh yes damn, the port Link to comment https://forums.phpfreaks.com/topic/83762-whoami/#findComment-433379 Share on other sites More sharing options...
Recommended Posts