Monthly Archives: April 2015

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 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.

Closing Comments

As another warning, consider the many security flaws you can put into your application by relying on server variables without regards to your application’s health. Always be sure to sanitize all input when interacting with your database and query strings, and remember HTTP information isn’t always trustworthy.

Learn more about current URL in php and php basics.

Related Php Coding Articles

Visit my website at for all of my videos and tutorials! Have questions or looking for source code? Check out the forum at …
Video Rating: 4 / 5

php coding eBay auctions you should keep an eye on:

Eat Sleep Code V2 Shirt - programmer, php, code monkey, code blooded ID: 734

End Date: Thursday May-10-2018 17:44:09 PDT
Buy It Now for only: $19.99
Buy It Now | Add to watch list

CodeIgniter 1.7 : Improve Your PHP Coding Productivity with the Free Compact...
End Date: Tuesday May-15-2018 18:18:20 PDT
Buy It Now for only: $4.32
Buy It Now | Add to watch list

PHP Master : Write Cutting-Edge Code
End Date: Saturday May-12-2018 22:15:36 PDT
Buy It Now for only: $4.09
Buy It Now | Add to watch list


Join With Us