If I understood your question correctly, it looks like you simply need $_SERVER['REQUEST_URI']
If that's not what you're trying to do, I suggest you to take a look at PHP's parse_url function, which parses any given URL and returns all its parts in a tidy array. It can also return a single specific part.