Chapter 18:
Further Adventures in PHP

Google

Query strings revisited

Beginning in Chapter 9, we looked at some of the things you can do with query strings. It sounded very simple and easy - and it was. But there are some problems with using query strings that I haven't talked about. Let's look first at an affiliate link that uses a query string:

http://hop.clickbank.net/?kaspar/merrymonk&p=2

That's a ClickBank affiliate link for a reseller of this ebook. It's pretty long, hard to remember and hard to understand. It doesn't look exactly like a normal ClickBank hop link - but that's another story altogether. Some search engines don't index pages with a ? in them.

Sure, you can use link cloakers and get your link indexed. You can redirect it through another page or use a URL redirection service. But sooner or later the search engines will see through these tricks...if they don't already. Don't panic! There IS another way. Look at this:

http://www.learntousephpintwohours.com/c/doofus.html

See the difference? This is my cloaked version of the link above. The page doofus.html has no content to interest the search engines - but you could add content and optimize it for keywords if you wanted to. It looks like our troubles are over. Well, there's good news and bad news. First, the bad news...

Since the identifying parts of the URL that used to be in a query string are now part of the path, PHP won't extract them automatically. You won't get those local variables that made it so easy to get the data from the query string. You'll have to do a little extra work to get them. Now for the good news.

There's an environment variable called "$REQUEST_URI" that's sent to the web server when the page request is made. We can unpack the data from here as if it were parts of a path! We can take it apart piece by piece and put it back together as local variables. Here's how:

First, let's get all the pieces of $REQUEST_URI into an array by using the explode() function. We know that they are separated by "/" so we use that in the explode() function to split them up:

$parts = explode("/", $REQUEST_URI);

Now these items (everything after the domain name) are in a array or list. Here's what's in our list: ("c" and "doofus.html"). The array is indexed from 0 to 1. So we have this: $parts[0] = "c" and $parts[1] = "doofus.html".

"c" is the name of a folder, so it makes sense to have a variable named $folder. "doofus.html" is file, so why not name that variable $file?

Now we can write:

$folder = $parts[0];
$file = $parts[1];

Put it together and you have:

<?php
$parts = explode("/", $REQUEST_URI);
$folder = $parts[0];
$file = $parts[1];
?>

If the url has more than two parts after the domain name, you may want to write a loop to get them all into variables. The point is: you can do whatever you want with the data once it's made into variables. Write them to a database, etc. That brings me to my next topic.

Tracking Hits with a Database

Back in Chapter 14 I covered the mechanics of connecting to a database and writing to it. I mentioned that you could make a more efficient tracking tool using a database than with a text file. Now I'm going to show you exactly how I do it.

I'll start by assuming you've created a database called "traffic" and that you have created a user for this database. That's a simple task - especially if you have a tool like phpMyAdmin. Tools like that let you point and click to create and run SQL commands.

Now you need a table to store the traffic data you'll be collecting. Let's call it "hits" and create it now. If you're using a GUI like phpMyAdmin you can just copy this command to the text box and click the button. Here it is:

CREATE TABLE `hits` (
`ip` varchar(16) default NULL,
`date` datetime default NULL,
`browser` varchar(16) default NULL,
`os` varchar(16) default NULL,
`engine` varchar(100) default NULL,
`lang` varchar(16) default 'Other',
`keywords` varchar(255) default NULL,
`page` varchar(100) default NULL,
`newuser` varchar(16) default 'No',
`last_visit` datetime default NULL,
`days_ago` tinyint(4) default NULL,
KEY `index1` (`date`)
) TYPE=MyISAM;;

Poof! You just made a brand-new table in your database. It's empty now but it's ready to store all your tracking data. The uri field is pretty special. Actually, it's what makes this table so great. Its value will come from the environment variable $REQUEST_URI. This variable contains everything after the first / in the page request (including the /). Say what?

Suppose someone visits a page whose URL is http://www.mysite.com/page5.php . What's after the first / ? Answer: page5.php. See? Now $REQUEST_URI will contain "/page5.php". For the home page, it will only contain "/" unless there's also a query string. Can you see why this is important? By storing this value in the database, we can search for hits on a specific page.

For example, if we asked the database to shows us the stats for page5.php, we'd only get the results of hits on that specific page.

One table will hold the hits info for as many pages as we want - without wasting space. But wait - this keeps getting better! You can use one chunk of code to do the hits tracking and it can be the same on each page you want to track. You can create the tracking script once and include it at the bottom of each page you want to track.

Because this script needs to check and set "cookies", it has to be called before any HTML headers are sent. So the trick is to add this line before the opening <>HTML> tag. Make it the very first line of each page you want to track:

<?php include ("tracker.php"); ?>

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey