One thing I’ve noticed about PHP is that you need to jump through all sorts of hoops when it’s time to detect URL structures. Either pulling the existing page URL or dissecting a passed URL for certain components just felt like it was too time consuming to code and re-code with slight changes every single time. Maybe there is an even better way to do this, but I’ve developed a pair of functions that takes the chore out of working with URLs.
The first function simply pulls the current page URL and returns it. There are many ways to do this and every programmer has his own reasons of why his way is the best (and plenty of reasons why everyone elses’ is awful). While we know that the $_SERVER variable can be spoofed, this method detects whether the page is using a secure protocol and then pulls the hostname followed by the rest of the requested URI.
The other function is where the components of the requested URL (or passed URL) are dissected and returned. Additionally, I’ve provided aliases for the components so multiple phrasings can be used as needed without confusion. The currently supported components are as follows. I’ll be using this long URL as my example: http://something.gearside.com/nebula/documentation/custom-functionality/nebula-url-components/filename.php?query=something
Returns the entire URL: “http://something.gearside.com/nebula/documentation/custom-functionality/nebula-url-components/filename.php?query=something”
Returns just the protocol (or false if none): “http”
Returns the subdomain (if present), SLD, and TLD: “something.gearside.com”
Returns “www” if it is present, otherwise returns false: “0”
Returns the subdomain if it exists (and is not “www”), otherwise returns false: “something”
Returns the SLD and TLD: “gearside.com”
Returns the protocol and the domain: “https://gearside.com”
Returns only the second level domain: “gearside”
Returns only the top level domain: “com”
Returns the entire path to the file including the extension: “/nebula/documentation/custom-functionality/nebula-url-components/filename.php”
Returns only the file and extension: “filename.php”
Returns only the file’s extension: “php”
Returns only the path to the file’s directory (not including the file itself): “/nebula/documentation/custom-functionality/nebula-url-components/”
Returns the query parameters of the URL if present: “query=something”
To use the functions, simply paste this into your PHP file:
You only need to call gearside_url_components() since it defaults to the current page URL, so the parameters are as follows:
The segment is what you want returned (from above), and the URL is an optional passed URL string.
I’m not sure if there is an even better way to do this, but these functions have made my life easier for quickly being able to pull data from URLs without constantly re-writing code. To see this in action (and test how it responds to specific URLs), check it out here.