Chapter 9: Query Strings |
|
What is a query string? | |
First, you need to understand a little about "environment variables". They have names like QUERY_STRING, REQUEST_METHOD, REMOTE_HOST, etc. They are all passed between client and server during processing of a PHP program. Data can be extracted from them that tells you all sorts of things about the connections, the type and length of content being passed, and so on. The $QUERY_STRING variable holds all the inputs that your form deliberately sent from the client (their web browser) to the server (computer where the script is running). PHP has arrays of environment variables. They're called global arrays because every PHP script has access to them. You don't have to create them or anything; they're always available to you. The web server will put information into environment variables and PHP reads it for you. An important environment variable is QUERY_STRING. |
|
|
The values of form inputs are loaded into QUERY_STRING when the "submit" button is clicked. They are sent to the server that way and PHP knows how to find them. PHP programs automatically extract any name/value pairs in the query string and create local variables for them as soon as the file is loaded. You can have more than one block of PHP code in the same file and more than one section of pure HTML. You can have more than one form on a page. You can also have different forms that appear or don't appear - under the control of your script. |
Tracking traffic with a query string |
|
The values of form inputs are loaded into QUERY_STRING when the "submit" button is clicked. They are sent to the server that way and PHP knows how to find them. PHP programs automatically extract any name/value pairs in the query string and create local variables for them as soon as the file is loaded. You can have more than one block of PHP code in the same file and more than one section of pure HTML. You can have more than one form on a page. You can also have different forms that appear or don't appear - under the control of your script. Have you ever noticed a bunch of characters beginning with a "?" following the URL of a Web page? That's a query string. This allows tracking hits on the page. The server logs will tell you how many times the URL with a particular query string was requested. By using different query strings in your ads, you can find out which ads were more effective at getting click-throughs to the page! You may not want to dig through the log files on your server - and you don't have to! You can capture the contents of the query string and log it to a file. A script can add a line containing your data to a file. The file can be easily read or further processed by another script. You can make a script read the file and get a count of hits on each version of the page. Here's an illustration of what I mean. Suppose you have these
two URLs: By logging the query strings "ad1" and "ad2" each time the page is requested, you have a record of the hits on each. Here's a simple piece of code to do it: <?php
Explanation: First, we put the name of the file to open into a local variable ($file). Then, we ask for a handle or pointer to the file and open the file for appending (adding lines to the file, starting at the end of the file). Then we write our data to the file and add a newline (\n) character. Finally, we close and save the updated file. Want to track more info on your visitors? No sweat! You can grab a whole bunch of handy data from the environment variables and tack it onto the line you're about to write to your log file. I suggest using a non-text character such as "|" as a separator between items on the line. This will make it easier to unpack it later and display it in a table on an HTML page. That's a nice way to get an easily readable report. OK, I hear what you're thinking. How do I do that? Grab that extra info? Relax, it's easy. Suppose you want to know what browser they are using, their IP address and the URL (with query string) that led them to your page. These lines will capture that information into local variables: $date = date("Y-m-d h:i:s"); What if they were sent to your page by a search engine? Wouldn't you like to know what search terms they used to find you? Of course you would. Why? Because that will help you find out which search terms are actually working for you! Here's an example of a referring URL from www.google.com: http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=website+income The first thing you should notice is that this URL contains a query string: ?hl=en&ie=UTF-8&oe=UTF-8&q=website+income. Capture this into a local variable the way I showed you already. The parts "hl=en&ie=UTF-8" and "oe=UTF-8" aren't going to be much help to you, so use a search-and-replace function to get rid of them. Now all that's left is: q=website+income. This is where the keywords are! You don't need the "q=" part either, so you can just make it part of the stuff you're going to throw away with that search-and-replace function. Now what's left is a string of keywords separated by the "+" sign. Use the explode() function to make a list of the keywords. You've seen this done before. $keywords = explode("\+", $qs); Now $keywords contains these items: website, income. If you did this every time someone landed on your page, you'd soon have a clear picture of which keywords are good for getting you traffic. If you keep a count of how many times each keyword was used, you'll be able to see which ones were used more often than others. Surely you can think of a lot of good uses for this information! But wait! What about the other search engines? Will the same thing work with them? Maybe and maybe not - at least not without some modifications. First, you'll need to know what the URLs they generate to send surfers to your site look like. Isolate the query string and figure out where the keywords are. For each one you check out, write a line to strip it down to just the list of keywords. How can you be sure which part contains the search terms/keywords? Easy! Go to the search engine yourself and do a search. You'll know what search terms you used. Look for them in the query string of the URL the search engine creates. To decide which code to use for extracting the query string, just check the top-level URL (for example, www.lycos.com) with an if-statement and then run the code for parsing that site's query string. It would look like this: if (ereg("google", $url)) Want to get a little fancier? Sure you do! Log the number of hits per keyword or keyword phrase sorted by search engines and you have a real traffic-analysis program that delivers meaningful results. Correlate these results with your conversion ratio (sales divided by hits x 100%) for each search engine and for keywords used on that search engine. You can see at a glance which search engines and keywords bring you paying customers. Spend your time optimizing your sales page for high page rank on the search engines using the keywords that help you make sales - and forget the rest! It's still very true that the majority of traffic comes from the search engines. This should be obvious: Real people are typing keywords into search engines, 24/7/365. People who want to buy what you're selling probably don't type in the same keywords as people who aren't buying. The better you get at figuring out what keywords real paying customers are using, the better your odds of making sales become. Remember that not all hits are equal. Focus on getting the hits that are likely to turn into sales. There are many scammers out there who promise you massive traffic. They might get you lots of hits but if those hits don't convert to sales, you wasted your time and money getting those hits. Knowledge is power and it can lead to wealth. |
| Previous Page Table of Contents Next Page |
Copyright © 2004 Steve Humphrey |