Finding the Current URL with the PHP Language

When you go about building your PHP scripts, you will soon find that there are many cases in which you need to know the current URL. PHP 5 comes loaded with several functions in which to do so, meaning that there will be very little programming on your part in the process.PHP Web Development Company

PHP uses what are called server variables to access different pieces of information about the current HTTP request, as well as a myriad of other miscellaneous server information that can come in quite handy. It should be noted before we get into the specifics of server variables that some information can be spoofed or faked- so be carefully when trusting HTTP information.

The first server variable to consider is HTTP_HOST. This server variable will allow you to find the current domain that is being browsed. Most of the time you already know the domain, so this is probably one of the least useful variables to you. Also keep in mind that this variable will store the domain in the format of “” – so you will have to include prefix or trailing path information to build a proper link.

The SCRIPT_NAME server variable is also of great use, as it can find the actual path to the running script. This is most used to find the current location of the file so that the developer can make a link to the current page. This is mostly done for search engine optimization reasons, but also helps out in the overall ease of use of the application you are working on.

The query string is a valuable tool used to pass information from one script to another. We use the server variable QUERY_STRING to reference it. Be very careful that you don’t forget to encode all of your URL strings, so that malicious users don’t try and befuddle your database information. With even a simple line of code and the right security hole, your entire database could be deleted in seconds without URL encoding.

A great way to get the current URL even if you are using rewriting on your domain is to use the server variable REQUEST_URI. This variable will ignore any rewriting information that the “.htaccess” file tells the server to perform, and instead report the actual name of the running script. This is must-know tool for anyone who makes use of the Mod Rewrite tool.

BCA Students Projects Training VadodaraMCA Students Projects Training in Vadodara

Pin It