Senior Trainer-Instructor- Developer C++ / C UNIX & Web-JAVA / J2EE,PhD or Masters
WSI Nationwide, Inc.
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Where Did They Go Today? Knowing Where People Go When They Leave Your Site.
Have you ever wondered where people go when they leave your site? With php3 it is easy to trace this. Sure, several solutions of redirect-cgi's exist, but why bother with another language when you can keep it straight to php3 and make it more comfortable?
My setting for writing this from scratch was a site where I had multiple pages with identical links in a part of them, but I'd wanted to know the use of the external links in detail: Who left where from which page and when. So here is how it works:
When you read the documentation about php3 in detail, you may have come across this kind of one-liner: <?php Header("Location: http://www.php.net"); exit; ?> You find it in Section XIV. It uses a HTTP redirect command to make the users browser access a new location. This is a good example how simple things in PHP3 can be: We will expand it, so that we make a suitable tool for the curious ;).
First we make targeting more flexible, it is the 'where'. This one is easy, because you just add a parameter to the URL of the redirector page. I'll call it <?php '$u_target' ?> . (u_ stands for URI/URL, this is a personal way to determine where the variable belongs to that I am using. Others: db_ is for database , p_ for page , f_ for function and so on.)
Then we want to record from 'which' page the link was activated. Since the referrer-information is sometimes filtered out by proxies, browsers and firewalls, we will use the additional variable <?php '$u_referrer' ?> for this purpose. You can use simple keywords like 'homepage', the name of the page example.html or the full address "http://...". It doesn't really matter, but keywords are better to understand on first look. When you skip this parameter, <?php $HTTP_REFERER ?> is used automatically. Note that if it doesn't exist either, <?php '$u_referrer' ?> will be set to 'none'.
We will determine the "who" by using server-variables. We'll take <?php $REMOTE_ADDR ?> address for this purpose, since Apache and IIS deliver it in nearly 90% of all installations ;). <?php $REMOTE_HOST ?> is better but since DNS-lookup is switched off sometimes, you'll have to try out on your server yourself. (Use <?php phpinfo() ?> for this purpose. It's tip I got from Tim and others which lead also to this script. Thanks ;)
'When' is easy: <?php time() ?> is all we need.
Logging the information gathered to a database is the method of choice. Since the data-structure is simple and flat, you should be able to use any kind of solutions - even plain text files will do it. Since MySQL is nice for this job, we'll take it for our purpose.
Since this article focuses on redirecting, I will keep the part of information exchange with the database brief. You'll find more information on this topic here in phpbuilder.com and on the web.
Here is the SQL-Command for creating the table in MySQL:
# Definition
# Table structure for table \'redirect\'
#
CREATE TABLE redirect (
  target varchar(80) DEFAULT \'no target\' NOT NULL,
  referrer varchar(80) DEFAULT \'none\' NOT NULL,
  client varchar(40) DEFAULT \'no client\' NOT NULL,
  timeused datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL
);
[ Next Page ]


Comments:
PHP problemViktor Stevich12/10/05 07:42
RE: how to build a search enginedaren06/15/05 13:26
RE: What if they type url in browser?James06/27/03 05:56
RE: how to build a search engineJosh Petrovich09/20/02 23:21
Header errorSurya08/10/02 12:35
IP AddressGreg03/07/02 08:55
RE: I don't get itJack10/04/01 07:06
RE: how to build a search enginePyroX10/01/01 15:42
another possiblity?David Rossiter09/20/01 09:50
RE: I don't get itOlivier Hill06/17/01 18:18
how to build a search enginecolin kelly05/28/01 08:22
What time is it now() ?Rob Halff05/15/01 01:04
RE: I don't get itDavid Davis02/02/01 14:07
RE: What if they type url in browser?David Davis02/02/01 13:54
RE: Another possible useBrett01/29/01 16:03
What if they type url in browser?Dzool01/09/01 14:59
Another possible useEric Naujock01/05/01 21:59
It's very usefulSunhawk10/28/00 09:06
RE: I don't get itColin Gilboy10/27/00 14:51
RE: I don't get itPat O\'Neill10/12/00 14:20
I don't get itOlivier Hill10/12/00 00:17
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.