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.
Requested URL and URL Components Functions
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
“all”, or “href”
Returns the entire URL: “http://something.gearside.com/nebula/documentation/custom-functionality/nebula-url-components/filename.php?query=something”
“protocol”, “scheme”, or “schema”
Returns just the protocol (or false if none): “http”
“host”, or “hostname”
Returns the subdomain (if present), SLD, and TLD: “something.gearside.com”
Returns “www” if it is present, otherwise returns false: “0”
“subdomain”, or “sub_domain”
Returns the subdomain if it exists (and is not “www”), otherwise returns false: “something”
Returns the SLD and TLD: “gearside.com”
“basedomain”, “base_domain”, or “origin”
Returns the protocol and the domain: “https://gearside.com”
“sld”, “second_level_domain”, or “second-level_domain”
Returns only the second level domain: “gearside”
“tld”, “top_level_domain”, or “top-level_domain”
Returns only the top level domain: “com”
“filepath”, or “pathname”
Returns the entire path to the file including the extension: “/nebula/documentation/custom-functionality/nebula-url-components/filename.php”
“file”, or “filename”
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/”
“query”, “queries”, “search”
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.