Jump to content

whoami()


mr_mind

Recommended Posts

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
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.