Fri, 17 May 2024


PHP Super Globals

By: Peter, NetArt Media
Wed, 1 August 2018
PHP Super Globals

There are a lot of super global options within the PHP language, so I could never cover them all and provide examples for each in one article. I can, however, give you an idea of how to implement and utilize a few of the globals which I personally use for gathering information about visitors and use to provide them with better surfing experiences on websites.


The code block above contains several of the PHP super globals I use myself on a regular basis for creating scripts and websites. Some globals will produce the same results, so while I have filtered out the ones that work the best for me, you may still notice that 'SERVER_NAME' and 'HTTP_HOST' will return the same data on certain servers. Especially those of the development variety, which is where I create the examples for these articles.

Take note of my use of a conditional statement before attempting to echo the referring URL. Because I create these examples on a development server on a PC, the referrer isn't set and therefore will produce a PHP error. Although used quite often, 'HTTP_REFERRER' is not a highly reliable way of returning referring URLs. Keep that in mind if you plan on using it and always using a conditional statement before attempting to return results from it.

/super_globals.php
phpstuts
phpstuts
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
127.0.0.1
Apache/2.4.9 (Win64) PHP/5.5.12
HTTP/1.1
55221

If you visit this site regularly then you have probably seen me use 'PHP_SELF' quite often. I like to use it to get the URL of a web page currently being viewed. 'REQUEST_URI' also works for this. I, however, also like to use an additional function with 'PHP_SELF', which is 'basename'. This strips out the previous slash before the file name within the URL.

I also like to include the substr function to remove the file extension, the result usually presenting the name of the page being viewed. Now, that isn't always the case, but it can still reap some information about the page currently being viewed within a browser.


The code above produces the below return.

super_globals

Now I can turn this data into a page title, if desired.


Super Globals

By including the 'ucwords' and str_replace functions we can produce a title, which you can use for your own beneficial statistics, or perhaps use to enhance a visitor's experience on your website.

'SERVER_ADDR' is a very handy PHP global function to use. Although not always reliable, as some visitors will use VPN's, proxy servers and other IP changing protocols when surfing, it can provide the means to allow or deny access to particular URLS hosted on a server. My IP blocking article is proof of the power of this super global.

As you can see, PHP super globals are very powerful and can provide both you and the end user with information and better surfing experiences. PHP is quite a good language when it comes to gathering server side data and is one of the many reasons I like it so much.

I hope this article has been helpful to you. Don't forget to use super globals whenever you feel they're necessary. They are there to take advantage of.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox