blog
HOME · CREATIVE · WEB · TECH · BLOG

Friday, September 12th, 2008

PHP 101: Using Apache Envirnoment Variables

This is written for beginners. There may be aspects of the post which aren't technically true if you really know the ins and outs of PHP, but they're essentially true, and are a good starting point for people just starting to learn PHP.

Building on PHP variables, there's a special type of variable called an array that can hold multiple values. Let's say you had 10 students and you wanted pull all their names into one variable - you would use an array for that since you have 10 values that are all names. In other words, think of arrays are groupings of similar values.

Where this is important for the person just starting to use PHP is that you'll want to use PHP to understand the context of the web page that you're serving. For example, is the person requesting the page using an iPhone or other mobile device, or is it googlebot crawling your site to put pages in Google search, or is it a regular web user? To answser those questions you need the HTTP_USER_AGENT variable from Apache (the web server software that's probably running on your server). All of the values that describe the page request are called "enviroment variables" and they're passed to PHP in an array.

The PHP array that holds the Apache environment variables is named $_SERVER. Let's say you wanted to print out on the web page the type of browser the person is using. To do that you'd have some php code that looks like this:

<?php echo $_SERVER['HTTP_USER_AGENT']; ?>

The result would look like this:

CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

That's a lot more information than you really want to know, so lets go into how you might use that value...

In the last post on PHP variables I mentioned that PHP had what are called functions that return various values. In that post we saw that rand(1,10) would return a number between 1 and 10.

The function we want to use to make sense of the user agent string is the stripos() function. This function returns the position of string B inside string A.

In other words, stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot') would return a number that number is the character position inside the user agent string. If the number is greater than zero, then the user agent contains 'googlebot'.

Let's try an example...

<?php
if (stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot') > 0) {
echo "You are Googlebot"; }
elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'firefox') > 0) {
echo "You are using Firefox"; }
elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE') > 0) {
echo "You are using Internet Explorer"; }
else {
echo "You are not Googlebot, and you are using something other than Firefox or Internet Explorer"; }
?>

Here's what it would say if you were using that on a page...

You are not Googlebot, and you are using something other than Firefox or Internet Explorer

Try it in a different browser and you'll see it change!

That can be used in a variety of ways. Imagine using it to specify one stylesheet for the iPhone and another for everyone else. All you need to do is see if iPhone can be found in the user agent string.

I should pause a moment and talk about the if, elseif, else statements used above. The basic syntax is:

if (something that resolves to true or false) { the php code you want to have execute if true }
elseif (something that resolves to true or false) { the php code you want to have execute if the first if case is false and this elseif case is true }
else { the php code you want to have execute if all the cases above are false }

When doing comparisons for things that will return true or false these are some of the most common ways to compare two items...

  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

Remember that elseif and else add onto what's gone before. You can also start something completely new by just using another if ().

User agent isn't the only thing you can get from Apache environment variables. Here's a list of other environment variables that a web designer would be likely to use...

  • HTTP_REFERER - (notice 'referrer' is misspelled referer). The referrer is the URL of the page that referred the user to the current page. You could use this to see if the referrer contained your domain name and, if not, welcome the new visitor to your site even if they land on a page that's deep into your site.
  • DOCUMENT_ROOT - This is typically used when you have to specify the path to a document. you're likely to want to use it on includes, but I'll do a different post on that subject...
  • REMOTE_ADDR - The IP address of the person browsing the site. This is useful if you are doing something sensitive and need to record who the person is.
  • HTTP_ACCEPT_LANGUAGE - Contains language codes for the languages the user would like the page returned in. If you have a multilingual site you can detect the user's preferred language and automatically present the page in the language that's best for them.
  • HTTP_COOKIE - As you get more advanced you can store values in cookies and retrieve them. Let's say your site is bilingual French and English and the user wants to over-ride the default language and see the site in English. You can store this in a cookie and then check the cookie before using a default language.

There are quite a few other environment variables, but you're not as likely to use them as you are the ones above.

So to wrap up...

  • Apache environment variables tell you about the context of the web page request. Things like the referrer, the user agent (type of browser), the user's IP address, etc.
  • Apache environment variables are accessed in PHP through the $_SERVER array that holds their values.
  • You learned how to handle basic if, elseif, else statements
  • And you now have a list of common environment variables that you're likely to use

Tags:
Categories: PHP

Previous Post: « PHP 101: Includes

Leave a Reply

HOME · CREATIVE · WEB · TECH · BLOG